开发者

Problem with binding Stacked colums series chart to Series

开发者 https://www.devze.com 2023-02-15 03:31 出处:网络
i have big problem with binding Stacked Column Series to my chart. I have public ObservableCollection Series property in my ViewModel and try by many ways but it still not working.

i have big problem with binding Stacked Column Series to my chart.

I have public ObservableCollection Series property in my ViewModel and try by many ways but it still not working.

This is code from ViewModel to prepare Series: 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);
    }

I cant bind it to:

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

I was tryin开发者_JAVA技巧g with SeriesDefinitions Collection and others. I will be very grateful to some help.


I hope I've answered your question there

Anyway I post the second part of my answer here:

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


I am not sure with the syntax but the logic should be like below:

ViewModel

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>

May this help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号