gpt4 book ai didi

c# - 饼图不显示 WPF

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:33 25 4
gpt4 key购买 nike

我使用 livechart 来创建饼图,但每当我运行程序时,都看不到图表 ( http://imgur.com/GWecwgD ),但在编辑代码时可以看到图表。 ( http://imgur.com/zVvkK3v )

这是我的 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>

这是我的 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;
}

最佳答案

这是在窗口还是用户控件中?

如果它是一个窗口,将绑定(bind)更改为:

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

如果它是一个 UserControl,您知道它的去向:

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

DataContext = this; 是个坏习惯。开始使用 UserControls 这样做,它会破坏事情。即使在窗口中,它也会造成不必要的困惑。

关于c# - 饼图不显示 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43823425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com