使用WPF chart ToolKit基于所选的组合框创建图表
本文关键字:组合 创建 于所选 WPF chart ToolKit 使用 | 更新日期: 2023-09-27 18:07:10
我有一个不同项目的组合框。根据这个组合框的值,我想要不同的小时,已注册在这个项目在图表中显示。
我创建的图表代码:
<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设置为一个在其上注册了一些小时的项目(并不是所有项目都在其上注册了小时),那么一切都很好。有小时数的项目会显示在图表中,没有小时数的项目也会显示为空图表。我错过了什么?找到答案了。我没有使用一个dataSourceList和一个chartingToolkit:ColumnSeries,而是创建了两个列表和两个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+""); }
}