0

我正在尝试在 ASP.NET(C#) 中制作一个向下钻取图表(堆栈列),当我单击一个列时(假设是 2012 年),按年份(2011 年、2012 年、2013 年等)显示总收入,图表显示了该年每个月的总收入。这同样适用于每个月。

所以这就是想法,但我不知道如何制作这样的东西,我已经搜索了几天找到一些向下钻取的例子,但对我没有任何帮助,有一些例子但没有日期向下钻取。

这是我已经得到的,但还有多远?

数据库表定义:
RevenueNumbers:int
Date:Date

(从 SQL 服务器创建和填充图表)

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>
4

2 回答 2

1

做了一些代码,它可以工作但有点乱

        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);
            }
        }

    }
于 2013-10-07T07:39:55.263 回答
0

遍历您的系列上的点集合并设置 URL:

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

那应该让你摆脱困境。

于 2013-10-04T12:22:12.093 回答