我想添加类似的自定义标签(类别1和类别2),但不知道我是否需要添加新系列或使用自定义标签功能..任何帮助都将非常感谢。
这是我的数据表,
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Section";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);
DataRow dr;
dr = dt.NewRow();
dr["Question"] = "A";
dr["Section"] = "Category 1";
dr["User1"] = "1";
dr["User2"] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "B";
dr["Section"] = "Category 1";
dr["User1"] = "4";
dr["User2"] = "5";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "C";
dr["Section"] = "Category 1";
dr["User1"] = "2";
dr["User2"] = "5";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "X";
dr["Section"] = "Category 2";
dr["User1"] = "3";
dr["User2"] = "5";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "Y";
dr["Section"] = "Category 2";
dr["User1"] = "5";
dr["User2"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "Z";
dr["Section"] = "Category 2";
dr["User1"] = "6";
dr["User2"] = "5";
dt.Rows.Add(dr);
我的图表绘制代码,
Series Series = new Series("Series1");
Chart4.Series.Add("Series1");
Chart4.Series.Add("Series2");
Chart4.ChartAreas.Add("ChartArea1");
// Set series chart type
Chart4.Series["Series1"].ChartType = SeriesChartType.Bar;
Chart4.Series["Series2"].ChartType = SeriesChartType.Bar;
// Draw as 3D Cylinder
Chart4.Series["Series1"]["DrawingStyle"] = "Cylinder";
Chart4.Series["Series2"]["DrawingStyle"] = "Cylinder";
Chart4.DataSource = dt.DefaultView;
Chart4.ChartAreas["ChartArea1"].AxisX.Interval = 1;
Chart4.Series["Series1"].XValueMember = "Question";
Chart4.Series["Series1"].YValueMembers = "User1";
Chart4.Series["Series2"].YValueMembers = "User2";
Chart4.DataBind();
解决方案
DataTable dt = new DataTable();
DataColumn dc;
dt.Columns.Add("Id", typeof(int));
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Section";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
DataRow dr;
dr = dt.NewRow();
dr["Question"] = "AA";
dr["Id"] = 1;
dr["Section"] = "Category1";
dr["User1"] = "6";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "BB";
dr["Id"] = 2;
dr["Section"] = "Category1";
dr["User1"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "XX";
dr["Id"] = 3;
dr["Section"] = "Category2";
dr["User1"] = "3";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "YY";
dr["Id"] = 4;
dr["Section"] = "Category2";
dr["User1"] = "4";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "ZZ";
dr["Id"] = 5;
dr["Section"] = "Category2";
dr["User1"] = "2";
dt.Rows.Add(dr);
Series Series = new Series("Series1");
Chart4.Series.Add("Series1");
Chart4.ChartAreas.Add("ChartArea1");
Chart4.Series["Series1"].ChartType = SeriesChartType.Bar;
Chart4.Series["Series1"]["BarLabelStyle"] = "Center";
Chart4.Series["Series1"]["DrawingStyle"] = "Cylinder";
Chart4.Series["Series1"].XValueMember = "Question";
Chart4.Series["Series1"].YValueMembers = "User1";
Chart4.DataSource = dt;
Chart4.DataBind();
foreach (var g in dt.AsEnumerable().GroupBy(x => x.Field<string>("Section")))
{
string section = g.Key;
var questions = g.Select(r => new { Id = r.Field<int>("Id"), Question = r.Field<string>("Question") });
int min = questions.Min(y => y.Id);
int max = questions.Max(y => y.Id);
var sectionlabel=new CustomLabel();
if (min == max)
{
sectionlabel = new CustomLabel(min - 0.5, max + 0.5, section, 1, LabelMarkStyle.LineSideMark);
}
else
{
sectionlabel = new CustomLabel(min, max, section, 1, LabelMarkStyle.LineSideMark);
}
Chart4.ChartAreas[0].AxisX.CustomLabels.Add(sectionlabel);
}
Chart4.ChartAreas["ChartArea1"].AxisX.Interval = 1;
答案 0 :(得分:2)
Chart4.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(1, 3, "Category 1", 1, LabelMarkStyle.LineSideMark);
答案 1 :(得分:1)
最简单的方法是在图表中添加注释
TextAnnotation text = new TextAnnotation();
text.Text = 'Category 1';
text.X = 20;
text.Y = 20;
Chart4.Annotations.Add(text);
您还可以使用LineAnnotation
绘制线条