0

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

 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

希望有人知道这应该如何完成。但请不要拖放和单击解决方案,而是代码。谢谢

4

1 回答 1

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;
    }

在此处输入图像描述

于 2013-03-11T13:30:18.317 回答