WPF Toolkit (February 2010 release) Chart with Column Series empty when ItemsSource has only one item
I'm having an annoying problem: I have a simple Chart with one ColumnSeries and two axis (a LinearAxis for the dependent value and a DateTimeAxis for the independent one). My ColumnSeries' ItemsSource is bound to a Collection of instances of DataPoint (simple class with only 3 properties: Date, IndependentValue and DependentValue). The Chart displays the columns correctly if the Collection has 2 or more items in it; but if it only has one, it won't show any column.
Any ideas on what's going on?
Here's the XAML (with both standard and WPF Toolkit's namespaces omitted for brevity):
<Window x:Class="Demo.MainWindow" (...) xmlns:local="clr-namespace:Demo">
<Grid>
<Grid.Resources>
<local:DataPointCollection x:Key="DataPointCollection" />
</Grid.Resources>
<ct:Chart Title="Demo">
<ct:ColumnSeries Title="A"
ItemsSource="{StaticResource DataPointCollection}"
IndependentValueBinding="{Binding Date}"
DependentValueBinding="{Binding DependentValue}" />
<ct:Chart.Axes>
<ct:LinearAxis Orientation="Y"
ShowGridLines="True"
Title="Dependent Title" />
<ct:DateTimeAxis Orientation=X"
ShowGridLines="True"
Interval="1"
IntervalType="Days" />
</ct:Chart.Axes>
</ct:Chart>
</Grid>
</Window>
The DataPointCollection class:
using System;
namespace Demo
{
using System.Collections.ObjectModel;
public class DataPointCollection: Collection<DataPoint>
{
public DataPointCollection()
{
Add(new DataPoint { Date = DateTime.Now.Date, DependentValue开发者_高级运维 = 5 });
// Comment next line to see an empty chart:
Add(new DataPoint { Date = DateTime.Now.Date.AddDays(1), DependentValue = 6 });
}
}
}
And the DataPoint class:
using System;
namespace Demo
{
public class DataPoint
{
public DateTime Date { get; set; }
public double DependentValue { get; set; }
}
}
The project is a regular WPF Application (WPF 4).
Thanks in advance.
Glancing at the source code for ColumnSeries
shows that the column width is related to the range of the data, which in your case is zero. Unless you want to fix bugs in the toolkit, the only workaround I can suggest is to surround your single point with dummy points to make it look like only one column is being shown:
var now = DateTime.Now;
Add(new DataPoint { Date = now, DependentValue = 5 });
Add(new DataPoint { Date = now + TimeSpan.FromDays(1), DependentValue = 0 });
Add(new DataPoint { Date = now - TimeSpan.FromDays(1), DependentValue = 0 });
精彩评论