Asp.Net图表,y轴上有多个系列和多个值

时间:2013-03-11 11:03:21

标签: c# asp.net charts

我有一个如下所示的数据表:

 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

希望有人知道这应该如何完成。但请,没有拖放和点击解决方案,但代码。 感谢

1 个答案:

答案 0 :(得分:4)

我想我使用以下代码了解你的工作方式。希望这会有所帮助:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = GetTestData();
            LoadChartCurrencyTotal(dt);
        }


    }

    private void LoadChartCurrencyTotal(DataTable initialDataSource)
    {
        for (int i = 1; i < initialDataSource.Columns.Count; i++)
        {
            Series series = new Series();
            foreach (DataRow dr in initialDataSource.Rows)
            {
                int y = (int)dr[i];
                series.Points.AddXY(dr["Data"].ToString(), y);
            }
            Chart1.Series.Add(series);
        }
    }

    private DataTable GetTestData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Data", Type.GetType("System.String"));
        dt.Columns.Add("Value1", Type.GetType("System.Int32"));
        dt.Columns.Add("Value2", Type.GetType("System.Int32"));
        dt.Columns.Add("Value3", Type.GetType("System.Int32"));
        DataRow dr1 = dt.NewRow();
        dr1["Data"] = "series1";
        dr1["Value1"] = 32;
        dr1["Value2"] = -2;
        dr1["Value3"] = 46;
        dt.Rows.Add(dr1);
        DataRow dr2 = dt.NewRow();
        dr2["Data"] = "series2";
        dr2["Value1"] = -62;
        dr2["Value2"] = 0;
        dr2["Value3"] = 99;
        dt.Rows.Add(dr2);
        DataRow dr3 = dt.NewRow();
        dr3["Data"] = "series3";
        dr3["Value1"] = 19;
        dr3["Value2"] = 23;
        dr3["Value3"] = 98;
        dt.Rows.Add(dr3);
        return dt;
    }

enter image description here