LiveCharts2 icon indicating copy to clipboard operation
LiveCharts2 copied to clipboard

Map Values to Fill / Stroke Color

Open waffle-lord opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. There used to be a way to map values to fill/stroke for individual points. I'm unsure if this is supported or not. I can't seem to find a way to do this in live charts 2.

Past Issue for reference: https://github.com/Live-Charts/Live-Charts/issues/162

Describe the solution you'd like A way for a HasMap configuration to be able to modify point's fill and stroke (possibly other properties).

Describe alternatives you've considered Maybe charts could have some kind of PointFormatter that can modify point related properties before they are graphed? (similar to DataLabelsFormatter)

Additional context Not much to add since this was previous disgusted in Live charts v1. It's just really handy to be able to modify the color of certain points based on their values.

waffle-lord avatar Oct 07 '21 18:10 waffle-lord

Any chance the "Point Mapper" feature has been added to livecharts2 yet? This feature will be very useful to have in livecharts 2 where we can define a mapper for each series separately and modify point color/etc via the mapper.

LiveCharts1 Mapper: https://lvcharts.net/App/examples/v1/wpf/Types%20and%20Mappers

LiveCharts1 Change Color with Mapper: https://stackoverflow.com/questions/56181200/individually-colored-data-points-in-livecharts-wpf

I noticed a "Mapping" action in the Series class however am not sure how this can be implemented as the Series.Mapper is no longer possible in livecharts2... LiveCharts2 Series.Mapping with color changes, is it possible via: public Action<TModel, ChartPoint>? Mapping { get; set; } //Line 77 of LiveChartsCore.Series class

preintel avatar Nov 18 '21 00:11 preintel

For now I am able to do the following but not what we need:

cartesianChart1.Series = new List<ISeries>
                                                     {
                                                          new ColumnSeries<DateTimePoint>
                                                         {
                                                              Name = "Test",
                                                              Values = myList,
                                                              Mapping = mapper
                                                         }
                                                     };

Where mapper is:

Action<DateTimePoint, LiveChartsCore.Kernel.ChartPoint> mapper = (model, point) =>
                                {
                                    if (model is null)
                                        throw new Exception(
                                            $"A {nameof(DateTimePoint)} can not be null, instead set to null the " +
                                            $"{nameof(DateTimePoint.Value)} property.");

                                    if (model.Value is null)
                                    {
                                        point.IsNull = true;
                                        return;
                                    }

                                    point.IsNull = false;
                                    point.PrimaryValue = model.Value.Value;
                                    point.SecondaryValue = model.DateTime.Ticks;

                                    try
                                    {
                                        //Check condition here and set point state...
                                        if(true)
                                        point.Context.Series.AddPointToState(point, "1");
                                    }
                                    catch(Exception except1)
                                    { }
                                };

preintel avatar Nov 18 '21 01:11 preintel

Looks like some people are also using the point created event.

https://github.com/beto-rodriguez/LiveCharts2/discussions/134

Haven't tried it myself though.

waffle-lord avatar Nov 18 '21 12:11 waffle-lord

This is already supported and will be included in the next version of the library:

// full sample at:
// https://github.com/beto-rodriguez/LiveCharts2/blob/dev/samples/ViewModelsSamples/General/ConditionalDraw/ViewModel.cs

var series1 = new ColumnSeries<ObservableValue>
{
    Values = new ObservableValue[] { new(2), new(5), new(4), new(6), new(8), new(3), new(2), new(4), new(6) }
};

series1
    .WithConditionalPaint(new SolidColorPaint(SKColors.Black))
    .When(point => point.Model?.Value > 5);

beto-rodriguez avatar Sep 20 '22 14:09 beto-rodriguez