欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# Chart控件实现柱形图和折线图在一个chart上同时显示,并显示滚动条

程序员文章站 2022-05-30 18:30:24
...

效果如下:左侧纵坐标表示柱形图Y轴数据,右侧纵坐标表示折线图Y轴数据

C# Chart控件实现柱形图和折线图在一个chart上同时显示,并显示滚动条

代码:

private void ButtonClickSelect()
        {
            string GD = textBoxGD.Text.Trim();
            string XH = textBoxXH.Text.Trim();
            string CJ = comboBoxCJ.Text.Trim();

            string Date1 = dateTimePicker1.Value.ToString("yyyyMMdd HH:mm:ss");
            string Date2 = dateTimePicker2.Value.ToString("yyyyMMdd HH:mm:ss");

            DataTable dt = BLL.MES.GetNGBoLaTuP1(GD, XH, CJ, Date1, Date2);

            if (dt != null && dt.Rows.Count > 0)
            {
                string ID_UNIQUE = BLL.Config.GetNewID();
                dt.Columns.Add("BLL");
                dt.Columns.Add("ID");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["BLL"] = (Math.Round(decimal.Parse(dt.Rows[i]["NGQty"].ToString()) / decimal.Parse(dt.Rows[i]["TR"].ToString()), 2) * 100).ToString() + "%";
                    dt.Rows[i]["ID"] = ID_UNIQUE;                    
                }
                bool flag = Util.DBHelper.AddTable(dt, "NGBoLaTuTempSave");

                if (flag)
                {
                    chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
                    chart1.ChartAreas["ChartArea1"].Area3DStyle.LightStyle = LightStyle.Realistic;
                    chart1.ChartAreas["ChartArea1"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
                    chart1.ChartAreas["ChartArea1"].CursorX.AutoScroll = true;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.Enabled = true;
                    chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
                    chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Position = 1;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size = 10;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.ButtonColor = System.Drawing.Color.Silver;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.LineColor = System.Drawing.Color.Black;

                    chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
                    chart1.ChartAreas[0].AxisX.Interval = 1; //设置X轴坐标的间隔为1
                    chart1.ChartAreas[0].AxisX.IntervalOffset = 1; //设置X轴坐标偏移为1
                    chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;                    
                    chart1.ChartAreas[0].BackColor = Color.White;//设置背景为白色
                    //chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;//启用X轴滚动条按钮

                    dt = BLL.MES.GetNGBoLaTuTempSaveData(ID_UNIQUE);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        chart1.Series[0].Points.Clear();                        
                        chart1.Series[0].IsValueShownAsLabel = true;//显示标签
                        
                        for (int m = 0; m < dt.Rows.Count; m++)
                        {
                            for (int n = 0; n < dt.Columns.Count; n++)
                            {
                                chart1.Series[0].Points.AddXY(dt.Columns[n].ColumnName, dt.Rows[m][n].ToString());
                            }
                        }
                    }

                    dt = BLL.MES.GetNGBoLaTu2TempSaveData(ID_UNIQUE);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        chart1.Series[1].Points.Clear();
                        chart1.Series[1].IsValueShownAsLabel = true;//显示标签
                        chart1.Series[1].MarkerStyle = MarkerStyle.Circle;   //线条上的数据点标志类型
                        chart1.Series[1].MarkerSize = 8;  //标志大小
                        // 宽度
                        chart1.Series[1].BorderWidth = 2;
                        // 线条颜色
                        chart1.Series[1].Color = Color.Red;
                        //数据点颜色
                        chart1.Series[1].MarkerColor = Color.BlueViolet;
                        chart1.Series[1].Label = "#VAL{P}";//设置标签文本 (在设计期通过属性窗口编辑更直观)
                        chart1.ChartAreas[0].AxisY2.LabelStyle.Format = "0.00%";
                        
                        for (int m = 0; m < dt.Rows.Count; m++)
                        {
                            for (int n = 0; n < dt.Columns.Count; n++)
                            {
                                chart1.Series[1].Points.AddXY(dt.Columns[n].ColumnName, double.Parse(dt.Rows[m][n].ToString().Trim('%'))*0.01);
                            }
                        }
                    }


                    //查询完成之后,删除临时表数据
                    string sql = "";
                    List<SqlParameter> paras = new List<SqlParameter>();
                    sql = "delete NGBoLaTuTempSave where ID = @ID";
                    paras = new List<SqlParameter>() { 
                        new SqlParameter("@ID",ID_UNIQUE),
                        };
                    string str = Util.DBHelper.Action(sql, paras);
                    
                    str = Util.DBHelper.Action(sql, paras);


                }

            }
        }

1、以下代码是显示横坐标滚动条

chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
                    chart1.ChartAreas["ChartArea1"].Area3DStyle.LightStyle = LightStyle.Realistic;
                    chart1.ChartAreas["ChartArea1"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
                    chart1.ChartAreas["ChartArea1"].CursorX.AutoScroll = true;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.Enabled = true;
                    chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
                    chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Position = 1;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size = 10;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.ButtonColor = System.Drawing.Color.Silver;
                    chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.LineColor = System.Drawing.Color.Black;

2、这是设置第二条折线的纵坐标以百分号形式显示

chart1.ChartAreas[0].AxisY2.LabelStyle.Format = "0.00%";

3、chart1.Series[0]表示第一条曲线,chart1.Series[1]表示第二条曲线

4、如何设置纵坐标显示在右侧,如下图

C# Chart控件实现柱形图和折线图在一个chart上同时显示,并显示滚动条

相关标签: C# Chart c#