2011-07-25 153 views
0

我正在使用MSChart控件來繪製一些數據,但我想在同一個圖表上繪製兩個不同的圖形 。例如,使用條形圖(系列1)和餅圖(系列2)在同一個圖表上繪製兩個不同的圖形

我試圖通過使用以下代碼使用單個圖表來顯示兩個不同的圖表 它顯示數據正確,但是一個問題是我想要以顯示兩個圖(餅圖,柱狀圖),但它顯示

能否評價一下這個

注意任何一個幫助(「兩個餅圖」):我用一個圖表所有這些操作

private void kpicartmouse_click(object sender, MouseEventArgs e) 
    { 
    Series acceptseries; 
    Series membershiptypeseries = null; 
     Title title; 
    string area; 

    try 
    { 
    var pos = e.Location; 
    var results = kpiChartControl.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint); 
    foreach (var result in results) 
    { 
     if (result.ChartElementType == ChartElementType.DataPoint) 
     { 


     DataTable visits = null; 
     visits = KPIData.Visits(dtStartDate.Value,dtEndDate.Value, mf); 

     DataTable accepts = null; 
     accepts = KPIData.AcceptedvisitsByMembership(mf); 

     area = "subchart"; 
     kpiChartControl.ChartAreas.Add(area); 
     acceptseries = kpiChartControl.Series.Add(area); 
     acceptseries.ChartArea = area; 

     title = kpiChartControl.Titles.Add("Accepted Visits By MemberShip Type"); 
     title.DockedToChartArea = area; 
     title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold); 
     title.Alignment = ContentAlignment.TopLeft; 
     kpiChartControl.Titles.Add("").DockedToChartArea = area; 
     kpiChartControl.Titles.Add("Accepted visits by memebrship types").DockedToChartArea = area; 



     foreach (Title titles in kpiChartControl.Titles) 
     { 
      titles.IsDockedInsideChartArea = false; 
     } 

     foreach (ChartArea chartArea in kpiChartControl.ChartAreas) 
     { 
      chartArea.Area3DStyle.Enable3D = true; 
      chartArea.Area3DStyle.Inclination = 45; 
      //chartArea.AxisX.LabelStyle.IsEndLabelVisible = !overview; 
     } 

     foreach (Series chartSeries in kpiChartControl.Series) 
     { 
      chartSeries.ChartType = SeriesChartType.StackedColumn; 
      chartSeries["ColumnDrawingStyle"] = "SoftEdge"; 
      chartSeries["LabelStyle"] = "Top"; 
      chartSeries.IsValueShownAsLabel = true; 
      chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft; 
     } 
     foreach (Series chartSeries in kpiChartControl.Series) 
     { 
      chartSeries.ChartType = SeriesChartType.Pie; 

      chartSeries["PieLabelStyle"] = "Outside"; 
      chartSeries["DoughnutRadius"] = "30"; 
      chartSeries["PieDrawingStyle"] = "SoftEdge"; 

      chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft; 
     } 





     foreach (Legend legend in kpiChartControl.Legends) 
     { 
      legend.Enabled = false; 
     } 

     if (accepts == null) 
     { 
      acceptseries.Points.Clear(); 
      acceptseries.Points.AddXY("no live", 0); 
     } 
     if (visits == null) 
     { 
      membershiptypeseries.Points.Clear(); 
      membershiptypeseries.Points.AddXY("no membershiptypes", 0); 


     } 

     kpiChartControl.Series[0].Points.DataBindXY(accepts.Rows, "mshipname", accepts.Rows, "Value"); 
     kpiChartControl.Series["subchart"].Points.DataBindXY(visits.Rows, "Status", visits.Rows, "Visits"); 



     foreach (Series chartSeries in kpiChartControl.Series) 
     { 
      foreach (DataPoint point in chartSeries.Points) 
      { 

      switch (point.AxisLabel) 
      { 
       case "Accepted": point.Color = Color.Green; break; 
       case "Refused": point.Color = Color.Red; break; 

      } 
      point.Label = string.Format("{0:0}", point.YValues[0]); 
      } 

     } 

     foreach (Series chartSeries in kpiChartControl.Series) 
     { 
      foreach (DataPoint point in chartSeries.Points) 
      { 

      switch (point.AxisLabel) 
      { 
       case "Silver membership": point.Color = Color.Green; break; 
       case "Gold Membership": point.Color = Color.Blue; break; 
       //case "Refused": point.Color = Color.Red; break; 
       case "Weekend Peak": point.Color = Color.Cyan; break; 
       case "prspect": point.Color = Color.Indigo; break; 


      } 
      point.Label = string.Format("{0:0}- {1}", point.YValues[0], point.AxisLabel); 
      } 

     } 





     } 
    } 
    } 
    catch 
    { 
    } 


} 
+1

爲什麼你給-1說原因,否則不downvote – user682417

+0

是我的問題是不清楚..... – user682417

+0

可以任何一個請幫我... – user682417

回答

2

只是猜...贏了這段代碼是否強制所有圖表成爲餅圖?

foreach (Series chartSeries in kpiChartControl.Series) 
    { 
     chartSeries.ChartType = SeriesChartType.Pie; 

     chartSeries["PieLabelStyle"] = "Outside"; 
     chartSeries["DoughnutRadius"] = "30"; 
     chartSeries["PieDrawingStyle"] = "SoftEdge"; 

     chartSeries.BackGradientStyle = GradientStyle.DiagonalLeft; 
    } 

如果這樣沒有幫助,然後嘗試打破你的代碼分解成更小的方法,然後評論每一位出反過來 - 那麼你就可以瞭解每一位做什麼。

+0

可能是.....但如果我刪除上述塊所有圖將條形圖沒有餅圖這是我不想要的..我需要兩個不同的圖表(PIE圖,條形圖)...... – user682417

+0

你有什麼想法... ..... – user682417

+0

你知道一個'foreach'循環是什麼,它做了什麼? – Stuart

相關問題