C#中向Chart中添加数据

在C#中有时需要将数据放入图表中进行直观展示,现介绍如何DataTable将数据添加到Chart

一、数据准备,DataTable数据

二、将DataTable数据添加到Chart数据源

chart1.DataSource = rentreturnTable;

初始化图标

chart1.Series.Clear();
chart1.Titles.Clear();
chart1.ChartAreas.Clear();
chart1.ChartAreas.Add("ChartArea1");

设置标题

chart1.Titles.Add("各租赁点日租还总量表 蓝色表示租借 黄色表示归还");
chart1.ChartAreas[0].AxisX.Title = "租赁点编号";
chart1.ChartAreas[0].AxisY.Title = "自行车数/辆";

设置元素

Series srent = new Series();
srent.ChartType = SeriesChartType.Column;
//srent.BorderWidth = 3;
srent.BorderColor = Color.Blue;
srent.Points.DataBind(rentreturnTable.AsEnumerable(), "STATIONID", "rentnum", "");
chart1.Series.Add(srent);
Series sreturn = new Series();
sreturn.ChartType = SeriesChartType.Column;
//sreturn.BorderWidth = 3;
sreturn.BorderColor = Color.Yellow;
sreturn.Points.DataBind(rentreturnTable.AsEnumerable(), "STATIONID", "returnnum", "");
chart1.Series.Add(sreturn);
chart1.ChartAreas[0].AxisX.Interval = 1;   //设置X轴坐标的间隔为1
chart1.ChartAreas[0].AxisX.IntervalOffset = 1;  //设置X轴坐标偏移为1
//chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true; 
chart1.Series[0].IsValueShownAsLabel = true;
chart1.Series[1].IsValueShownAsLabel = true;

全部代码展示

			chart1.DataSource = rentreturnTable;
                        //初始化图表
                        chart1.Series.Clear();
                        chart1.Titles.Clear();
                        chart1.ChartAreas.Clear();
                        chart1.ChartAreas.Add("ChartArea1");
                        //设置标题
                        chart1.Titles.Add("各租赁点日租还总量表 蓝色表示租借 黄色表示归还");
                        chart1.ChartAreas[0].AxisX.Title = "租赁点编号";
                        chart1.ChartAreas[0].AxisY.Title = "自行车数/辆";
                        //设置元素
                        Series srent = new Series();
                        srent.ChartType = SeriesChartType.Column;
                        //srent.BorderWidth = 3;
                        srent.BorderColor = Color.Blue;
                        srent.Points.DataBind(rentreturnTable.AsEnumerable(), "STATIONID", "rentnum", "");
                        chart1.Series.Add(srent);
                        Series sreturn = new Series();
                        sreturn.ChartType = SeriesChartType.Column;
                        //sreturn.BorderWidth = 3;
                        sreturn.BorderColor = Color.Yellow;
                        sreturn.Points.DataBind(rentreturnTable.AsEnumerable(), "STATIONID", "returnnum", "");
                        chart1.Series.Add(sreturn);
                        chart1.ChartAreas[0].AxisX.Interval = 1;   //设置X轴坐标的间隔为1
                        chart1.ChartAreas[0].AxisX.IntervalOffset = 1;  //设置X轴坐标偏移为1
                        //chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true; 
                        chart1.Series[0].IsValueShownAsLabel = true;
                        chart1.Series[1].IsValueShownAsLabel = true;


你可能感兴趣的:(C#,c#,chart)