在我的网络应用程序中,我需要显示从数据表绑定(bind)的柱形图数据我在我的图表区域的 X 轴上有 3 列我需要显示 col1 一侧和第二列我需要显示不同的颜色系列显示图例也来自数据表。
我需要像这样我该怎么做请任何人都可以帮助我如何做到这一点。
谢谢
最佳答案
假设您的 DataTable
具有以下列/类型:
Columns.Add("Month", typeof(DateTime));
Columns.Add("Charges", typeof(double));
Columns.Add("Payments", typeof(double));
ASPX:
<asp:Chart ID="Chart1" runat="server" Height="400px" Width="600px">
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<AxisY>
<MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
<LabelStyle Format="C2" />
</AxisY>
<AxisX>
<MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
</AxisX>
</asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Name="Legend1">
</asp:Legend>
</Legends>
</asp:Chart>
CS:
protected void Page_Load(object sender, EventArgs e)
{
foreach (DataRow r in dt.Rows)
{
Series s = new Series(string.Format("{0} {1}", ((DateTime)r["Month"]).ToString("MMM"), ((DateTime)r["Month"]).Year));
s.ChartType = SeriesChartType.Column;
s.Points.AddXY("Charges", new object[] { r["Charges"] });
s.Points.AddXY("Payments", new object[] { r["Payments"] });
Chart1.Series.Add(s);
}
}
编辑:假设您的 DataTable
具有以下列/类型:
Columns.Add("Month", typeof(string));
Columns.Add("Charges", typeof(double));
Columns.Add("Payments", typeof(double));
或
Columns.Add("Month");
Columns.Add("Charges");
Columns.Add("Payments");
那么你应该把代码改成这样:
protected void Page_Load(object sender, EventArgs e)
{
foreach (DataRow r in dt.Rows)
{
Series s = new Series((string)r["Month"]);
s.ChartType = SeriesChartType.Column;
s.Points.AddXY("Charges", new object[] { r["Charges"] });
s.Points.AddXY("Payments", new object[] { r["Payments"] });
Chart1.Series.Add(s);
}
}
关于c# - 如何在 asp.net 中显示多系列柱形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40022376/