DevExpress ChartControl动态创建按月份按姓名对比的柱状图对比图
DevExpress ChartControl动态创建按月份按姓名对比的柱状图对比图
数据结构
ChartControl效果
C#源码
动态创建Series组件
根据表1,循环年份,自动生成按年对比数据的柱状图
C# 全选
//获取数据源
DataSet dataSource = _BLL.GetTongJiData(year);
chartControl1.Series.Clear();
chartControl2.Series.Clear();
chartControl3.Series.Clear();
foreach (DataRow R in dataSource.Tables[0].Rows)
{
dataSource.Tables[1].DefaultView.RowFilter = $"yyyymm='{R["yyyymm"].ToString()}'";
var source = dataSource.Tables[1].DefaultView.ToTable();
var series = new Series(R["yyyymm"].ToString(), ViewType.Bar);
series.DataSource = source;
series.SetDataMembers("syr", "avgTotal");
SetView(series);
chartControl1.Series.Add(series);
this.chartControl1.Titles[0].Text = year + "年平均每车单子数";
var series2 = new Series(R["yyyymm"].ToString(), ViewType.Bar);
series2.DataSource = source;
series2.SetDataMembers("syr", "total1");
SetView(series2);
chartControl2.Series.Add(series2);
this.chartControl2.Titles[0].Text = year + "年单子数";
var series3 = new Series(R["yyyymm"].ToString(), ViewType.Bar);
series3.DataSource = source;
series3.SetDataMembers("syr", "total2");
SetView(series3);
chartControl3.Series.Add(series3);
this.chartControl2.Titles[0].Text = year + "年车数";
}
设置Label样式
C# 全选
private void SetView(Series series)
{
// 显示Label
series.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;
// 对齐方式
series.Label.TextAlignment = StringAlignment.Center;
// 文本方向
series.Label.TextOrientation = TextOrientation.Horizontal;
// Label位置
(series.Label as BarSeriesLabel).Position = BarSeriesLabelPosition.Top;
}
设置Legend样式
C# 全选
//设置Legend样式
chartControl1.Legend.Visibility = DefaultBoolean.True;
chartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
chartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.BottomOutside;
chartControl1.Legend.Direction = LegendDirection.LeftToRight;
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网