我有一个如下所示的数据表:
Data Value 1 Value 2 Value 3
series1 32 -2 46
series2 -62 99
series3 19 23 98
在图表上,我需要它看起来像这样:
32 -2 46 -62 99 19 23 98
series1 series2 series3
和图例:值 1,值 2,值 3
我尝试过的代码:
private void LoadChartCurrencyTotal(DataTable initialDataSource)
{
DataTable pivotedDt = Pivot(initialDataSource);
chart1.DataSource = pivotedDt;
foreach (DataRow dr in pivotedDt.Rows)
{
Series series = new Series(dr["Data"].ToString());
List<string> colNames = (from DataColumn col in pivotedDt.Columns where col.ColumnName != "Data" select col.ColumnName).ToList();
series.XValueMember = "Data";
series.YValueMembers = string.Join(",", colNames);
chart1.Series.Add(series);
}
chart1.DataBind();
FormatChart(chart1);
}
Data points insertion error. Only 1 Y values can be set for this data series.
由于连接的列名,这会返回。
也试过:
private void LoadChartCurrencyTotal(DataTable initialDataSource)
{
DataTable pivotedDt = Pivot(initialDataSource);
foreach (DataRow pivotDr in pivotedDt.Rows)
{
Series serie = new Series(pivotDr["Data"].ToString());
List<decimal?> colValues = new List<decimal?>();
foreach (DataColumn col in pivotedDt.Columns)
{
if (col.ColumnName != "Data")
{
//colValues.Add(pivotDr[col.ColumnName] != DBNull.Value
// ? decimal.Parse(pivotDr[col.ColumnName].ToString())
// : new decimal?());
decimal? colValue = pivotDr[col.ColumnName] != DBNull.Value
? decimal.Parse(pivotDr[col.ColumnName].ToString())
: new decimal?();
serie.Points.AddXY(pivotDr["Data"], colValue);
}
}
//serie.Points.AddXY(pivotDr["Data"], string.Join(",", colValues));
chart1.Series.Add(serie);
}
FormatChart(chart1);
}
这编译但结果完全搞砸了:传说sais series1,series2,series3,结果是:
32 -62 19 -2 23 46 99 98
series1 series2 series3
我得到每列而不是每行的结果。
我最后一次尝试的是:
DataView pivotedDv = pivotedDt.AsDataView();
chart1.DataBindTable(pivotedDv, pivotedDt.Columns[0].ColumnName);
但这只会返回:
0.00 0.00 0.00
series1 series2 series3
和传说:0.00
希望有人知道这应该如何完成。但请不要拖放和单击解决方案,而是代码。谢谢