3

我有一个“仪表板”,上面有几个图表。其中之一是带有多个系列的饼图。

LiveCharts 有一个 DataClick 事件

DataClick(object sender, ChartPoint chartPoint)

sender是类型PieSlice。我如何SeriesCollection从该事件或图表名称/ ID 中访问?

我想要实现的是访问发送事件的图表,然后是系列集合并检查哪个系列/饼图触发了事件。

4

2 回答 2

6

首先,不要使用events,使用commands- 这是 MVVM 方式。IE

<LiveCharts:PieChart DataClickCommand="{Binding DrillDownCommand}" Series="{Binding MySeries}" ...>

注意绑定到MySeries

public SeriesCollection MySeries
{
    get
    {
        var seriesCollection = new SeriesCollection(mapper);

        seriesCollection.Add(new PieSeries()
        {
            Title = "My display name",
            Values = new ChartValues<YourObjectHere>(new[] { anInstanceOfYourObjectHere })
        });

        return seriesCollection;
    }
}

关于处理命令:

public ICommand DrillDownCommand
{
    get
    {
        return new RelayCommand<ChartPoint>(this.OnDrillDownCommand);
    }
}

private void OnDrillDownCommand(ChartPoint chartPoint)
{
    // access the chartPoint.Instance (Cast it to YourObjectHere and access its properties)
}
于 2017-03-06T08:28:23.203 回答
2

您需要处理参数,而不是发送者。第二个参数是ChartPoint,即包含SeriesView。所以只需访问它并使用它Title

    private void Chart_OnDataClick(object sender, ChartPoint chartpoint) {
        MessageBox.Show(chartpoint.SeriesView.Title);
    }

如何从该事件或图表名称/ID 访问 SeriesCollection?

SeriesView 不是整个 SeriesCollection,而是Series你点击的。你可以拥有它的名字

于 2017-03-06T08:51:23.133 回答