使用WPF Chart ToolKit基于所选的ComboBox创建图表

时间:2013-04-03 14:24:09

标签: c# wpf charts combobox

我有一个包含不同项目的ComboBox。根据这个组合框的值,我想要在图表中显示的该项目注册的不同时间。

我创建的图表的代码:

                    <chartingToolkit:Chart Height="262" HorizontalAlignment="Left" 
                    Margin="33,0,0,620" Name="columnChart"
                    VerticalAlignment="Bottom" Width="360" BorderBrush="AntiqueWhite" BorderThickness="1">
                    <chartingToolkit:ColumnSeries 
                        DependentValuePath="Value" 
                        IndependentValuePath="Key" 
                        ItemsSource="{Binding}"/>
                </chartingToolkit:Chart>

“应该”填充将创建图表的列表的代码:

private void showColumnChart()
    {
        List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string,int();
        List<ProsjektTime> timeListe = new List<ProsjektTime>();

        foreach (ProsjektTime p in Master.getDC().ProsjektTimes)
        {
            if (p.ProsjektID_FK == prosjekt.ProsjektID)
            {
                timeListe.Add(p);
            }
        }

        foreach(ProsjektTime p in timeListe)
        {
            valueList.Add(new KeyValuePair<string,int(p.TimeTyper.Type,p.AntallTimer));
        }

        try
        {
            columnChart.DataContext = valueList; 
        }
        catch (InvalidOperationException e) { MessageBox.Show(e+""); }
    }

不幸的是我收到错误,“此时无法修改此节点的逻辑子节点,因为树步行了”。

然而我发现了一些有趣的东西。如果我将ComboBox的selectedIndex设置为在其上注册了几个小时的项目(并非所有项目都在其上注册了数小时),那么一切正常。在图表和项目中显示小时数而没有任何注册时间的项目也会显示为空图表。我错过了什么?

1 个答案:

答案 0 :(得分:0)

找到答案。考虑到有一个dataSourceList和一个chartingToolkit:ColumnSeries,我创建了两个listing和两个chartingToolkit:ColumnSeries。下面是代码:

        private void prosjektTimeGraf()
    {
        int estimerteTimer = 0;
        int registrerteTimer = 0;

        List<KeyValuePair<string, int>> estimerListe = new List<KeyValuePair<string, int>>();
        List<KeyValuePair<string, int>> registrertListe = new List<KeyValuePair<string, int>>();

        foreach (ProsjektTime p in Master.getDC().ProsjektTimes)
        {
            if (p.ProsjektID_FK == prosjekt.ProsjektID)
            {
                estimerteTimer += p.AntallTimer;
                estimerListe.Add(new KeyValuePair<string, int>(p.TimeTyper.Type, p.AntallTimer));
            }
        }

        foreach (ProsjektTimeBruker ptb in Master.getDC().ProsjektTimeBrukers)
        {
            if (ptb.ProsjektID_FK == prosjekt.ProsjektID)
            {
                registrerteTimer += (int)ptb.AntallTimer;
                registrertListe.Add(new KeyValuePair<string, int>(ptb.TimeTyper.Type, (int)ptb.AntallTimer));
            }
        }

        estimerListe.Insert(0, new KeyValuePair<string, int>("Totalt", estimerteTimer));
        registrertListe.Insert(0, new KeyValuePair<string, int>("Totalt", registrerteTimer));

        try
        {
            espTimer.DataContext = estimerListe;
            regpTimer.DataContext = registrertListe;
        }
        catch (InvalidOperationException e) { MessageBox.Show(e+""); }
    }