将数据点动态添加到图表时出现问题。没有价值出现

时间:2011-04-29 23:58:57

标签: c# asp.net charts code-behind datapoint

我正在尝试学习asp.net中的图表控件,但我遇到了一些问题。

我想做的就是制作一个简单的柱形图。每列都应该有一个名称。我想在代码隐藏中操纵数据库中的数据,并在图表中添加一个列,并在该列上添加一个名称。

我正在审核的示例将它们添加到.ascx文件中。在代码隐藏中做同样的事情应该是直截了当的,但不知何故它不起作用。我正在看的例子是:

<asp:Chart ID="chtNBAChampionships" runat="server">
<Series>
    <asp:Series Name="Championships" YValueType="Int32" Palette="Berry"     ChartType="Column"
        ChartArea="MainChartArea" IsValueShownAsLabel="true">
        <Points>                
            <asp:DataPoint AxisLabel="Celtics" YValues="0" />
            <asp:DataPoint AxisLabel="Lakers" YValues=" />
            <asp:DataPoint AxisLabel="Bulls" YValues="6" />
            <asp:DataPoint AxisLabel="Spurs" YValues="4" />
            <asp:DataPoint AxisLabel="76ers" YValues="3" />
            <asp:DataPoint AxisLabel="Pistons" YValues="3" />
            <asp:DataPoint AxisLabel="Warriors" YValues="3" />
            <asp:DataPoint AxisLabel="Mara" YValues="4" />
            <asp:DataPoint AxisLabel="Saza" YValues="9" />
            <asp:DataPoint AxisLabel="Buha" YValues="6" />
        </Points>
    </asp:Series>
</Series>
<ChartAreas>
    <asp:ChartArea Name="MainChartArea">
    </asp:ChartArea>
</ChartAreas>

我尝试在代码隐藏中添加一个数据点,如下所示:

    DataPoint dp = new DataPoint();
    dp.AxisLabel = "Test";
    dp.YValues = new double[18];

    this.chtNBAChampionships.Series["Championship"].Points.Add(dp);

但这只会让我在图表中显示0。有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:0)

这是我正在使用的代码段:

        dp = new DataPoint(i++, value);
        dp.AxisLabel = axisName;
        dp.ToolTip = axisName;
        dp.SetValueY(value);
        dp.IsValueShownAsLabel = true;
        s1.Points.Add(dp);
        dp.XValue = s1.Points.Count;

其中s1是Series对象。也许你需要指定X asis的值......

相关问题