Closed schnerring closed 1 year ago
Hello
There are 2 ways to map an object to the chart coordinates, using mappers, or implementing IChartEntity.
The thing here is that DateTime point already implements IChartEntity
, that is why your mapper is ignored.
The magic to use the DateTime
type, is to map it to the `Ticks
property:
public class LogarithmicDateTimePoint
{
public DateTime X { get; set; }
public double Y { get; set; }
}
// then on your mapper:
public ISeries[] Series { get; set; } =
{
new LineSeries<LogarithmicDateTimePoint>
{
Mapping = (logPoint, chartPoint) =>
{
// USE DATETIME.TICKS
chartPoint.Coordinate = new(logPoint.X.Ticks, Math.Log(logPoint.Y, s_logBase));
},
Values = new LogarithmicDateTimePoint[]
{
new() { X = s_start.AddDays(1), Y = 1 },
new() { X = s_start.AddDays(2), Y = 10 },
new() { X = s_start.AddDays(3), Y = 100 },
new() { X = s_start.AddDays(4), Y = 1000 },
new() { X = s_start.AddDays(5), Y = 10000 },
new() { X = s_start.AddDays(6), Y = 100000 },
new() { X = s_start.AddDays(7), Y = 1000000 },
new() { X = s_start.AddDays(8), Y = 10000000 }
}
}
};
// Finally, you can use the `DateTimeAxis` so it handles the ticks property for you:
public Axis[] XAxes { get; set; } = new Axis[]
{
new DateTimeAxis(TimeSpan.FromDays(1), date => date.ToString("dd MMM"))
};
And that's it!
Describe the bug
From the logarithmic scale sample, I use the following as a working baseline:
This produces the following result:
Now let's replace the
LogarithmicPoint
s withDateTimePoint
s:I set a breakpoint inside the
Mapping
action, and it is never executed. Here's the rendered result:To Reproduce
Define a line series with
DateTimePoint
values and a mapping functionExpected behavior
So while writing this issue I digged into the source code a bit and found this:
https://github.com/beto-rodriguez/LiveCharts2/blob/5dcc732235a99033516790fa12484b4a44df57da/src/LiveChartsCore/Kernel/Providers/DataFactory.cs#L335-L340
Do I understand correctly that because
DateTimePoint
s are "chart entities" (IChartEntity
), they already "define a point with a visual representation in the user interface" and hence shouldn't be remapped? It kind of makes sense that we want to map models instead, but it's still a bit confusing that "chart entities" are just ignored by the mapping function. If my understanding is correct here, I think it would be nice if an exception was thrown if the user tries to remap chart entities.The obvious solution to this is applying
Math.Log
in the constructor ofDateTimePoint
, or is there another way I'm unaware of?Desktop (please complete the following information):