ASP 图表、年、月、日向下钻取
Posted
技术标签:
【中文标题】ASP 图表、年、月、日向下钻取【英文标题】:ASP Chart, Year, Month, Day drill down 【发布时间】:2013-10-04 11:06:30 【问题描述】:我正在尝试在 ASP.NET(C#) 中制作一个向下钻取图表(堆栈列),当我单击一个列时,它会按年份(2011 年、2012 年、2013 年等)显示总收入2012 年,图表显示了该年每个月的总收入。每个月都一样。
所以这就是想法,但我不知道如何制作这样的东西,我已经搜索了几天找到一些向下钻取的示例,但对我没有任何帮助,有一些示例但不适用于日期向下钻取。
这是我已经得到的,但还有多远?
数据库表定义: 收入数字:int 日期:日期
(从 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>
【问题讨论】:
您到底遇到了什么问题?注册用户点击? SQL查询? ... 您好 Walther,我不知道如何编写“点击”事件以在 ASP.NET 图表上从年>月>日向下钻取总收入,这是我的麻烦。 你有没有考虑看官方文档?也许这可以帮助你msdn.microsoft.com/en-us/library/… 【参考方案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);
【讨论】:
【参考方案2】:在您的系列中循环点数集合并设置 URL:
foreach (DataPoint p in Chart1.Series[0].Points)
p.Url = string.Format("details.aspx?id=0", p.XValue);
这应该会让你摆脱困境。
【讨论】:
以上是关于ASP 图表、年、月、日向下钻取的主要内容,如果未能解决你的问题,请参考以下文章