2011-03-03 42 views
0

我有綁定堆疊柱系列到我的圖表很大的問題。有約束力的問題堆棧柱系列圖到系列

我有 公共ObservableCollection系列屬性在我的ViewModel並嘗試許多方法,但它仍然無法正常工作。

這是從視圖模型代碼以製備系列: 私人無效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收集和別人嘗試。 我將非常感謝一些幫助。

+0

我已經做了一個retag,下次使用'wpf'標籤是因爲它有更多的追隨者,而且問題有更好的回答機會。 – vorrtex 2011-03-05 14:55:36

回答

0

我希望我已經回答了你的問題there

反正我在這裏發佈我的回答的第二部分:

MainWindow.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> 

MainWindow.xaml。 cs

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

    } 
} 

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

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

我不知道的語法,但邏輯應該象下面這樣:

視圖模型

public class GraphItem { 
    public string IndependentValue { get; set; } 
    public int DependentValue1 { get; set; } 
    public int DependentValue2 { get; set; } 
} 

public class ChartViewModel 
{ 
    private List<GraphItem> itemCollection; 
    public List<GraphItem> ItemCollection 
    { 
     get { return itemCollection;} 
     set { 
      itemCollection=value; 
      OnPropertyChanged("ItemCollection"); 

      } 
    } 

    public ChartViewModel() 
    { 
     //Bind ItemCollection 
    } 


} 

的XAML:

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

願這幫助。