2013-04-05 126 views
0

我想添加自定義標籤類似於(類別1和類別2),但不知道我是否需要添加新系列或使用自定義標籤功能。任何幫助將非常感謝。MSChart:如何在條形圖中添加自定義標籤

enter image description here

這裏是我的數據表,

DataTable dt = new DataTable(); 

    DataColumn dc; 

    dc = new DataColumn(); 
    dc.ColumnName = "Question"; 
    dt.Columns.Add(dc); 

    dc = new DataColumn(); 
    dc.ColumnName = "Section"; 
    dt.Columns.Add(dc); 

    dc = new DataColumn(); 
    dc.ColumnName = "User1"; 
    dt.Columns.Add(dc); 

    dc = new DataColumn(); 
    dc.ColumnName = "User2"; 
    dt.Columns.Add(dc); 


    DataRow dr; 
    dr = dt.NewRow(); 

    dr["Question"] = "A"; 
    dr["Section"] = "Category 1"; 
    dr["User1"] = "1"; 
    dr["User2"] = "2"; 

    dt.Rows.Add(dr); 

    dr = dt.NewRow(); 

    dr["Question"] = "B"; 
    dr["Section"] = "Category 1"; 
    dr["User1"] = "4"; 
    dr["User2"] = "5"; 


    dt.Rows.Add(dr); 

    dr = dt.NewRow(); 

    dr["Question"] = "C"; 
    dr["Section"] = "Category 1"; 
    dr["User1"] = "2"; 
    dr["User2"] = "5"; 

    dt.Rows.Add(dr); 

    dr = dt.NewRow(); 

    dr["Question"] = "X"; 
    dr["Section"] = "Category 2"; 
    dr["User1"] = "3"; 
    dr["User2"] = "5"; 

    dt.Rows.Add(dr); 

    dr = dt.NewRow(); 

    dr["Question"] = "Y"; 
    dr["Section"] = "Category 2"; 
    dr["User1"] = "5"; 
    dr["User2"] = "1"; 

    dt.Rows.Add(dr); 

    dr = dt.NewRow(); 

    dr["Question"] = "Z"; 
    dr["Section"] = "Category 2"; 
    dr["User1"] = "6"; 
    dr["User2"] = "5"; 


    dt.Rows.Add(dr); 

我的圖表繪製代碼,

Series Series = new Series("Series1"); 
    Chart4.Series.Add("Series1"); 
    Chart4.Series.Add("Series2"); 

    Chart4.ChartAreas.Add("ChartArea1"); 

    // Set series chart type 
    Chart4.Series["Series1"].ChartType = SeriesChartType.Bar; 
    Chart4.Series["Series2"].ChartType = SeriesChartType.Bar; 

    // Draw as 3D Cylinder 
    Chart4.Series["Series1"]["DrawingStyle"] = "Cylinder"; 
    Chart4.Series["Series2"]["DrawingStyle"] = "Cylinder"; 

    Chart4.DataSource = dt.DefaultView; 

    Chart4.ChartAreas["ChartArea1"].AxisX.Interval = 1; 

    Chart4.Series["Series1"].XValueMember = "Question"; 
    Chart4.Series["Series1"].YValueMembers = "User1"; 
    Chart4.Series["Series2"].YValueMembers = "User2"; 

    Chart4.DataBind(); 

解決方案

  DataTable dt = new DataTable(); 

      DataColumn dc; 

      dt.Columns.Add("Id", typeof(int)); 

      dc = new DataColumn(); 
      dc.ColumnName = "Question"; 
      dt.Columns.Add(dc); 

      dc = new DataColumn(); 
      dc.ColumnName = "Section"; 
      dt.Columns.Add(dc); 

      dc = new DataColumn(); 
      dc.ColumnName = "User1"; 
      dt.Columns.Add(dc); 

      DataRow dr; 

      dr = dt.NewRow(); 

      dr["Question"] = "AA"; 
      dr["Id"] = 1; 
      dr["Section"] = "Category1"; 
      dr["User1"] = "6"; 

      dt.Rows.Add(dr); 

      dr = dt.NewRow(); 

      dr["Question"] = "BB"; 
      dr["Id"] = 2; 
      dr["Section"] = "Category1"; 
      dr["User1"] = "1"; 

      dt.Rows.Add(dr); 


      dr = dt.NewRow(); 

      dr["Question"] = "XX"; 
      dr["Id"] = 3; 
      dr["Section"] = "Category2"; 
      dr["User1"] = "3"; 

      dt.Rows.Add(dr); 

      dr = dt.NewRow(); 

      dr["Question"] = "YY"; 
      dr["Id"] = 4; 
      dr["Section"] = "Category2"; 
      dr["User1"] = "4"; 

      dt.Rows.Add(dr); 

      dr = dt.NewRow(); 

      dr["Question"] = "ZZ"; 
      dr["Id"] = 5; 
      dr["Section"] = "Category2"; 
      dr["User1"] = "2"; 

      dt.Rows.Add(dr); 

      Series Series = new Series("Series1"); 
      Chart4.Series.Add("Series1"); 

      Chart4.ChartAreas.Add("ChartArea1"); 

      Chart4.Series["Series1"].ChartType = SeriesChartType.Bar; 

      Chart4.Series["Series1"]["BarLabelStyle"] = "Center"; 

      Chart4.Series["Series1"]["DrawingStyle"] = "Cylinder"; 

      Chart4.Series["Series1"].XValueMember = "Question"; 

      Chart4.Series["Series1"].YValueMembers = "User1"; 

      Chart4.DataSource = dt; 
      Chart4.DataBind(); 

      foreach (var g in dt.AsEnumerable().GroupBy(x => x.Field<string>("Section"))) 
      { 
       string section = g.Key; 
       var questions = g.Select(r => new { Id = r.Field<int>("Id"), Question = r.Field<string>("Question") }); 
       int min = questions.Min(y => y.Id); 
       int max = questions.Max(y => y.Id); 

       var sectionlabel=new CustomLabel(); 
       if (min == max) 
       { 
        sectionlabel = new CustomLabel(min - 0.5, max + 0.5, section, 1, LabelMarkStyle.LineSideMark); 
       } 
       else 
       { 
        sectionlabel = new CustomLabel(min, max, section, 1, LabelMarkStyle.LineSideMark); 
       } 
       Chart4.ChartAreas[0].AxisX.CustomLabels.Add(sectionlabel); 

      } 

      Chart4.ChartAreas["ChartArea1"].AxisX.Interval = 1; 

回答

0

最簡單的方法是隻需添加註釋到圖表

TextAnnotation text = new TextAnnotation(); 
text.Text = 'Category 1'; 
text.X = 20; 
text.Y = 20; 
Chart4.Annotations.Add(text); 

您也可以使用LineAnnotation

http://msdn.microsoft.com/en-us/library/dd456725.aspx

+0

註解在我的情況下可能不起作用,因爲我將有許多類別,並且它必須顯示在圖表上,以提供乾淨的外觀來報告。 – Rishi 2013-04-05 20:55:23

0

您是否嘗試過這個劃清界線呢?它將爲項目1-3創建一個組標籤。

Chart4.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(1, 3, "Category 1", 1, LabelMarkStyle.LineSideMark); 
相關問題