C# 基于选定的组合框,使用WPF图表工具包创建图表

C# 基于选定的组合框,使用WPF图表工具包创建图表,c#,wpf,charts,combobox,C#,Wpf,Charts,Combobox,我有一个包含不同项目的组合框。根据这个组合框中的值,我希望在图表中显示在这个项目上注册的不同小时数 我创建的图表的代码: <chartingToolkit:Chart Height="262" HorizontalAlignment="Left" Margin="33,0,0,620" Name="columnChart" VerticalAlignment="Bott

我有一个包含不同项目的组合框。根据这个组合框中的值,我希望在图表中显示在这个项目上注册的不同小时数

我创建的图表的代码:

                    <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+""); }
    }
private void showColumnChart()
{

List valueList=new List找到了答案。我创建了两个列表和两个chartingToolkit:ColumnSeries,而不是一个dataSourceList和一个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+""); }
    }
private void prosjektTimeGraf()
{
int-estimetertimer=0;
int registerentetimer=0;
List estimerListe=新列表();
List RegistreTListe=新列表();
foreach(Master.getDC()prosjektimes中的prosjektime p)
{
if(p.ProsjektID_FK==prosjekt.ProsjektID)
{
estimerteTimer+=p.AntallTimer;
添加(新的键值对(p.TimeTyper.Type,p.AntallTimer));
}
}
foreach(Master.getDC()ProsjektTimeBrukers中的ProsjektTimeBruker ptb)
{
if(ptb.ProsjektID_FK==prosjekt.ProsjektID)
{
寄存器定时器+=(int)ptb.AntallTimer;
registerListe.Add(新的KeyValuePair(ptb.TimeTyper.Type,(int)ptb.AntallTimer));
}
}
插入(0,新的KeyValuePair(“Totalt”,estimerteTimer));
Insert(0,新的KeyValuePair(“Totalt”,registererttimer));
尝试
{
espTimer.DataContext=estimerListe;
regpTimer.DataContext=RegistreTListe;
}
catch(invalidooperationexception e){MessageBox.Show(e+“”);}
}