.NET图表控件中的多系列条形图

时间:2013-03-21 17:10:27

标签: c# asp.net .net

我正在尝试基于“名称”的多系列条形图,但它正在抛出错误

  

图表区域包含不兼容的图表类型。例如,条形图和柱形图不能存在于同一图表区域中。

这是我的代码:

            Chart1.Series["Series1"].ChartType = SeriesChartType.Bar;
            Chart1.Series["Series1"]["DrawingStyle"] = "Emboss";
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
            Chart1.Series["Series1"].IsValueShownAsLabel = true;

            DataTable dt = new DataTable();
            DataColumn dc;

            dc = new DataColumn();
            dc.ColumnName = "Name";
            dt.Columns.Add(dc);

            dc = new DataColumn();
            dc.ColumnName = "Question";
            dt.Columns.Add(dc);

            dc = new DataColumn();
            dc.ColumnName = "Marks";
            dt.Columns.Add(dc);

            DataRow dr;
            dr = dt.NewRow();
            dr["Name"] = "Fred";
            dr["Question"] = "2D";
            dr["Marks"] = "54";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Name"] = "Bill";
            dr["Question"] = "3D";
            dr["Marks"] = "66";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Name"] = "Rhona";
            dr["Question"] = "4D";
            dr["Marks"] = "32";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Name"] = "Peter";
            dr["Question"] = "5D";
            dr["Marks"] = "46";
            dt.Rows.Add(dr);

            var IEtable = (dt as System.ComponentModel.IListSource).GetList();

            Chart1.DataBindTable(IEtable, "Name");

和Mark up is,

<asp:Chart ID="Chart1" runat="server" Width="850px" Height="500px" >

                            <Series>
                                    <asp:series Name="Series1" ChartArea="ChartArea1"></asp:series>
                            </Series>
                            <ChartAreas>
                                    <asp:ChartArea Name="ChartArea1">
                                    </asp:ChartArea>
                            </ChartAreas>
                    </asp:Chart>

我正在寻找像

这样的输出

enter image description here

其中颜色条表示“名称”

1 个答案:

答案 0 :(得分:3)

这绝对是可能的,您将能够生成如下所示的图表: enter image description here

以下代码可以满足您的需求:

<强> ASPX

        <asp:Chart ID="Chart1" runat="server" Palette="SeaGreen" BackColor="LightGray" Width="654px">
            <Series>

            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1" AlignmentOrientation="Horizontal">
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>

<强> C#

        //Build Sample Data DataTable
        DataTable dt = new DataTable();
        DataColumn dc;

        dc = new DataColumn();
        dc.ColumnName = "Name";
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "Question";
        dt.Columns.Add(dc);

        dt.Columns.Add("Marks", typeof(int));

        string question = "2D";

        DataRow dr;
        dr = dt.NewRow();
        dr["Name"] = "Fred";
        dr["Question"] = question;
        dr["Marks"] = 54;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Bill";
        dr["Question"] = question;
        dr["Marks"] = 66;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Rhona";
        dr["Question"] = question;
        dr["Marks"] = 32;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Peter";
        dr["Question"] = question;
        dr["Marks"] = 46;
        dt.Rows.Add(dr);

        question = "4D";
        dr = dt.NewRow();
        dr["Name"] = "Fred";
        dr["Question"] = question;
        dr["Marks"] = 89;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Bill";
        dr["Question"] = question;
        dr["Marks"] = 99;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Rhona";
        dr["Question"] = question;
        dr["Marks"] = 28;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Peter";
        dr["Question"] = question;
        dr["Marks"] = 44;
        dt.Rows.Add(dr);

        question = "3D";
        dr = dt.NewRow();
        dr["Name"] = "Fred";
        dr["Question"] = question;
        dr["Marks"] = 26;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Bill";
        dr["Question"] = question;
        dr["Marks"] = 89;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Rhona";
        dr["Question"] = question;
        dr["Marks"] = 73;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Peter";
        dr["Question"] = question;
        dr["Marks"] = 14;
        dt.Rows.Add(dr);

        //Sort the datatable
        DataView dv = dt.DefaultView;
        dv.Sort = "Question ASC, Name ASC";
        dt = dv.ToTable();

        DataTable table = new DataTable();
        table.Columns.Add("Question", typeof(string));
        foreach (DataRow dr2 in dt.Rows)
        {
            //Add user Names to DataTable table
            if (!table.Columns.Contains(dr2["Name"].ToString())) {
                table.Columns.Add(dr2["Name"].ToString(), typeof(int));
            }

            //Add empty Question rows to DataTable
            if (table.AsEnumerable().Where(x => x.Field<string>("Question") == dr2["Question"].ToString()).Count() == 0)
            {
                table.Rows.Add(dr2["Question"].ToString());
            }
        }

        // Loop through all columns and questions and then calculate the mark
        for (int i = 1; i < table.Columns.Count;i++ )
        {
            for (int j = 0; j < table.Rows.Count; j++) 
            {
                string questionName = table.Rows[j][0].ToString();
                for (int k = 0; k < dt.Rows.Count; k++) 
                {
                    string userName = table.Columns[i].ColumnName;
                    table.Rows[j][i] = dt.AsEnumerable().Where(x => x.Field<string>("Name") == userName).Where(y=>y.Field<string>("Question") == questionName).Sum(r => r.Field<int>("Marks"));
                }
            }
        }

        Chart1.Series[seriesName].ChartType = SeriesChartType.Bar;

        //convert datatable to a IEnumerable form
        var IEtable = (table as System.ComponentModel.IListSource).GetList();

        Chart1.DataBindTable(IEtable, "Question");