Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map Values to Fill / Stroke Color #229

Closed
waffle-lord opened this issue Oct 7, 2021 · 4 comments
Closed

Map Values to Fill / Stroke Color #229

waffle-lord opened this issue Oct 7, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@waffle-lord
Copy link

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: Live-Charts/Live-Charts#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.

@beto-rodriguez beto-rodriguez added the enhancement New feature or request label Oct 21, 2021
@preintel
Copy link

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
Copy link

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

@waffle-lord
Copy link
Author

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

#134

Haven't tried it myself though.

@beto-rodriguez
Copy link
Owner

beto-rodriguez commented Sep 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants