我正在尝试在ASP.NET(C#)中制作一个向下钻取图表(堆栈列),它按年度(2011年,2012年,2013年等)显示总收入,当我点击列时让我们说年份2012年,该图表显示了该年度每月的总收入。每月都是如此。
所以这就是这个想法,但我不知道如何制作这样的东西,我搜索了几天找到一些深入的例子,但没有什么对我有用,有一些例子但没有日期深入研究。
这就是我已经得到的,但又如何呢?
数据库表定义:
RevenueNumbers: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>
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:0)
循环播放系列中的Points系列并设置网址:
foreach (DataPoint p in Chart1.Series[0].Points)
{
p.Url = string.Format("details.aspx?id={0}", p.XValue);
}
那应该让你摆平。