2017-05-30 591 views
1

我試圖用列值的名稱來標記我的餅圖(在這種情況下,這將是機器名稱和它旁邊的百分比),這是相當困難的。默認情況下,在C#中,我從MySQL數據庫繪製它基於列「機器」上採集數據,而下面是我目前擁有的代碼,這將只顯示百分比:在c#中的餅圖標籤的文字和百分比

string DatabaseCountMachines = "SELECT Machine, COUNT(*) total FROM completed GROUP BY Machine"; 

MySqlConnection conDataBase = new MySqlConnection(constring); 
MySqlCommand cmdDataBase = new MySqlCommand(DatabaseCountMachines, conDataBase); 
MySqlDataReader StatsbyMachine; 

try 
{ 
    conDataBase.Open(); 
    StatsbyMachine = cmdDataBase.ExecuteReader(); 

    while (StatsbyMachine.Read()) 
    { 
     this.chartMachine.Series["Series1"].Points.AddXY(StatsbyMachine.GetString("Machine"), StatsbyMachine.GetString("total")); 
     this.chartMachine.Series["Series1"].CustomProperties = "PieLabelStyle=Outside"; 
     this.chartMachine.Series["Series1"].Label = "#LEGENDTEXT" + "#PERCENT";  
    } 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 
+0

在餅圖中,x值不適用。 (你可以使用'AddY')你可以顯示你想要的圖像嗎? – TaW

回答

1

餡餅圖x-values不適用所以他們不顯示...你可能還不如使用AddY

只需將兩個數據添加到每個DataPointLabel

變化

chartMachine.Series["Series1"].Points 
      .AddXY(StatsbyMachine.GetString("Machine"), StatsbyMachine.GetString("total")); 

int index = chartMachine.Series["Series1"].Points.AddY(StatsbyMachine.GetString("total")); 
chartMachine.Series["Series1"].Points[index].Label = 
     StatsbyMachine.GetString("Machine") + " : " + StatsbyMachine.GetString("total"); 

請注意,雖然字符串添加爲y值將被轉換爲數字,這將不適用於x值,即使您使用使用x值的圖表類型。

某些圖表數據不需要數字x值,如姓名,城市甚至ID,因此不會自動轉換,即使在可能的情況下也是如此。

Y值otoh總是需要數字,否則圖表的整個想法是沒有意義的。因此,如果可能的話,您的string total將被轉換爲數字。如果轉換失敗,值將爲0 ..

+1

我得到一個錯誤,說這個代碼不能隱式地將類型字符串轉換爲System.Windows.Forms.DataVisualization.Charting.DataPoint。該鏈接顯示了我想要做的一個例子:http://www.fusioncharts.com/dev/chart-guide/pie-and-doughnut-charts/configuring-pie-and-doughnut-charts.html – LitteringAnd

+0

Ouch,我已經省去了'Label'部分!,更正..對不起。 – TaW

+1

我將'StatsbyMachine.GetString(「total」)'更改爲'#PERCENT'。完美的作品。做得好。 – LitteringAnd