我使用VB.Net在ASP.Net中使用MS Chart Control。图表类型是Pie。我面临一个奇怪的问题,图例中显示的项目数量是系列数量的平方。这意味着如果我在饼图中添加了2个系列,则图例中将显示4个项目,如果我在饼图中添加了7个系列,则图例中将显示49个项目。第一组7项显示正确的数据,其他项只显示0。
以下是ASPX中我的图表控件的标记 -
<asp:Chart runat="server" ID="chartX" CssClass="chart" Width="420px" Height="500px" ImageType="Jpeg">
<Series></Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Docking="Bottom" Alignment="Center" Font="Calibri"></asp:Legend>
</Legends>
</asp:Chart>
以下是在ASPX.vb中填充图表控件的代码 -
Dim table As DataTable = PopulateData()
Dim dv As DataView = New DataView(table, "Count > 0", "", DataViewRowState.OriginalRows)
For Each r As DataRow In dv.ToTable().Rows
Dim series As String = r("Name").ToString()
chartX.Series.Add(series)
chartX.Series(series).ChartType = DataVisualization.Charting.SeriesChartType.Pie
chartX.Series(series).XValueMember = "Name"
chartX.Series(series).YValueMembers = "Count"
chartX.ChartAreas(0).Area3DStyle.Enable3D = True
chartX.Series(series).Label = "#VALX" & Environment.NewLine & "(#PERCENT)"
chartX.Series(series)("PieLabelStyle") = "Disabled"
Next
chartX.DataSource = dv
chartX.DataBind()
希望得到任何答案。
答案 0 :(得分:1)
如果我没弄错的话,你会为你表中的每一行添加一个新系列。相反,您应该为表格添加一次系列,然后绑定数据。
设置图表时,无需遍历所有行。只有绑定到表后才能执行此操作。我从不接触VB,但看起来应该是这样的
Dim table As DataTable = PopulateData()
Dim dv As DataView = New DataView(table, "Count > 0", "", DataViewRowState.OriginalRows)
' setup just once
Dim series As String = "Series Name"
chartX.Series.Add(series)
chartX.Series(series).ChartType = DataVisualization.Charting.SeriesChartType.Pie
chartX.Series(series).XValueMember = "Name"
chartX.Series(series).YValueMembers = "Count"
chartX.ChartAreas(0).Area3DStyle.Enable3D = True
chartX.Series(series).Label = "#VALX" & Environment.NewLine & "(#PERCENT)"
chartX.Series(series)("PieLabelStyle") = "Disabled"
' bind to the entire set
chartX.DataSource = dv
chartX.DataBind()