Search code examples
c#livecharts

How to access index when using Mappers.Xy<double> with vertical Series?


I'm trying to understand the Mappers.XY class using a vertical series based on doubles. To test it, I want to have all the even indices of the Y axis be red filled but the .Fill seems to only use the X values. Here's the code I have and the results:

var RedBrush = new SolidColorBrush(Color.FromRgb(238, 83, 80));

Mapper = Mappers.Xy<double>()
    .X((value, index) => value)
    .Y((value, index) => index)
    .Fill(index => index % 2 == 0 ? RedBrush : null)
    .Stroke(index => index % 2 == 0 ? RedBrush : null);

I end up with red bars only when the X value is even as shown here: Red X-values I even changed the .Fill to be based on value but with no change.

EDIT: I think I've narrowed down the problem to using the default method for .Fill which is:

public CartesianMapper<T> Fill(Func<T, object> predicate);

I think this uses the double as the basis for the fill, which in my case, would be the X-values. Instead, I should use the overload for .Fill which seems to consider the index as an int...which is what I want:

 public CartesianMapper<T> Fill(Func<T, int, object> predicate);

The problem is that I'm not sure how to use this. Instead of having:

.Fill(index => index % 2 == 0 ? RedBrush : null)

what should it be? My lamda skills are at a novice level.

***PREVIOUS TEXT THAT CAN BE IGNORED...

I've reviewed the Types and Configurations for help here: https://lvcharts.net/App/examples/v1/wpf/Types%20and%20Configuration and more specifically am trying to adapt these two concepts to my own:

  1. At Series Collection Level

When you define a Series collection instance you can also pass a default configuration, this configuration overrides the global configuration and will be set only if Series configuration is null:

  var mapper = Mappers.Xy<MyClass>().X(v => v.XProp).Y(v => v.YProp);
  var seriesCollection = new SeriesCollection(mapper);
  myChart.SeriesCollection = seriesCollection;
  1. At a specific series

Finally you can also define a mapper only for a series, this will override the >globally and SeriesCollection configuration:

var mapper = Mappers.Xy<MyClass>().X(v => v.XProp).Y(v => v.YProp);
var pieSeries = new PieSeries(mapper);

The problem is that the examples use ObservablePoints with properties I don't have because I'm just using doubles.

Does anyone know if it's possible to do this using only doubles? Any alternatives?


Solution

  • You need to change the lambda expression for .Fill as follows:

        var Mapper = Mappers.Xy<double>()
                .X((value, index) => value)
                .Y((value, index) => index)
                .Fill((value, index) => index % 2 == 0 ? RedBrush : null);
    

    For some reason in your expression it seems the lambda expression was evaluating value % 2 instead of index % 2.

    Below is an image of a row series chart using the mapper: Row series chart