C#动态创建窗体显示Chart柱状图表


        /// <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);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值