Search code examples
wpfsilverlightc#-4.0silverlight-toolkit

Silverlight : How to bind a result of loadoperation to LineSeries chart which contains few null values


I have bound a my chart to a result of load operation. The result returned by the load operation returns complex type with values for some of the object's properties is null.

So I am getting an error in my silver light application.

The code of my XAML is as follows:

<Grid x:Name="LayoutRoot">
        <toolkit:Chart Name="historicalTotalChart" Title="Chart Title">
            <toolkit:LineSeries >

            </toolkit:LineSeries>
        </toolkit:Chart>
    </Grid>

Code of in my CS file is as follows:

LoadOperation<GetHistoricalTotalReport_Result> hisTotalsLoadOp =
                context.Load(context.GetHistoricalToalReportQuery(tableName, sD, startDate, endDate));

            LineSeries line = new LineSeries();
            line.ItemsSource = hisTotalsLoadOp.Entities;

            //line.DependentValuePath ="rep_date";
            //line.IndependentValuePath = "expr1";
            line.DependentValueBinding = new Binding("[rep_date]");
            line.IndependentValueBinding = new Binding("[expr1]");
            line.Title = "Historical Totals";
            historicalTotalChart.Series.Add(line);

Can any one say how can I over come this error?

rep_date, expr1 are the properties with in my complex type GetHistoricalTotalReport_Result. I am I binding to the properties correctly?

Any help much appreciated.

Thanks


Solution

  • There are 3 possible issues in your application:

    1. Your bindings shouldn't contain square brackets, they are for arrays and dictionaries. Also I would rather use the properties Dependent/IndependentValuePath, you shouldn't comment them, they are completely correct.
    2. The DependentValuePath property must be of a numerical data type, because it is situated on the Y-axis. In your example the rep_date property could be of the double type.
    3. The IndependentValuePath is situated on the X-axis and could be of any type but the values can't contain nulls. Really, I have no idea where a null value can be displayed on the X-axis, it doesn't make sense.

    Here is the example of the correct code which works fine:

    LineSeries line = new LineSeries();
    line.ItemsSource = new[]
    {
        new ItemViewModel{expr1 = "Item1", rep_date = 25},
        new ItemViewModel{expr1 = "Item2", rep_date = null},
        new ItemViewModel{expr1 = "Item3", rep_date = 31},
        new ItemViewModel{expr1 = "Item4", rep_date = null},
    };
    
    line.DependentValuePath = "rep_date";
    line.IndependentValuePath = "expr1";
    line.Title = "Historical Totals";
    historicalTotalChart.Series.Add(line);
    

    The next code will not work:

    line.ItemsSource = new[]
    {
        new ItemViewModel{expr1 = null, rep_date = 25} //wrong, x-value can't be null
    }
    

    But you can filter your entities before displaying them:

    line.ItemsSource = hisTotalsLoadOp.Entities.Where(e => e.expr1 != null).ToList();