2013-03-11 86 views
0

我有一個數據表,看起來像這樣:Asp.Net圖與多個系列多個值在y軸上

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

這編譯,但結果完全地搞砸了:傳奇的最高審計機關係列1,系列2,系列3,其結果是:

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

希望有人有線索應如何acomplished 。但請不要拖放並點擊解決方案,而是編碼。 謝謝

回答

3

我想我已經得到了這個工作如何使用下面的代碼。希望這有助於:

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

+0

它的工作,謝謝。儘管圖例元素的名稱都是Series:Series1,Series2 ....與圖表底部的名稱無關,這只是我在這裏給他們的一個名稱,在我的應用程序中有不同的名稱。任何想法我怎麼能告訴它傳說應該有列的名稱? – bokkie 2013-03-11 15:16:40

+0

我解決了它:'Series series = new Series(initialDataSource.Columns [i] .ColumnName);' – bokkie 2013-03-11 15:39:45

+0

哦,很高興你得到它的工作 – dave823 2013-03-11 15:40:52