我遵循本教程中的示例:但不幸的是,他们的文档非常模糊,我无法正常工作。
http://www.visiblox.com/blog/posts/2011/06/13/data-binding-with-visiblox-charts/#asg
我正在尝试做的是使用自动系列生成器自动为我创建一个新系列,并将该线系列添加到主 Y 轴。我希望你们中的一些人对 Visiblox 有很好的体验。以下是我认为他们的例子应该用 C# 表示的方式:
<local:WeatherStationsData x:Key="dataCollection">
<local:WeatherStationTemperature>
<local:HourTemperature Hour="1" Temperature="10" />
<local:HourTemperature Hour="2" Temperature="14" />
</local:WeatherStationTemperature>
<local:WeatherStationTemperature>
<local:HourTemperature Hour="1" Temperature="20" />
<local:HourTemperature Hour="2" Temperature="19" />
</local:WeatherStationTemperature>
</local:WeatherStationsData>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<charts:Chart x:Name="Chart"
charts:AutoSeriesGenerator.XValuePath="Hour"
charts:AutoSeriesGenerator.YValuePath="Temperature"
charts:AutoSeriesGenerator.ChartSeriesProvider="LineSeries"
charts:AutoSeriesGenerator.ItemsSource="{StaticResource dataCollection}">
<charts:Chart.XAxis>
<charts:LinearAxis />
</charts:Chart.XAxis>
<charts:Chart.YAxis>
<charts:LinearAxis />
</charts:Chart.YAxis>
</charts:Chart>
//X and Y points
public class ChartDataPoint
{
public DateTime X { get; set; }
public float Y { get; set; }
public ChartDataPoint(DateTime X, float Y)
{
this.X = X;
this.Y = Y;
}
}
//represents a dataseries - collection of datapoints
public class DataPointsCollection : ObservableCollection<ChartDataPoint>
{
}
//the "dataseries"
public class DataSeries : ObservableCollection<DataPointsCollection>
{
}
But it isn't working as expected.
public IChartSeries CreateSeries(object targetParent, object boundObject)
{
LineSeries lineSeries = null;
if (targetParent is LineSeries)
{
lineSeries = new LineSeries();
lineSeries.YAxis = new LinearAxis();
AxisCollection.Add(lineSeries.YAxis);
}
return lineSeries;
}
他们的文档说targetObject是IChartSeries所包裹的业务对象。但是我得到的不是对象是一个集合!并且该方法总是被多次调用,这意味着它会生成大量线序列。我已经尝试了 CollectionChanged 事件,这是同样的问题。它不会被调用一次,但被调用多次,这对我来说没有意义。
我该如何解决这个问题,谢谢。
这很尴尬,因为我整天都在为此苦苦挣扎。讨厌我发布一个问题,然后在 :O 几分钟后设法解决它。
我决定尝试这个,它奏效了 - 非常慢! :(
在代码隐藏中:
private void Series_CollectionChanged(object sender,
NotifyCollectionChangedEventArgs e)
{
if(e.Action == NotifyCollectionChangedAction.Add)
{
var lineSeries = (LineSeries) MultiChart.Series[e.NewStartingIndex];
if (!collection.Contains(lineSeries.YAxis))
{
lineSeries.YAxis = new LinearAxis();
collection.Add(lineSeries.YAxis);
}
}
else if(e.Action == NotifyCollectionChangedAction.Reset )
{
collection.Clear();
}
}
在您的定制系列或其他任何产品中:
public IChartSeries CreateSeries(object targetParent, object boundObject)
{
if (boundObject is DataPointsCollection)
{
LineSeries lineSeries = new LineSeries();
return lineSeries;
}
return null;
}
我应该确保绑定对象是一个业务对象和 ta da!