【问题标题】:Create Pie Chart Slices in LiveCharts在 LiveCharts 中创建饼图切片
【发布时间】:2017-01-09 22:53:15
【问题描述】:

我正在使用 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 提供PieSlicesPieChart Control 只接受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 的任何其他示例代码,有人知道怎么做吗?

【问题讨论】:

    标签: c# winforms charts livecharts


    【解决方案1】:

    所以我终于让它工作了:

            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 的人。

    【讨论】:

      【解决方案2】:

      这很有帮助。在我看到你的解决方案之前,我一直在努力解决这个问题——但它仍然可以简单得多。在此示例中,我创建了一个简单的 2 切片 %bad 饼图,但很容易将其扩展到任意数量的切片

      1st,在您的 Window 或 UserControl xaml 中定义一个占位符 pieChart

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

      然后将切片作为 PieSeries 添加到您的控制 ctor 中,并带有虚拟的占位符值

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

      【讨论】:

        【解决方案3】:

        如果有人仍在寻找解决方案,我就是这样解决的

        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;
        

        【讨论】:

          【解决方案4】:
          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;
                  }
              }
          }
          

          【讨论】:

          • 请为您的代码添加一些解释。这样会更容易理解你的答案。
          • 欢迎来到 Stack Overflow!虽然这段代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多