Search code examples
wpfchartswpftoolkit

WPF Toolkit Chart Unordered LineSeries


The default LineSeries implementation orders data points by independed value. This gives me strange results for data like this:

Ordered LineSeries

Is it possible to plot a line series where lines are drawn between the points in the original order?


Solution

  • I currently solved this by inheriting from LineSeries:

    class UnorderedLineSeries : LineSeries
    {
        protected override void UpdateShape()
        {
            double maximum = ActualDependentRangeAxis.GetPlotAreaCoordinate(
                ActualDependentRangeAxis.Range.Maximum).Value;
    
            Func<DataPoint, Point> PointCreator = dataPoint =>
                new Point(
                    ActualIndependentAxis.GetPlotAreaCoordinate(
                    dataPoint.ActualIndependentValue).Value,
                    maximum - ActualDependentRangeAxis.GetPlotAreaCoordinate(
                    dataPoint.ActualDependentValue).Value);
    
            IEnumerable<Point> points = Enumerable.Empty<Point>();
            if (CanGraph(maximum))
            {
                // Original implementation performs ordering here
                points = ActiveDataPoints.Select(PointCreator);
            }
            UpdateShapeFromPoints(points);
        }
    
        bool CanGraph(double value)
        {
            return !double.IsNaN(value) &&
                !double.IsNegativeInfinity(value) &&
                !double.IsPositiveInfinity(value) &&
                !double.IsInfinity(value);
        }
    }
    

    Result: Unordered line series