C# Chart控件使用心得

本文详细介绍了如何在RDLC报表中利用Chart控件进行数据可视化,并强调了Y轴必须为数值类型的重要性。通过示例代码展示了在WinForm中创建3D图表的过程,包括填充数据、设置图表类型、调整图表样式等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chart 控件可以直接使用,也可以在RDLC报表上使用,  如果会使用RDLC报表,那么在RDLC报表使用比较容易.

但是有一点我认为是通用的,那就是Y轴 必须 是数字的, 不能是文本的, 这点非常重要.

在RDLC报表 为了保证 Y轴是数字,可以用以下的办法

 <Field Name="ga42">
          <DataField>ga42</DataField>
          <rd:TypeName>System.decimal</rd:TypeName>
        </Field>

在WINFORM  最简单的代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;


namespace _3DChartDemo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }


        private void Form2_Load(object sender, EventArgs e)
        {
            FillCylinderChart();
        }
        void FillCylinderChart()
        {
            //Y值一定要是数值,因为有刻度线
            string sql = @"SELECT ID,ga42 FROM Qcount WHERE ga51='2015-04-01' ORDER BY ID";
            string con = "server=xx-pc;database=HNBD;uid=sa;pwd=1";
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            List<string> x = new List<string>();
            List<decimal> y = new List<decimal>();
            foreach (DataRow dr in dt.Rows)
            {
                x.Add(dr[0].ToString());
                y.Add(decimal.Parse(dr[1].ToString()));
            }      
            this.chart1.Legends.Clear();           
            this.chart1.Series[0].ChartType = SeriesChartType.Spline;//波浪状图
            this.chart1.Series[0]["DrawingStyle"] = "Cylinder";
            this.chart1.Series[0].Points.DataBindXY(x, y);          
        }


    }
}

手动修改是折线图还是柱状图

Series-charttype

  this.chart1.Legends.Clear();
            this.chart1.Series[0].ChartType = SeriesChartType.Line;//折线图
            this.chart1.Series[0]["DrawingStyle"] = "Cylinder";
            this.chart1.Series[0].Points.DataBindXY(x, y);
            // 宽度
            chart1.Series[0].BorderWidth = 2;
            // 颜色
            chart1.Series[0].Color = Color.BlueViolet;
            //数据点的大小
            chart1.Series[0].MarkerSize = 5;
            //获取公差范围
            int ID = int.Parse(txtZE07.SelectedValue.ToString());
            Maticsoft.BLL.Aparason bll2 = new Maticsoft.BLL.Aparason();
            Maticsoft.Model.Aparason model2 = bll2.GetModel(ID);
            if (model2 == null)
            {
                return;
            }
            lblstrd.Text = "上限 " + model2.strd;
            lblstre.Text = "中心限 " + model2.stre;
            lblstrf.Text = "下限 " + model2.strf;
            //增加公差范围
            chart1.ChartAreas[0].AxisY.StripLines.Add(new StripLine());
            chart1.ChartAreas[0].AxisY.StripLines[0].BackColor = Color.FromArgb(65,217, 38, 65);//背景色
            chart1.ChartAreas[0].AxisY.StripLines[0].StripWidth = Double.Parse((model2.strd-model2.strf).ToString());//带状线宽度
            //chart1.ChartAreas[0].AxisY.StripLines[0].Interval =40;//带状线间隔
            chart1.ChartAreas[0].AxisY.StripLines[0].IntervalOffset = Double.Parse(model2.strf.ToString());//设定带状线偏移量
            //chart1.ChartAreas[0].AxisY.Maximum = 7;//设定y轴的最大值
            //chart1.ChartAreas[0].AxisY.Minimum= 6.7;//设定y轴的最小值
            //myChart1.ChartAreas[0].AxisX.Maximum = 1000;//设定x轴的最大值
            //myChart1.ChartAreas[0].AxisY.Maximum = 1000;//设定y轴的最大值搜索
            //myChart1.ChartAreas[0].AxisX.Minimum = 0;//设定x轴的最小值
            //myChart1.ChartAreas[0].AxisY.Minimum = 0;//设定y轴的最小值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值