我必须为一些统计创建wpf应用程序。数据将来自数据库。现在我尝试使用LiveCharts基本堆叠CartesianChart
。我想在主窗口中有两个不同的图表。所以我下载了一个例子,把它作为第一张图表,第二张我复制。
但当我点击运行按钮生成图表时(图表中的数据是一些随机值,稍后它将来自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; }
有人能帮我哪里出了错吗?
错误是的第一行
DataContext = this;
如果移除它,它应该会起作用。在构造函数末尾设置DataContext
一次。
为什么?
wpf中的绑定是智能,它们能够监视属性的更改。为此,您必须为每个类型实现INotifyPropertyChanged,并使用更改了其值的属性的名称引发PropertyChanged
事件。你可以让你的代码工作通过做所有说。
对于从未更改的属性,不需要通知,但这意味着在之后必须设置DataContext
,所有这些属性都设置了值。
WPF有许多优化,所以通常在属性setter内部会检查值是否为新值,这会导致许多问题,包括您的情况。另一种解决问题的可能性是以黑客的方式执行刷新值:
DataContext = this; // first time
...
DataContext = null;
DataContext = this; // refresh the value