繁体   English   中英

使用WPF和LVC(LiveCharts)在饼图或其他图表中具有n个楔形

[英]Having n wedges in a Pie Chart or other charts using WPF and LVC(LiveCharts)

我一直在尝试使用LVC合并饼图,并且效果很好。 我一直在玩这个简单的代码。

<UserControl x:Class="UI.PieChart"
             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" 
             xmlns:local="clr-namespace:UI"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="500"
             d:DataContext = "{d:DesignInstance local:PieChart}">
    <Grid>
        <lvc:PieChart LegendLocation="Bottom" DataClick="Chart_OnDataClick" Hoverable="False" DataTooltip="{x:Null}">
            <lvc:PieChart.Series>
                <lvc:PieSeries Name ="Portion" Title="Maria" Values="3" DataLabels="True"
                               LabelPoint="{Binding PointLabel0}"/>
                <lvc:PieSeries Title="Charles" Values="4" DataLabels="True" 
                               LabelPoint="{Binding PointLabel1}"/>
                <lvc:PieSeries Title="Frida" Values="6" DataLabels="True" 
                               LabelPoint="{Binding PointLabel2}"/>
                <lvc:PieSeries Title="Frederic" Values="2" DataLabels="True" 
                               LabelPoint="{Binding PointLabel3}"/>
            </lvc:PieChart.Series>
        </lvc:PieChart>
    </Grid>
</UserControl>

以及激活用户操作的此代码... .xaml.cs

namespace UI
{
    /// <summary>
    /// Interaction logic for DataChart.xaml
    /// </summary>
    public partial class PieChart : UserControl
    {
        public PieChart()
        {
            InitializeComponent();
            PieSeries()
            PointLabel = chartPoint =>
                            string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);

            DataContext = this;
        }
        public Func<ChartPoint, String> PointLabel { get; set;}

        private void Chart_OnDataClick(object sender, ChartPoint chartpoint)
        {
            var chart = (LiveCharts.Wpf.PieChart) chartpoint.ChartView;

            foreach (PieSeries series in chart.Series)
                series.PushOut = 0;
            var selectedSeries = (PieSeries)chartpoint.SeriesView;
            selectedSeries.PushOut = 8;
        }
    }
}

我对C#、. xaml,WPF和LVC完全陌生。但是,我想做的并不是硬编码PIE图表中的楔形数量。 相反,我想根据给出的数据创建一个饼图。 我想用C#做到这一点。 我在哪里实例化类PieChart()。 我可以在PieChart(5)这样的参数中传递5。 然后,将创建PieChart,然后继续创建5个PieSeries或5个楔形...侧面的问题,是否有比LVC甚至WPF更好的工具呢?

一种方法是创建一个SeriesCollection,根据一些用户输入为此添加一个PieSeries列表,并将XAML中的PieChart绑定到SeriesCollection。 您还需要实现INotifyPropertyChanged,以确保对SeriesCollection的更改反映在用户界面中(此处在RaisePropertyChanged中实现):

您将绑定到的SeriesCollection:

    private SeriesCollection myPie = new SeriesCollection();
    public SeriesCollection MyPie
    {
        get
        {
            return myPie;
        }
        set
        {
            myPie = value;
            RaisePropertyChanged("MyPie");
        }
    }

处理某些用户输入的代码(此处基于具有属性Value的UserInput类)。 切片的数量将等于您添加到SeriesCollection的PieSeries的数量:

foreach(item in UserInput)
{
    MyPie.Add(new PieSeries { Values = new ChartValues<int> { item.Value }, DataLabels = true };
}

在您的XAML中,将SeriesCollection绑定到PieChart:

<lvc:PieChart Series="{Binding MyPie}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM