简体   繁体   English

饼图未显示WPF

[英]Pie chart not displayed WPF

I used livechart in order to create a Pie chart, but whenever I run the program, the chart can't be seen ( http://imgur.com/GWecwgD ), but I can see the chart when I'm editing the code. 我使用livechart来创建一个饼图,但每当我运行程序时,都无法看到图表( http://imgur.com/GWecwgD ),但是当我编辑代码时我可以看到图表。 ( http://imgur.com/zVvkK3v ) http://imgur.com/zVvkK3v

This is my WPF code: 这是我的WPF代码:

        <lvc:PieChart Series="{Binding seriesCollection}" Height="150" InnerRadius="100" LegendLocation="Bottom" DataClick="Chart_OnDataClick" Hoverable="True">
            <lvc:PieChart.ChartLegend>
                <lvc:DefaultLegend BulletSize="20"></lvc:DefaultLegend>
            </lvc:PieChart.ChartLegend>
            <lvc:PieChart.DataTooltip>
                <lvc:DefaultTooltip BulletSize="20"></lvc:DefaultTooltip>
            </lvc:PieChart.DataTooltip>
        </lvc:PieChart>

And this is my c# code: 这是我的c#代码:

public Overview()
{
    InitializeComponent();
    NorthwindEntities db = new NorthwindEntities();
    var data = (from d in db.Sales_by_Categories group d by d.CategoryName into grouped select new { Key = grouped.Key, Sum = grouped.Sum(e => (double)e.ProductSales) });
    IEnumerable<Categorysales> datas = from c in data.AsEnumerable() select new Categorysales(c.Key, c.Sum);


    seriesCollection = new SeriesCollection();
    foreach (var item in datas)
    {
        seriesCollection.Add(new PieSeries { Title = item.Categoryname, Values = new ChartValues<ObservableValue> { new ObservableValue(item.Categorysum) }, DataLabels = true});//, LabelPoint = PointLabel 
    }
   /* PointLabel = chartPoint =>
        string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation);*/

   // DataContext = this;
}
public SeriesCollection seriesCollection { get; set; }



public Func<ChartPoint, string> PointLabel { get; set; }

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

    //clear selected slice.
    foreach (PieSeries series in chart.Series)
        series.PushOut = 0;

    var selectedSeries = (PieSeries)chartpoint.SeriesView;
    selectedSeries.PushOut = 8;
}

Is this in a Window or a UserControl? 这是在Window还是UserControl中?

If it's a window, change the binding to this: 如果是窗口,请将绑定更改为:

<lvc:PieChart 
    Series="{Binding seriesCollection, RelativeSource={RelativeSource AncestorType=Window}}"

If it's a UserControl, you know where this is going: 如果它是UserControl,您知道这是怎么回事:

<lvc:PieChart 
    Series="{Binding seriesCollection, RelativeSource={RelativeSource AncestorType=UserControl}}"

DataContext = this; is a bad habit. 是一个坏习惯。 Start doing that with UserControls and it breaks things. 用UserControls开始这样做,它打破了事情。 Even in a Window, it creates needless confusion. 即使在窗口中,它也会造成不必要的混乱。

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

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