2017-01-09 8 views
0

私はLiveChartを使って円グラフを作成しています。私は円グラフで表現したいダブルスのリストを持っています。問題は、リストの値が変化する可能性があるため、それに応じてチャートを変更できることです。ここでライブチャットで円グラフスライスを作成

が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を提供しますが、PieChartControlSeriesCollectionしか受け付けないので、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スライス%悪い円グラフを作成していますが、あなたのウィンドウまたはUserControlの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> 
にプレースホルダpieChartのを定義し、スライス

第一の任意の数にこれを拡張することは容易です

その後プレースホルダがデータ更新するには

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

値、ダミーで、あなたのコントロールのctorでなPieSeriesとしてスライスを追加 - に簡単にインデックスを、各シリーズ/スライスに

を第一値を更新210
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; 
を解決する方法であります
関連する問題