Search code examples
c#wpflivecharts

LiveCharts (LVC) WPF - get chart from DataClick event


I've got a 'dashboard' with several charts on it. One of them is a pie chart with a number of series.

LiveCharts has a DataClick event

DataClick(object sender, ChartPoint chartPoint)

sender is of type PieSlice. How can i access SeriesCollection from that event or, alternatively, chart name / id?

What I am trying to achieve is access chart that sent the event, then it's series collection and check which of the series / pie slice fired the event.


Solution

  • First and foremost, don't use events, use commands - that's the MVVM way. i.e.

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

    Note the binding to 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;
        }
    }
    

    And about handling the command:

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