Search code examples
c#wpflivecharts

Is there a way to remove just one lineseries from the legend in a wpf Livecharts Graph?


So i'm using the Livecharts for wpf and made a SeriesCollection which holds multiple LineSeries and StackedAreas. So far so good.

I display all graphs in a single Chart, thus every line or stacked area is listed in the legend. My problem is, that i need to remove just one LineSeries from the legend.

In the documentary for LiveCharts there's only a way to remove the whole legend or to make a custom one but thats not exactly what i need. I figuered there must be an easy way to just remove the single graph from the legend.


Solution

  • You have to override the default legend by overriding the ControlTemplate of CartesianChart.ChartLegend.
    Then create your own legend by introducing a new collection inside your model, which holds all the LineSeries you wish to create a legend for.

    This example uses a ListBox to hold the legend items and overrides the ListBox.ItemsTemplate to actually design the items.

    Model public class PointShapeLineExample { public PointShapeLineExample() { InitializeComponent();

            SeriesCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "Series 1",
                    Values = new ChartValues<double> { 4, 6, 5, 2 ,4 }
                },
                new LineSeries
                {
                    Title = "Series 2",
                    Values = new ChartValues<double> { 6, 7, 3, 4 ,6 },
                    PointGeometry = null
                },
                new LineSeries
                {
                    Title = "Series 3",
                    Values = new ChartValues<double> { 4,2,7,2,7 },
                    PointGeometry = DefaultGeometries.Square,
                    PointGeometrySize = 15
                }
            };
    
            Labels = new[] {"Jan", "Feb", "Mar", "Apr", "May"};
            YFormatter = value => value.ToString("C");
    
            //modifying the series collection will animate and update the chart
            SeriesCollection.Add(new LineSeries
            {
                Title = "Series 4",
                Values = new ChartValues<double> {5, 3, 2, 4},
                LineSmoothness = 0, //0: straight lines, 1: really smooth lines
                PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"),
                PointGeometrySize = 50,
                PointForeground = Brushes.Gray
            });
    
            //modifying any series values will also animate and update the chart
            SeriesCollection[3].Values.Add(5d);
    
            // Only create a legend for the first two series
            LegendSeries = new List<LineSeries>(SeriesCollection.Take(2));
        }
    
        public IEnumerable<LineSeries> LegendSeries { get; set; }
        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public Func<double, string> YFormatter { get; set; }
    
    }
    

    View

    <Window>
      <Window.DataContext>
        <PointShapeLineExample />
      </Window.DataContext>    
    
      <CartesianChart Series="{Binding SeriesCollection}" 
                      LegendLocation="Left" >
        <CartesianChart.ChartLegend>
          <DefaultLegend>
            <DefaultLegend.Template>
              <ControlTemplate TargetType="DefaultLegend">
                <ListBox ItemsSource="{Binding LegendSeries}"
                         VerticalAlignment="Center"
                         HorizontalAlignment="Center"
                         IsHitTestVisible="False">
                  <ListBox.ItemTemplate>
                    <DataTemplate DataType="{x:Type LineSeries}">
                      <StackPanel Orientation="Horizontal">
                        <Rectangle Fill="{Binding Stroke}" 
                                   Height="12" 
                                   Width="12" 
                                   Margin="0,0,4,0" />
                        <TextBlock Text="{Binding Title}" />
                      </StackPanel>
                    </DataTemplate>
                  </ListBox.ItemTemplate>
                </ListBox>
              </ControlTemplate>
            </DefaultLegend.Template>
          </DefaultLegend>
        </CartesianChart.ChartLegend>
    
        <CartesianChart.AxisY>
          <Axis Title="Sales" LabelFormatter="{Binding YFormatter}" />
        </CartesianChart.AxisY>
        <CartesianChart.AxisX>
          <Axis Title="Month" Labels="{Binding Labels}" />
        </CartesianChart.AxisX>
      </CartesianChart>
    </Window>