for (int i = 0; i < initialDataSource.Columns.Count; i++)
{
Series series = new Series();
series.IsValueShownAsLabel = true;
series.ChartType = SeriesChartType.Column;
foreach (DataRow dr in initialDataSource.Rows)
{
string y = (string)dr[i];
if (y != "0")
{
series.Points.AddXY(dr["date"].ToString(), y);
}
}
Chart1.Series.Add(series);
}
}
使用此代码我得到的图表是系列图表,图表类型是列(有三种颜色)。现在我想在此图表中添加图例。我怎么能帮助我。
答案 0 :(得分:1)
简答:
- &GT;给你的Series-object一个名字(series.Name =“myColumname”;或者见下面的例子)
- &GT;为图表对象分配一个新的Legend()对象(参见第二个例子)
以示例回答:
使用您的代码作为基础..
for (int i = 0; i < initialDataSource.Columns.Count; i++)
{
Series series = new Series();
series.IsValueShownAsLabel = true;
series.ChartType = SeriesChartType.Column;
series.Name = initialDataSource.Columns[i].ColumnName;
foreach (DataRow dr in initialDataSource.Rows)
{
string y = (string)dr[i];
if (y != "0")
{
series.Points.AddXY(dr["date"].ToString(), y);
}
}
Chart1.Series.Add(series);
}
而且,无论是在数据循环之前还是之后,都无关紧要:
Chart1.Legends.Add(new Legend());
// or, if you want to change the properties of your legend:
// Legend myLegend = new Legend();
// Chart1.Legends.Add(myLegend);