gpt4 book ai didi

c# - .NET Chart 控件中的多系列条形图

转载 作者:太空狗 更新时间:2023-10-29 23:32:30 24 4
gpt4 key购买 nike

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

The chart area contains incompatible chart types. For example, bar charts and column charts cannot exist in the same chart area.

这是我的代码:

            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");

标记是,

<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

其中颜色条代表“姓名”

最佳答案

这绝对是可能的,您将能够生成如下所示的图表: 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");

关于c# - .NET Chart 控件中的多系列条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15553875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com