Lews Therin
Lews Therin

Reputation: 10995

How to use AutoSeriesGenerator with MVVM Visiblox

I am following the example at this tutorial: But unfortunately their documentation is so vague I can't get anything working properly.

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

What I am trying to do is use AutoSeriesGenerator to create a new series automatically for me and also add that lineseries to a primary Y Axis. I am hoping some of you have a good experience with Visiblox. Here is how I thought their example should be represented in 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;
            }

Their documentation says targetObject is business object that the IChartSeries is wrapped around. But what I am getting instead of the object is a collection! And that method is always called multiple times, which means it generates a lot of Lineseries. I have tried the CollectionChanged event and it is the same problem. It doesn't get called once but gets called multiple times which doesn't make sense to me.

How can I solve this problem, thanks.

Upvotes: 0

Views: 510

Answers (1)

Lews Therin
Lews Therin

Reputation: 10995

This is quite embarrassing as I was struggling with this all day. Hate it when I post a question then manage to solve it minutes after :O.

I decided to try this and it worked - very slow! :(

In your code behind:

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();
        }
}

In your CustomLineSeries or whatever:

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

I should have made sure that the boundobject is a business object and ta da!

Upvotes: 0

Related Questions