2012-01-10 39 views
1

如何交換WPF Toolkit折線圖中的座標軸?我有以下XAML:如何在WPF Toolkit折線圖中交換座標軸?

<chartingToolkit:Chart Name="lineChart" Title="Line Series Demo" Margin="0,0,0,0"> 
    <chartingToolkit:LineSeries DependentValuePath="Key" IndependentValuePath="Value" ItemsSource="{Binding}" IsSelectionEnabled="True" /> 
    </chartingToolkit:Chart> 

從屬值始終顯示爲Y軸。我需要它們作爲X軸。這可能與WPF工具包圖表?如果我不能使用WPF Toolkit來做到這一點,那麼我可以使用其他免費的WPF圖表庫嗎?我所需要的是可以在X軸上處理多個系列的線形圖,並在Y軸上處理字符串。

+0

你能不能簡單地設置'DependentValuePath =「Value」IndependentValuePath =「Key」'? – 2012-01-10 21:43:09

+0

@djacobson數據被定義爲'KeyValuePair ',所以如果我按照您的建議切換值和鍵,我會得到一個InvalidOperationException「沒有合適的座標軸可用於繪製相關值。 – Rado 2012-01-10 21:54:53

回答

0

在試圖找到原始WPF工具包問題的答案失敗後,我決定自己編輯Charting工具包源代碼。下面是我爲了做得到它的工作的變化:

(顯示源代碼的變化,看到原始來源,看看原帖編輯之前)

  1. 獲取源。我使用的是VS2010,由於WPF Toolkit的某些組件 已成爲.NET4的一部分,因此我不得不修改WPF 工具包解決方案。謝天謝地,這項工作已經完成了。 獲取源here
  2. 更改LineSeries.cs:115

       if (axis == null) 
           { 
            axis = new CategoryAxis(); 
           } 
    
  3. 更改LineAreaBaseSeries.cs:243

    double x = ActualIndependentAxis.GetPlotAreaCoordinate(dataPoint.ActualIndependentValue).Value; 
        double y = this.InternalActualDependentAxis.GetPlotAreaCoordinate(dataPoint.ActualDependentValue).Value; 
    
        if (ValueHelper.CanGraph(x) && ValueHelper.CanGraph(y)) 
        { 
         dataPoint.Visibility = Visibility.Visible; 
    
         double coordinateY = Math.Round(y - (dataPoint.ActualHeight/2)); 
         Canvas.SetTop(dataPoint, coordinateY); 
         double coordinateX = Math.Round(x - (dataPoint.ActualWidth/2)); 
         Canvas.SetLeft(dataPoint, coordinateX); 
        } 
        else 
        { 
         dataPoint.Visibility = Visibility.Collapsed; 
        } 
    

更改LineAreaBaseSeries.cs:298

 Func<DataPoint, Point> createPoint = 
      dataPoint => 
       new Point(
        ActualIndependentAxis.GetPlotAreaCoordinate(dataPoint.ActualIndependentValue).Value, 
        this.InternalActualDependentAxis.GetPlotAreaCoordinate(dataPoint.ActualDependentValue).Value); 
     IEnumerable<Point> points = Enumerable.Empty<Point>(); 
     if (ActualIndependentAxis is IRangeAxis) 
     { 
      points = DataPointsByIndependentValue.Select(createPoint); 
     } 
     else 
     { 
      points = 
      ActiveDataPoints 
        .Select(createPoint) 
        .OrderBy(point => point.X); 
     } 

完成這些更改後,您可以使用相關的字符串來源。

乾杯!