2011-03-05 68 views
1

我有綁定堆疊列系列到我的圖表很大的問題。圖表綁定在WPF

我在我的ViewModel中公開了ObservableCollection Series屬性,並嘗試了很多方法,但仍然無法正常工作。

這是從ViewModel準備系列代碼:private void drawChart(){this.Series.Clear();

var dataValues = new List<List<SimpleDataValue>>(); 

    int wartoscNiezalezna = 1; 

    for (int i = 0; i < 2; i++) 
    { 
     dataValues.Add(new List<SimpleDataValue>()); 
    } 

    foreach (var item in myCollection) 
    { 
     var param = someparam; 

     dataValues[0].Add(new SimpleDataValue { IndependentValue = "Czujnik " + wartoscNiezalezna, DependentValue = 100 }); 
     //czerwone 
     dataValues[1].Add(new SimpleDataValue { IndependentValue = "" + wartoscNiezalezna, DependentValue = 200 }); 

     wartoscNiezalezna++; 

    } 
    var stackedSeries = Activator.CreateInstance(typeof(StackedColumnSeries)) as DefinitionSeries; 

    int itemnr=0; 
    foreach (var item in dataValues) 
    { 
     var definicja = new SeriesDefinition(); 
     if(itemnr==0) 
     definicja.Title = "Stan 1"; 
     else 
      definicja.Title = "Stan 2"; 
     definicja.DependentValuePath = "DependentValue"; 
     definicja.IndependentValuePath = "IndependentValue"; 
     definicja.ToolTip = "asdas"; 
     definicja.ItemsSource = item; 
     stackedSeries.SeriesDefinitions.Add(definicja); 
     itemnr++; 
    } 
    Series.Add(stackedSeries); 
} 

我不能把它綁定到:

<charting:Chart x:Name="MyChart" Padding="10,10,10,10"> 
    <charting:Chart.Series> 
     <charting:StackedColumnSeries> 
      <charting:SeriesDefinition ItemsSource="{Binding Series}" DependentValuePath="DependentValue" IndependentValuePath="IndependentValue"> 
      </charting:SeriesDefinition> 
     </charting:StackedColumnSeries> 
    </charting:Chart.Series> 
</charting:Chart> 

我與SeriesDefinitions收集和別人嘗試。我會非常感謝一些幫助。

回答

2

我在WPF中找不到StackedColumnSeries,所以我會使用Silverlight。當我看到你在代碼中創建SeriesDefinition,在那種情況下,在結合沒有意義,這裏是正確的代碼:

XAML中沒有海圖系列:

<charting:Chart x:Name="MyChart" Padding="10,10,10,10" /> 

後面的代碼:

public class SimpleDataValue 
{ 
    public string IndependentValue { get; set; } 
    public int DependentValue { get; set; } 
} 

public partial class MainPage : UserControl 
{ 
    private int[] myCollection = new int[10]; 
    private List<DefinitionSeries> Series = new List<DefinitionSeries>(); 

    public MainPage() 
    { 
     InitializeComponent(); 
     this.drawChart(); 
     this.Series.ForEach(this.MyChart.Series.Add); 
    } 

    private void drawChart() 
    { 
     this.Series.Clear(); 
     this.Series = new List<DefinitionSeries>(); 
     var dataValues = new List<List<SimpleDataValue>>(); 

     int wartoscNiezalezna = 1; 

     for (int i = 0; i < 2; i++) 
     { 
      dataValues.Add(new List<SimpleDataValue>()); 
     } 

     foreach (var item in myCollection) 
     { 
      dataValues[0].Add(new SimpleDataValue { IndependentValue = "Czujnik " + wartoscNiezalezna, DependentValue = 100 }); 
      //czerwone 
      dataValues[1].Add(new SimpleDataValue { IndependentValue = "" + wartoscNiezalezna, DependentValue = 200 }); 

      wartoscNiezalezna++; 
     } 

     var stackedSeries = Activator.CreateInstance(typeof(StackedColumnSeries)) as DefinitionSeries; 

     int itemnr = 0; 
     foreach (var item in dataValues) 
     { 
      var definicja = new SeriesDefinition(); 
      if (itemnr == 0) 
       definicja.Title = "Stan 1"; 
      else 
       definicja.Title = "Stan 2"; 
      definicja.DependentValuePath = "DependentValue"; 
      definicja.IndependentValuePath = "IndependentValue"; 
      ToolTipService.SetToolTip(definicja, "asdas"); 
      definicja.ItemsSource = item; 
      stackedSeries.SeriesDefinitions.Add(definicja); 
      itemnr++; 
     } 
     Series.Add(stackedSeries); 
    } 

} 

那麼我呢,我會以這種方式解決你的問題,因爲ViewModel一定不能包含任何形式的視圖:

Xaml

<charting:Chart x:Name="MyChart" Padding="10,10,10,10"> 
    <charting:Chart.Series> 
     <charting:StackedColumnSeries> 
      <charting:SeriesDefinition Title="Stan 1" ItemsSource="{Binding FirstCollection}" DependentValuePath="DependentValue" IndependentValuePath="IndependentValue" /> 
      <charting:SeriesDefinition Title="Stan 2" ItemsSource="{Binding SecondCollection}" DependentValuePath="DependentValue" IndependentValuePath="IndependentValue" /> 
     </charting:StackedColumnSeries> 
    </charting:Chart.Series> 
</charting:Chart> 

代碼隱藏

public class ChartModel 
{ 
    public List<SimpleDataValue> FirstCollection { get; set; } 
    public List<SimpleDataValue> SecondCollection { get; set; } 
} 

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     this.MyChart.DataContext = new ChartModel 
     { 
      FirstCollection = Enumerable.Range(1, 10).Select(i => new SimpleDataValue { IndependentValue = "Czujnik " + i, DependentValue = 100 }).ToList(), 
      SecondCollection = Enumerable.Range(1, 10).Select(i => new SimpleDataValue { IndependentValue = "" + i, DependentValue = 200 }).ToList() 
     }; 

    } 
} 
+0

非常感謝你,非常多,這是很好的工作了。 THANKS !! :) – jars 2011-03-10 16:04:35

+1

@jars好的,接受答案,將來你會詢問其他可能的問題時,它會給你更好的聲譽。 – vorrtex 2011-03-10 17:40:57