2013-04-29 49 views
0

如何用DVC系列系列圖形添加多個系列?帶多個系列的DVC系列圖表

我有下面的代碼生成我的第一個系列:

WPF:

<DVC:Chart Name="Chart" 
      Background="#463F3F">     
     <DVC:Chart.Series> 
      <DVC:LineSeries Title=" Monthly Count" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}"> 
     </DVC:LineSeries> 
    </DVC:Chart.Series> 
    <DVC:Chart.PlotAreaStyle> 
     <Style TargetType="Grid"> 
      <Setter Property="Background" Value="Transparent" /> 
     </Style> 
    </DVC:Chart.PlotAreaStyle> 
</DVC:Chart> 

C#:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

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

namespace WpfApplication1 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     LoadLineChartData(); 

    } 

    private void LoadLineChartData() 
    { 
     ((LineSeries)Chart.Series[0]).ItemsSource = 
      new KeyValuePair<DateTime, int>[]{ 
    new KeyValuePair<DateTime,int>(DateTime.Now, 100), 
    new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(1), 130), 
    new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(2), 150), 
    new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(3), 125), 
    new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(4),155)}; 
    } 
} 
} 

的C#代碼是在系列設置,我只需要知道如何添加另一個。

在此先感謝。

回答

0

您可以在代碼背後做所有事情。從您的XAML中刪除<DVC:Chart.Series>部分。 然後,您可以通過編程設定所有系列(這個例子是基於XAML中添加一個系列,只要你想用FOR循環)複製儘可能多的時間(即添加幾個系列):

private void LoadLineChartData() 
{ 
    //Chart is your chart object in Xaml 
    //declare your series 
    LineSeries ls = new LineSeries(); 

    ls.Title = "Monthly Count"; 
    ls.IndependentValueBinding = new Binding("Key"); 
    ls.DependentValueBinding = new Binding("Value"); 

    ls.ItemsSource = new KeyValuePair<DateTime, int>[] 
    { 
     new KeyValuePair<DateTime,int>(DateTime.Now, 100), 
     new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(1), 130), 
     new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(2), 150), 
     new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(3), 125), 
     new KeyValuePair<DateTime,int>(DateTime.Now.AddMonths(4),155)}; 

     // then add it to the chart 
     Chart.Series.Add(ls);  
    } 
}