如何在ASP.NET图表的同一轴上显示2组数据

时间:2017-02-20 06:59:20

标签: c# asp.net .net sql-server charts

我想在图表的同一轴上显示2组数据。在X轴上,我将显示UserID,在Y轴上显示用户检查的所有纸张的平均标记。但我还想显示为每个用户导出平均值的小册子数量。怎么去呢?我希望图表看起来像enter image description here

除了平均值之外的数字是特定用户检查的纸张数量。

直到现在我有了这个:

     private void Bindchart()
    {

        string msg = string.Empty;
        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand("select Teacher_code, sum(Total_marks)/ count(*) as avgmarks , COUNT(*) as no_of_copies from " + connection.Database + "_transctn where sub_code='" + DropDown_Subjects.SelectedValue + "' and Teacher_code!='' group by Teacher_code", connection);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            DataTable ChartData = ds.Tables[0];

            //storing total rows count to loop on each Record  
            string[] XPointMember = new string[ChartData.Rows.Count];


            decimal[] YPointMember = new decimal[ChartData.Rows.Count];

            int totalrows = ChartData.Rows.Count;

            if (totalrows > 0)
            {
                for (int count = 0; count < ChartData.Rows.Count; count++)
                {
                    //storing Values for X axis  
                    XPointMember[count] = ChartData.Rows[count]["Teacher_code"].ToString();
                    //storing values for Y Axis  
                    YPointMember[count] = Convert.ToDecimal(ChartData.Rows[count]["avgmarks"]);




                }
                //binding chart control  
                Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);

                //Setting width of line  
                Chart1.Series[0].BorderWidth = 5;
                //setting Chart type   
                Chart1.Series[0].ChartType = SeriesChartType.Column;
                //Chart1.Series[0].ChartType = SeriesChartType.StackedColumn;  

                //Hide or show chart back GridLines  
                Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = true;
                Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = true;

                //Enabled 3D  
                //Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;  
                connection.Close();
            } 
        }

1 个答案:

答案 0 :(得分:1)

试试这个:

    protected void Page_Load(object sender, EventArgs e)
    {
        connection.Open();

        SqlCommand cmd = new SqlCommand("Your SQL here.", connection);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        DataTable ChartData = ds.Tables[0];

        Chart1.Series[0].Points.DataBind(ChartData.DefaultView, "Teacher_code", "avgmarks", "");

        for (int i = 0; i < Chart1.Series[0].Points.Count; i++)
            Chart1.Series[0].Points[i].Label = string.Format("{0:0.00} ({1})", ChartData.Rows[i]["avgmarks"], ChartData.Rows[i]["no_of_copies"]);

        connection.Close();
    }

enter image description here