简体   繁体   中英

How to count data from a column in database which will have 3 different values and display it in Graph?

Currently I able to generate a graph type column for only 1 series which is unpaid.

In mySQL, I have column name status which always be any of this three values Unpaid/Paid/Finish.

How do I count the number of customer with each status (Unpaid/Paid/Finish) and add it to the graph?

Default.aspx

    <asp:Chart ID="Chart1" runat="server" Palette="EarthTones">
            <Legends>
            <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default" LegendStyle="Row" />
            </Legends>
            <series>
                <asp:Series Name="Unpaid" XValueMember="MONTH(paymentDate)" YValueMembers="COUNT(status)"  ShadowColor="#FF9900"></asp:Series>
            </series>
            <chartareas>
                <asp:ChartArea Name="ChartArea1">
                </asp:ChartArea>
            </chartareas>
        </asp:Chart>

Default.aspx.cs

    protected void displayGraph()
{
    string Up = "Unpaid";
    string month = DateTime.Now.ToString("MMMM");

    MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem");
    con.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT COUNT(status),MONTH(paymentDate) FROM monthlytracker WHERE status='" + Up + "'", con);

    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    DataTable dt = new DataTable(); 


    da.Fill(dt);

    Chart1.DataSource = dt;
    Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Payment Status";
    Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Amount";
    Chart1.DataBind(); 
}

Could someone help me out please ?

Thank you in advanced. Help is much appreciated.

Replace COUNT with SUM , like this:

SELECT
    SUM(CASE WHEN status='unpaid' THEN 1 ELSE 0 END) as Unpaid
,   SUM(CASE WHEN status='paid' THEN 1 ELSE 0 END) as Paid
,   SUM(CASE WHEN status='finished' THEN 1 ELSE 0 END) as Finished
FROM monthlytracker
WHERE ... -- the filter condition

This will produce three columns with counts for each payment status.

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