/// <summary>
/// 特征重要性
/// </summary>
/// <param name="ImportanceList"></param>
/// <param name="FeaturesList"></param>
private void CreateGraphColumn(List<string> ImportanceList, List<string> FeaturesList)
{
//绘图区域
ChartArea chartArea = new ChartArea();
//chartArea.AxisY.Title = "Relative Importance";
chartArea.AxisX.MajorGrid.Enabled = false;
//chartArea.AxisY.MajorGrid.Enabled = false;
chartArea.AxisY.MajorGrid.LineColor = Color.Gray;
//自定义标签
List<string> customNameList = FeaturesList.Select(m =>
{
string name = worksheet.GetCustomNameByColumnName(m);
if (string.IsNullOrEmpty(name))
return m;
else
return name;
}).ToList(); //自定义列名
double fromPosition = 0.5;
for (int i = 0; i < customNameList.Count; i++)
{
CustomLabel customLabel = new CustomLabel();
customLabel.RowIndex = 0;
customLabel.Text = customNameList[i];
customLabel.FromPosition = fromPosition;
customLabel.ToPosition = fromPosition + 1;
chartArea.AxisX.CustomLabels.Add(customLabel);
fromPosition += 1;
}
//添加数据源
Series series = new Series();
for (int i = 0; i < ImportanceList.Count; i++)
{
series.Points.AddXY(i + 1, ImportanceList[i]);
}
//图表标题
Title title = new Title();
if (Program.language.Equals("zh-CN"))
{
title.Text = "特征重要性";
title.Font = new Font("宋体", 12, FontStyle.Bold);
}
else
{
title.Text = "Feature Importance";
title.Font = new Font("Arial", 12, FontStyle.Bold);
}
//绘图控件
Chart chart = new Chart();
chart.ChartAreas.Add(chartArea);
chart.Series.Add(series);
chart.Titles.Add(title);
chart.Dock = DockStyle.Fill;
chart.MouseMove += new MouseEventHandler(ChartColumnGraph_MouseMove); //鼠标悬停事件
//容器
Panel pnlGraph = new Panel();
pnlGraph.Dock = DockStyle.Fill;
pnlGraph.Controls.Add(chart);
//新建窗体
Form frmGraphColumn = new Form();
frmGraphColumn.TopLevel = false;
frmGraphColumn.ShowIcon = false;
frmGraphColumn.Size = new Size(1000, 600);
frmGraphColumn.Controls.Add(pnlGraph);
TMMTab frmParent = (this.Owner) as TMMTab;
frmParent.Mainpanel.Controls.Add(frmGraphColumn);
frmGraphColumn.Show();
frmGraphColumn.BringToFront();
frmGraphColumn.FormClosing += new FormClosingEventHandler(frmGraph_FormClosing);
//显示窗体位置:居中显示
Rectangle screenRect = Screen.PrimaryScreen.WorkingArea;
int x = screenRect.Left + (screenRect.Width - frmGraphColumn.Width) / 2;
int y = screenRect.Top + (screenRect.Height - frmGraphColumn.Height) / 2;
frmGraphColumn.Location = new Point(x, y);
}
C#动态创建窗体显示Chart柱状图表
最新推荐文章于 2024-09-16 10:36:43 发布