2013-03-26 30 views
3

默認LineSeries實現通過獨立值對數據點進行排序。這讓我對這樣的數據奇怪的結果:WPF工具包Chart無序LineSeries

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

結果: Unordered line series

回答

5

我目前由供LineSeries繼承解決了這個將你的XAML指向這個,而不是程序集。 即

XAML:

xmlns:chart="clr-namespace:MyApplication.UserControls.Charting" 

C#:

using System.Windows.Controls.DataVisualization.Charting; 
using System.Windows; 

namespace MyApplication.UserControls.Charting { 

    class Chart : System.Windows.Controls.DataVisualization.Charting.Chart {} 

    class UnorderedLineSeries : LineSeries { 
    .... 
    }  
} 
+1

解決我的問題太多,謝謝! – tdc 2013-05-01 14:32:37

+2

你在哪裏使用該代碼? – FrancescoDS 2015-02-04 17:57:37

+0

@FrancescoDS,它已經晚了一年多,但我已經在下面粘貼瞭如何使用它。 :) – XtraSimplicity 2016-05-05 05:04:29

0

值得指出的是,上面用@ hansmaad的建議,你需要創建一個新的命名空間和