2017-01-09 96 views
0

我使用LiveCharts創建餅圖。我有一張我想在餅圖中表示的雙打名單。問題是,列表的值可以,並且會改變,因此我希望能夠相應地更改圖表。在LiveCharts中創建餅圖切片

下面是從LiveCharts網站一些示例代碼:

using System; 
using System.Windows.Forms; 
using LiveCharts; 
using LiveCharts.Wpf; 

namespace Winforms.PieChart 
{ 
public partial class PieChartExample : Form 
{ 
    public PieChartExample() 
    { 
     InitializeComponent(); 

     Func<ChartPoint, string> labelPoint = chartPoint => 
      string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation); 

     pieChart1.Series = new SeriesCollection 
     { 
      new PieSeries 
      { 
       Title = "Maria", 
       Values = new ChartValues<double> {3}, 
       PushOut = 15, 
       DataLabels = true, 
       LabelPoint = labelPoint 
      }, 
      new PieSeries 
      { 
       Title = "Charles", 
       Values = new ChartValues<double> {4}, 
       DataLabels = true, 
       LabelPoint = labelPoint 
      }, 
      new PieSeries 
      { 
       Title = "Frida", 
       Values = new ChartValues<double> {6}, 
       DataLabels = true, 
       LabelPoint = labelPoint 
      }, 
      new PieSeries 
      { 
       Title = "Frederic", 
       Values = new ChartValues<double> {2}, 
       DataLabels = true, 
       LabelPoint = labelPoint 
      } 
     }; 

     pieChart1.LegendLocation = LegendLocation.Bottom; 
    } 
} 

}

從本質上講,我希望做同樣的事情,而是遍歷列表和創建片,適當數量的爲餅圖。 LiveCharts提供PieSlices,但PieChartControl只接受SeriesCollection,這就是爲什麼我的代碼崩潰時,我將pieChartData分配給圖表。 這是我嘗試在填充餅圖:

 LiveCharts.Wpf.PieChart pieChartData = new LiveCharts.Wpf.PieChart(); 

     foreach (var n in areavalues) 
     { 
      pieChartData.AddToView(new PieSlice 
      { 
       PieceValue = n 
      }); 
     } 

     areaChart.Series.Add(new PieSeries(pieChartData)); //<-- CRASH 

我有困難的時候找到了LiveCharts任何其他示例代碼,沒有人知道如何做到這一點?

回答

2

所以我終於得到它的工作:

 foreach (var n in classChartData.Slice) 
     { 
      areaChart.Series.Add(new PieSeries 
      { 
       Title = n.Key, 
       Values = new ChartValues<double> { n.Value } 
      }); 
     } 

     areaChart.LegendLocation = LegendLocation.Bottom; 

classChartData

private Dictionary<string, double> slice = new Dictionary<string, double>(); 

    public Dictionary<string, double> Slice 
    { 
     get { return slice; } 
     set { slice = value; } 
    } 

    public void AddSlice(string slicename, double slicevalue) 
    { 
     slice.Add(slicename, slicevalue); 
    } 

我希望這可以幫助任何使用LiveCharts。

0

這是非常有幫助的。在我看到您的解決方案之前,我正在努力解決這個問題 - 但它可以更簡單。在這個例子中,我創建一個簡單的,2片%壞餅圖,但很容易在這你的窗口或用戶控件XAML

<UserControl x:Class="BillyBob.SimplePieControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mlns:local="clr-namespace:BillyBob" 
    xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf" 
    mc:Ignorable="d" d:DesignHeight="185" d:DesignWidth="150"> 

    <lvc:PieChart x:Name="myPieChart" StartingRotationAngle="0" Height="120"/> 
</UserControl> 
擴展到任何數量的片

1,定義一個佔位符餅圖

然後加入切片作爲PieSeries將在控制構造函數,用假人,佔位符值

public SimplePieControl() 
{ 
    InitializeComponent(); 
    myPieChart.Series.Add(new PieSeries { Title = "BAD", Fill = Brushes.Red, StrokeThickness=0, Values = new ChartValues<double> { 0.0 } }); 
    myPieChart.Series.Add(new PieSeries { Title = "GOOD", Fill = Brushes.Green, StrokeThickness=0, Values = new ChartValues<double> { 100.0 } }); 

    DataContext = this; 
} 

要更新數據 - 簡單索引並在每個系列/片更新的第一個值

internal void RefreshData(double badPct) 
{ 
    myPieChart.Series[0].Values[0] = badPct; 
    myPieChart.Series[1].Values[0] = 100.0 - badPct; 
} 
0

萬一有人還在尋找一個解決方案,這是我如何解決它

Func<ChartPoint, string> labelPoint = chartPoint => 
     string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation); 

     ChartValues<double> cht_y_values = new ChartValues<double>(); 

     LiveCharts.SeriesCollection series = new LiveCharts.SeriesCollection(); 

     foreach (DataRow dr in dt.Rows) 
     { 
      PieSeries ps = new PieSeries 
      { 
       Title = dr[x_column_name].ToString(), 
       Values = new ChartValues<double> { 
           double.Parse(dr[y1_column_name].ToString())}, 
       DataLabels = true, 
       LabelPoint = labelPoint 

      }; 

      series.Add(ps); 
     } 
    pieChart.Series = series;