1

I am using charts of winrt xaml toolkit in my app. In certain screens chart works fine but in certain screens it now throws UnhandledException (earlier it was working fine. I didn't update ANYHTING). I am not getting much exception details. What's wrong with the chart?

Exception details

System.Exception at Windows.UI.Xaml.UIElement.Measure(Size availableSize) at WinRTXamlToolkit.Controls.DataVisualization.Charting.Primitives.EdgePanel.MeasureOverride(Size constraint) at Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)

"Error HRESULT E_FAIL has been returned from a call to a COM component."

Here's code to regenerate it. I am doing all the stuff in code behind because in real app there would be dynamic number of charts.

XAML

<Page.Resources>
    <Style TargetType="charting:ColumnDataPoint" x:Key="MyColumnDataPointStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="charting:ColumnDataPoint">
                    <Grid>
                        <ToolTipService.ToolTip>
                            <ContentControl>
                                <TextBlock Text="{Binding Tooltip}" TextAlignment="Center" />
                            </ContentControl>
                        </ToolTipService.ToolTip>
                        <Rectangle Fill="{Binding Color}" Stroke="{Binding Color}" StrokeThickness="3" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <charting:Chart x:Name="ChartSalesRevenue" Margin="70,0" /> 
</Grid>

C#

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ChartSalesRevenue.Series.Clear();
    ChartSalesRevenue.Series.Add(new StackedColumnSeries());
    Binding DependentBinding = new Binding();
    Binding IndependentBinding = new Binding();
    IndependentBinding.Converter = new DateToStringConverter();
    SeriesDefinition series;

    series = new SeriesDefinition();
    series.DataPointStyle = this.Resources["MyColumnDataPointStyle"] as Style;
    DependentBinding.Path = new PropertyPath("Units");
    IndependentBinding.Path = new PropertyPath("Begin_Date");
    series.DependentValueBinding = DependentBinding;
    series.IndependentValueBinding = IndependentBinding;
    series.ItemsSource = GetDataItems();

    ((StackedColumnSeries)ChartSalesRevenue.Series[0]).SeriesDefinitions.Add(series);
}

private List<DataItem> GetDataItems()
{
    return new List<DataItem> 
    {
        new DataItem("Tooltip 1", new SolidColorBrush(Colors.Red), 5, DateTime.Now.AddDays(-1)),
        new DataItem("Tooltip 2", new SolidColorBrush(Colors.Violet), 3, DateTime.Now.AddDays(1)),
        new DataItem("Tooltip 3", new SolidColorBrush(Colors.PaleGoldenrod), 22, DateTime.Now.AddDays(-2)),
        new DataItem("Tooltip 4", new SolidColorBrush(Colors.DarkBlue), 15, DateTime.Now.AddDays(2)),
        new DataItem("Tooltip 5", new SolidColorBrush(Colors.DeepPink), 9, DateTime.Now),
    };
}

public class DateToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string Language)
    {
        return ((DateTime)value).ToString("MMM dd, yyyy", System.Globalization.CultureInfo.InvariantCulture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

public class DataItem
{
    public DataItem(string _Tooltip, SolidColorBrush _Color, int _Units, DateTime _Begin_Date)
    {
        Tooltip = _Tooltip;
        Color = _Color;
        Units = _Units;
        Begin_Date = _Begin_Date;
    }
    public string Tooltip { get; set; }
    public SolidColorBrush Color { get; set; }
    public int Units { get; set; }
    public DateTime Begin_Date { get; set; }
}
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
  • I ran in to the same problem once , Be sure to assign a Min width and min height property for the chart component or its parent. Check this [Help link](http://winrtxamltoolkit.codeplex.com/discussions/429746) for another solution . I have adopted both to create dynamic charts at run time . – Rajmohan Kathiresan Nov 20 '13 at 09:11

1 Answers1

1

I encountered the same problem and found a very easy solution.

XAML

    <charting:Chart x:Name="ChartSalesRevenue" Margin="70,0" >
        <charting:Stacked100BarSeries>
            <charting:SeriesDefinition
                    DependentValuePath="Value"
                    IndependentValuePath="Name"
                    IsTapEnabled="True"
                    Title="" />
        </charting:Stacked100BarSeries>
    </charting:Chart>

C#

First in the page,add the Series("charting:Stacked100BarSeries").Before you add Series for Chart Control,Clear the Series .

eg:

                ChartSalesRevenue.Series.Clear();
                ChartSalesRevenue.Series.Add(...);
fengs
  • 11
  • 2
  • Your solution worked but the colors are not showing in legends. http://imgur.com/D435VXR – Farhan Ghumra Feb 14 '14 at 09:40
  • "Error HRESULT E_FAIL has been returned from a call to a COM component." would happen if series is Stacked100AreaSeries or Stacked100LineSeries or StackedAreaSeries or StackedLineSeries,but ColumnChartSeries is OK in my code. – fengs Feb 17 '14 at 03:24