我必须为某些统计信息创建wpf应用程序。数据将来自数据库。
现在,我尝试使用LiveCharts基本堆叠CartesianChart
。我想在主窗口中有2个不同的图表。因此,我下载了一个示例,将其作为第一个图表,第二个作为副本。
但是,当我单击运行按钮以生成图表时(图表中的数据是一些随机值,以后它将来自LIST),我看到仅第一个图表为空。
这里xaml:
<ScrollViewer x:Name="ScrollViewerDay"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Hidden"
Grid.Row="2"
Grid.RowSpan="20"
Grid.ColumnSpan="20">
<StackPanel x:Name="StackDay"
Grid.Column="0"
Grid.Row="2"
Grid.ColumnSpan="20"
Grid.RowSpan="21"
Margin="10,5,10,10">
<Label x:Name="LabelDayTitle1"
Content="Scrap Top10 (sorted by A2C number)"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"/>
<lvc:CartesianChart x:Name="ChartDayA2C"
Series="{Binding SeriesCollectionDayA2C}"
LegendLocation="Bottom" MinHeight="280">
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="Component scrap (Top10 A2C numbers) "
Labels="{Binding LabelsDayA2C}"
Separator="{x:Static lvc:DefaultAxes.CleanSeparator}" />
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="Usage"
LabelFormatter="{Binding FormatterDayA2C}">
</lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
<Label x:Name="LabelDayTitle2"
Content="Scrap Top10 (sorted by shape) "
HorizontalContentAlignment="Center"
HorizontalAlignment="Center"/>
<lvc:CartesianChart x:Name="ChartDayShape"
Series="{Binding SeriesCollectionDayShape}"
LegendLocation="Bottom"
MinHeight="280">
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="Component scrap (Top10 Shapes)"
Labels="{Binding LabelsDayShape}"
Separator="{x:Static lvc:DefaultAxes.CleanSeparator}" />
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="Usage"
LabelFormatter="{Binding FormatterDayShape}">
</lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
</StackPanel>
</ScrollViewer>
以及此处的代码:
private void BtDailyShow_Click(object sender, RoutedEventArgs e)
{
SeriesCollectionDayA2C = new SeriesCollection
{
new StackedColumnSeries
{
Values = new ChartValues<double> {4, 5, 6, 8},
StackMode = StackMode.Values, // this is not necessary, values is the default stack mode
DataLabels = true
},
new StackedColumnSeries
{
Values = new ChartValues<double> {200, 5, 6, 7},
StackMode = StackMode.Values,
DataLabels = true
}
};
//adding series updates and animates the chart
SeriesCollectionDayA2C.Add(new StackedColumnSeries
{
Values = new ChartValues<double> { 6, 2, 7 },
StackMode = StackMode.Values
});
//adding values also updates and animates
SeriesCollectionDayA2C[2].Values.Add(4d);
LabelsDayA2C = new[] { "Chrome", "Mozilla", "Opera", "IE" };
FormatterDayA2C = value => value + " Mill";
DataContext = this;
SeriesCollectionDayShape = new SeriesCollection
{
new StackedColumnSeries
{
Values=new ChartValues<double> {20,40,60,80 },
StackMode=StackMode.Values,
DataLabels =true
},
new StackedColumnSeries
{
Values = new ChartValues<double> {100,200,300,400 },
StackMode=StackMode.Values,
DataLabels=true
}
};
SeriesCollectionDayShape.Add(new StackedColumnSeries
{
Values = new ChartValues<double> { 30, 50, 60, 90 },
StackMode = StackMode.Values
});
SeriesCollectionDayShape[2].Values.Add(4d);
LabelsDayShape = new[] { "aaaa", "aass", "eeee", "laka" };
FormmatterDayShape= value => value + " Mill";
DataContext = this;
}
public SeriesCollection SeriesCollectionDayShape { get; set; }
public SeriesCollection SeriesCollectionDayA2C { get; set; }
public string[] LabelsDayA2C { get; set; }
public string[] LabelsDayShape { get; set; }
public Func<double, string> FormatterDayA2C { get; set; }
public Func<double,string> FormatterDayShape { get; set; }
public Func<object, object> FormmatterDayShape { get; set; }
有人可以帮助我哪里出错了吗?
答案 0 :(得分:2)
错误是第一行与
DataContext = this;
如果将其删除,它应该可以工作。在构造函数的末尾设置一次DataContext
。
为什么?
wpf中的绑定是 smart ,它们能够监视属性的变化。为此,您必须为每种类型实现INotifyPropertyChanged,并使用属性名称引发PropertyChanged
事件,该事件的属性值已更改。您可以通过说完所有内容使代码正常工作。
对于永不更改的属性,不需要通知,但这意味着您必须在所有这些属性都设置了其值之后DataContext
之后进行设置。
WPF有很多优化,因此通常在属性设置器中有一个check if value is a new,这会引起很多问题,包括您的情况。解决您问题的另一种可能性是以
DataContext = this; // first time
...
DataContext = null;
DataContext = this; // refresh the value