如何将AutoSeriesGenerator与MVVM Visiblox一起使用

时间:2012-07-04 14:49:52

标签: wpf visifire visiblox

我正在关注本教程中的示例: 但不幸的是,他们的文档非常模糊,我无法正常工作。

http://www.visiblox.com/blog/posts/2011/06/13/data-binding-with-visiblox-charts/#asg

我要做的是使用AutoSeriesGenerator自动为我创建一个新系列,并将该lineseries添加到主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所包含的业务对象。但是我得到的不是对象而是一个集合!并且该方法总是被多次调用,这意味着它会产生大量的Lineseries。 我尝试过CollectionChanged事件,这是同样的问题。它不会被调用一次但被多次调用,这对我来说没有意义。

我该如何解决这个问题,谢谢。

1 个答案:

答案 0 :(得分:1)

这令我非常尴尬,因为我整天都在苦苦挣扎。当我发布一个问题然后在几分钟后设法解决它时讨厌它: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();
        }
}

在CustomLineSeries或其他任何内容中:

 public IChartSeries CreateSeries(object targetParent, object boundObject)
 {
   if (boundObject is DataPointsCollection)
   {
       LineSeries lineSeries = new LineSeries();
       return lineSeries;
   }
  return null;
}

我应该确保boundobject是一个业务对象并且ta da!