Search code examples
c#asp.netasp.net-charts

ASP.NET Chart with DataBindCrossTable


This is the first time I'm trying to use the ASP.NET Chart control. I always used other third party charts.

This time, I'm creating a web form user control that uses the Chart control.

Let me start with my data. This is the data I have, that is being returned from a database function: enter image description here

I want my chart control to display data for October, November, and December. As you can see, November has 3 records - that is because the 'status' differs. So, I expect my chart control to have the following bars:

- 1 October X value with 1 bar 'lost' with Y count 2

- 1 November X value with 3 bars 'lost, stolen, found' with 1 Y count each.

- 1 December X Value with 1 bar 'stolen' with Y count 1

I'm getting a datatable with the above screenshot structure when the control loads, and then bind the chart like this:

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = _stolenFunctions.GetDashboardChartDataTable(DateTime.Now.Year);

            StolenControlBarChart.DataBindCrossTable(dt.AsEnumerable(), "Status", "Month", "Count", "Label=Status");
        }

ASP.NET code:

    <asp:Chart ID="StolenControlBarChart" CssClass="stolenControlBarChart" Width="500" runat="server">
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
        </ChartAreas>
    </asp:Chart>

But for some reason, the data the chart displays looks like this: enter image description here

Can anyone please help me in the right direction here? :)


Solution

  • Not sure this is going to be much help, but what does the graph look like if you add the other statuses that aren't showing with 0 counts. Does that make the graph look correct? I'm just wondering if each month is expecting 3 results and something is getting skewed from only receiving 1 result in the first month. Not that it's a solution but it might give you enough information to move forward.

    In other words, try adding rows to the database to get this result back

    MONTH    Status    Count
    October  Lost      2
    October  Stolen    0
    October  Found     0
    November Stolen    1
    November Lost      1
    November Found     1
    December Stolen    1
    December Lost      0
    December Found     0
    

    Does that make the chart properly?