简体   繁体   中英

Help With ASP.NET C# chart control

I have the following dataset:

NAME | GP   | ORD_GP | EXP   | TOTAL GP | TARGET
a      206     48      -239     15         1600
b      0       27       0        27        1520

I am trying to display this in a chart using the following code, all I want to display is the NAME TOTAL_GP and the TARGET :

    Chart1.Width=600;
    Chart1.BackColor = Color.Gray;
    Chart1.BackSecondaryColor = Color.WhiteSmoke;
    Chart1.BackGradientStyle = GradientStyle.DiagonalRight;
    Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
    Chart1.BorderlineColor = Color.Gray;
    Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;

    // format the chart area
    Chart1.ChartAreas[0].BackColor = Color.Wheat;
    // add and format the title
    Chart1.Titles.Add("ASP.NET Chart");
    Chart1.Titles[0].Font = new Font("Utopia", 16);

    // clear the chart series and bind to the table
    Chart1.DataSource = ds.Tables[0];
    Chart1.Series[0].XValueMember = "NAME";
    Chart1.Series[0].YValueMembers = "TOTAL_GP";

    Chart1.Series[0].ChartType = SeriesChartType.StackedBar;

But I keep getting an error. Please can you advise on what I might be doing wrong. The error is as follows

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. 
Must be non-negative and less than the size of the collection.
parameter name: index

UPDATE

I have the following in my .aspx file

 <asp:Chart ID="Chart1" runat="server">
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1" />
        </ChartAreas>
    </asp:Chart>

solved it, had to change the .aspx file to

<asp:Chart ID="Chart1" runat="server">
    <Series>
        <asp:Series Name="Series1">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM