Chart使用示例

Load事件添加如下代码:

Chart chart1 = new Chart();
            ChartArea myArea = new ChartArea();
            Series mySerise = new Series();

            mySerise.Type = SeriesChartType.Line;


            double[] valueY = { 12, 67, 45, double.NaN, 67, 89, 35, 12, 78, 54 };
            string[] valueX = { "D", "A", "B", "A", "C", "C", "B", "A", "C", "B" };


            chart1.ChartAreas.Add(myArea);
            chart1.Series.Add(mySerise);

            //设置X的刻度显示
            myArea.AxisX.LabelStyle.Interval = 1;
            myArea.AxisX.MajorGrid.Interval = 1;
            myArea.AxisX.MajorTickMark.Interval = 1;
            myArea.AxisX.Title = "时间";

            //设置Y轴刻度显示,
            myArea.AxisY.LabelStyle.Interval = 5;
            myArea.AxisY.MajorGrid.Interval = 10;
            myArea.AxisY.MajorTickMark.Interval = 5;
            myArea.AxisY.Title = "油压";

            mySerise.XValueIndexed = true;
            mySerise.XValueType = ChartValueTypes.Auto;


            //可以是一张表的两个字段绑定图表显示                                                                            //mySerise.Points.DataBindXY(this.dataTab.DefaultView,"字段A",dataTab.DefaultView,"字段B");

            //也可以是数组,x值可以是数字或者字符串,但是y值必须是数          
            mySerise.Points.DataBindXY(valueX, valueY);

            chart1.Dock = DockStyle.Fill;
            Controls.Add(chart1);
效果图

2.柱状图

public void zhuhzuangTu()
        {
            //01 创建Chart对象
            Chart chart = new Chart();
            //02 创建ChartArea对象
            ChartArea chartArea = new ChartArea();
            //03 把ChartArea添加到Chart对象中
            chart.ChartAreas.Add(chartArea);
            //04 创建Series对象
            Series series1 = new Series();
            series1.Name = "测试细目";
            //05 创建Point
            Double[] db1 = { 22 };
            Double[] db2 = { 44 };
            DataPoint dp1 = new DataPoint();
            dp1.XValue = 1;
            dp1.YValues = db1;
            DataPoint dp2 = new DataPoint();
            dp2.XValue = 2;
            dp2.YValues = db2;
            //06 加入点
            series1.Points.Add(dp1);
            series1.Points.Add(dp2);
            //07 加入series1
            chart.Series.Add(series1);
            //08 定义表头 标题
            Title t = new Title();
            t.Text = "我是标题我怕谁";
            t.Color = Color.Red;
            t.Font = new Font("Times New Roman", 12, FontStyle.Bold);
            t.Alignment = System.Drawing.ContentAlignment.BottomCenter;
            chart.Titles.Add(t);
            //09 定义展现位置坐标
            chart.Location = new System.Drawing.Point(0, 0);
            //10 Chart的大小
            chart.Size = new System.Drawing.Size(360, 260);
            //11 展现Chart
            this.Controls.AddRange(new System.Windows.Forms.Control[] { chart });
        }
赞(0) 打赏
未经允许不得转载:TaKaSa » Chart使用示例

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  • Q Q(选填)

赞助下

支付宝扫一扫打赏

微信扫一扫打赏