Question

I'm trying to make a drill down chart(stack column) in ASP.NET(C#) which presents a total revenue by year(2011, 2012, 2013 etc.), when I click a column let say the year 2012, the chart shows the total revenue from each month from that year. This the same for month to day.

So this is the idea, but I have no clue how to make something like this, I've searched days finding some drill down examples but nothing worked for me, there are some examples but not with date drill down.

This is what I got already, but how further?

Database Table definition:
RevenueNumbers: int
Date: Date

(Create and fill chart from SQL server)

public SqlConnection conn;

    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString);

        string query = "SELECT SUM(RevenueNumber) AS TotalRevenue, DATENAME(YYYY, Date) AS Year FROM test GROUP BY DATENAME(YYYY, Date)";
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open();

        Chart1.DataSource = cmd.ExecuteReader();
        Series Series1 = new Series();
        Series1.XValueMember = "Year";
        Series1.YValueMembers = "TotalRevenue";

        Chart1.Series.Add(Series1);
        Chart1.Series[0].PostBackValue = "#XVAL";
        Chart1.Series[0].ChartType = SeriesChartType.Column;
        Chart1.DataBind();

    }

(HTML)

<div>  
    <asp:Chart ID="Chart1" runat="server">
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>
</div>
Was it helpful?

Solution 2

Loop through the Points collection on your Series and set the Url:

foreach (DataPoint p in Chart1.Series[0].Points)
{
    p.Url = string.Format("details.aspx?id={0}", p.XValue);
}

That should get you squared away.

OTHER TIPS

Made some code, it's working but a bit messy

        protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString);
        string query = "";
        string Year = "";
        string Month = "";
        if (Request["Year"] != null) Year = Request["Year"].ToString();
        if (Request["Month"] != null) Month = Request["Month"].ToString();
        if ((Year == "") && (Month == ""))
        {
            query = "SELECT SUM(TotalRevenue) AS Total, YEAR(Date) AS Year FROM test GROUP BY YEAR(Date)";
        }
        else if (Month == "")
        {
            query = "SELECT SUM(TotalRevenue) AS Total, MONTH(Date) AS year FROM test WHERE YEAR(Date) = " + Year + "  GROUP BY MONTH(Date)";
        }
        else
        {
            query = "SELECT SUM(TotalRevenue) AS Total, DAY(Date) AS Year FROM test WHERE YEAR(Date) = " + Year + " AND MONTH(Date) = " + Month + "  GROUP BY DAY(Date)";
        }

        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open();

        Chart1.DataSource = cmd.ExecuteReader();
        Series Series1 = new Series();
        Series1.XValueMember = "Year";
        Series1.YValueMembers = "Total";

        Chart1.Series.Add(Series1);

        Chart1.Series[0].ChartType = SeriesChartType.Column;
        Chart1.DataBind();

        foreach (DataPoint p in Chart1.Series[0].Points)
        {
            if ((Year != "") && (Month != ""))
            {
                p.Url = string.Format("Default.aspx");
            }
            else if (Year != "")
            {
                p.Url = string.Format("Default.aspx?Year={0}&Month={1}", Year, p.XValue);
            }
            else
            {
                p.Url = string.Format("Default.aspx?Year={0}", p.XValue);
            }
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top