将存储过程中的数据添加到图表上

时间:2014-06-20 14:32:47

标签: c# asp.net sql-server

我正在尝试绘制一个aspx折线图。我有一个存储过程的数据,我想在图表上绘制。

正如您在下面看到的,我从存储过程中获取数据,该数据称为" ConsoleSales,"我将这些数据放入数据集中。我还为图表的X轴创建了一个名为months的类。我想展示从1月到12月的月份,当然对于我的Y轴,我想显示我的存储过程数据。我怎样才能做到这一点?我无法在谷歌上找到任何有用的建议吗?

这是我的标记代码:

 <asp:Chart ID="Chart1" runat="server" Height="296px" Width="500px" BorderDashStyle="Solid"
        BackSecondaryColor="White" BackGradientStyle="TopBottom" BorderWidth="2px" BackColor="211, 223, 240"
        BorderColor="#1A3B69">
        <Titles>
            <asp:Title Text="Title of the Graph comes here" />
        </Titles>
        <Series>
            <asp:Series Name="Series1" BorderColor="180, 26, 59, 105" ChartType="Line">
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid"
                BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent"
                BackGradientStyle="TopBottom">
                <Area3DStyle Rotation="10" Perspective="10" Inclination="15" IsRightAngleAxes="False"
                    WallWidth="0" IsClustered="False"></Area3DStyle>
                <AxisY LineColor="64, 64, 64, 64">
                    <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
                    <MajorGrid LineColor="64, 64, 64, 64" />
                </AxisY>
                <AxisX LineColor="64, 64, 64, 64">
                    <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
                    <MajorGrid LineColor="64, 64, 64, 64" />
                </AxisX>
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>

这是我的CS代码:

     public class Months
    {
        public string MonthsOfTheYear { get; set; }
    }


protected void Page_Load(object sender, EventArgs e)
    {

        var monthsList = new List<Months>()
        {
            new Months() {MonthsOfTheYear = "Jan"},
            new Months() {MonthsOfTheYear = "Feb"},
            new Months() {MonthsOfTheYear = "March"},
            new Months() {MonthsOfTheYear = "April"},
            new Months() {MonthsOfTheYear = "May"},
            new Months() {MonthsOfTheYear = "Jun"},
            new Months() {MonthsOfTheYear = "Jul"},
            new Months() {MonthsOfTheYear = "Aug"},
            new Months() {MonthsOfTheYear = "Sep"},
            new Months() {MonthsOfTheYear = "Oct"},
            new Months() {MonthsOfTheYear = "Nov"},
            new Months() {MonthsOfTheYear = "Dec"}
        };


        DateTime startDate = DateTime.Now;
        DateTime endDate = DateTime.Now.AddYears(-1);

        string cS = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cS))
        {
            SqlDataAdapter da = new SqlDataAdapter("[ConsoleSales]", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add(new SqlParameter("@StartDate", startDate));
            da.SelectCommand.Parameters.Add(new SqlParameter("@EndDate", endDate));
            DataSet ds = new DataSet();
            da.Fill(ds);


            Chart1.DataSource = ds.Tables[0];

            Chart1.Series["Series1"].YValueMembers = "TimeOfSales";
            Chart1.Series["Series1"].XValueMember = monthsList.ToString() ;

       }
   }

2 个答案:

答案 0 :(得分:3)

请尝试以下方法。

SqlConnection connection = new SqlConnection("......");

SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "<<<Enter your stored Procedure Name>>>";

// create sql parameter if your procedure expects any input
SqlParameter param1 = new SqlParameter("@spParam1",SqlDbType.NVarChar);

// add parameters to parameters collection
command.Parameters.Add(param1);

// you can define more parameters based on your Stored Procedure's design

// set this parameter to a value we would like to set
command.Parameters["@spParam1"].Value = "<<Input goes here...>>"; 

// open connection
command.Connection.Open();

// populate data reader with return data result set 
// and close connection after populating data set
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

// assign data source to Chart
chart1.DataSource = reader;

// Set series data source to stored procedures returned data set's columns
chart1.Series[0].ValueMemberX = "ProductName";
chart1.Series[0].ValueMembersY = "TotalPurchase";

// data bind chart
chart1.DataBind(); 

答案 1 :(得分:0)

有效!

protected void Chart1_Load(object sender, EventArgs e)
{


    SqlCommand cmd = new SqlCommand("sp_chart", Cn); // Definir cmd
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@id_district", SqlDbType.Int);
    cmd.Parameters["@id_district"].Value = 1;

    Cn.Open();

    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    Graficas.DataSource = reader;

    Graficas.Series[0].XValueMember = "name";
    Graficas.Series[0].YValueMembers = "vote";

    Graficas.DataBind();

    reader.Close();
    Cn.Close();


}