This is a migrated thread and some comments may be shown as answers.

Empty Legend on StackedBar Chart

2 Answers 104 Views
Chart
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 04 Feb 2011, 06:15 PM

I've got a hybrid Chart where the high-level definitions are in XAML, but I build the DataSeries at run-time.  I'm getting a case where I have a Legend on the right, but there are no entries there.  Here's the XAML:

<telerik:RadChart Name="PowerChart" ItemsSource="{Binding}" >
            <telerik:RadChart.DefaultView>
                <telerik:ChartDefaultView>
                    <telerik:ChartDefaultView.ChartArea>
                        <telerik:ChartArea>
                            <telerik:ChartArea.AxisY>
                                <telerik:AxisY Title="Watts"/>
                            </telerik:ChartArea.AxisY>
                            <telerik:ChartArea.AxisX>
                                <telerik:AxisX Title="Time" IsDateTime="True"
                                              Step="5"
                                              LabelStep="1"
                                              DefaultLabelFormat="hh:mm"
                                              StepLabelLevelHeight="10">
                                </telerik:AxisX>
                            </telerik:ChartArea.AxisX>
                        </telerik:ChartArea>
                    </telerik:ChartDefaultView.ChartArea>
                    <telerik:ChartDefaultView.ChartTitle>
                        <telerik:ChartTitle Content="Power"/>
                    </telerik:ChartDefaultView.ChartTitle>
                </telerik:ChartDefaultView>
            </telerik:RadChart.DefaultView>
            <telerik:RadChart.DefaultSeriesDefinition>
                <telerik:StackedBarSeriesDefinition ShowItemLabels="False"/>
            </telerik:RadChart.DefaultSeriesDefinition>
        </telerik:RadChart>

Here's the code-behind that populates the chart via a non-default constructor on a Window:

public PowerChartWindow(DateRangeFilter dateRange, IEnumerable<Circuit> circuitList)
{
    InitializeComponent();
    // Hide the legend and adjust the title if one circuit
    if (circuitList.Count() < 2)
    {
        PowerChart.DefaultView.ChartLegend.Visibility = System.Windows.Visibility.Collapsed;
    }
    else
    {
        PowerChart.DefaultView.ChartLegend.Visibility = System.Windows.Visibility.Visible;
    }
    // Initialize data series
    foreach (Circuit c in circuitList) 
    {
        // Set up the item mappings
        SeriesMapping circuitMapping = new SeriesMapping();
        circuitMapping.ItemMappings.Add(new ItemMapping("Timestamp", DataPointMember.XValue));
        circuitMapping.ItemMappings.Add(new ItemMapping("WattsAllPhases",DataPointMember.YValue));
        circuitMapping.LegendLabel = c.Name;
        circuitMapping.FilterDescriptors.Add(new ChartFilterDescriptor("Circuit.Name", typeof(String), Telerik.Windows.Data.FilterOperator.IsEqualTo, c.Name));
        // Bind the mappings to the series
        PowerChart.SeriesMappings.Add(circuitMapping);
    }
}

Some of the high points:

  • As you see in the XAML, the ItemsMapping is pointed to the DataContext of the Window
  • The DataContext is Collection<LoggedACMeterData>
  • A LoggedACMeter object has, among other properties, a DateTime Timestamp, a double WattsAllPhases, and a Circuit object which has a String Name property.
  • At the moment, the dateRange is ignored.
  • I hand in the IEnumerable<Circuit> to the constructor with the idea of building a DataSeries for each Circuit.
  • To do the above, I use the ChartFilterDescriptor on each DataSeries to only pull records from the DataContext that matches the circuit name.

So the grid is getting set up properly, with the X-Axis correct, ane the Y-Axis labelled properly, and multiple color-coded series.  But the Legend is empty.  Thoughts? 

Attached is a screen shot of what I get.

David Yon

 (on edit: I guess I really mean a SeriesMapping, not a DataSeries as described above)

2 Answers, 1 is accepted

Sort by
0
Accepted
Missing User
answered on 09 Feb 2011, 04:00 PM
Hi David,

By default, the RadChart.DefaultView.ChartLegend is associated with the RadChart.DefaultView.ChartArea. When you predefine the DefaultView of the RadChart, the LegendName property of the ChartArea needs to be set to the name of the newly created ChartLegend. Otherwise, your legend items will not be auto generated. Here you can find more information about the RadChart Legend:
http://www.telerik.com/help/silverlight/radchart-features-chart-legend.html

You can try the following:
<telerik:RadChart Name="PowerChart" ItemsSource="{Binding}" >
    <telerik:RadChart.DefaultView>
        <telerik:ChartDefaultView>
            <telerik:ChartDefaultView.ChartArea>
                <telerik:ChartArea LegendName="ChartLegend1">
                    <telerik:ChartArea.AxisY>
                        <telerik:AxisY Title="Watts"/>
                    </telerik:ChartArea.AxisY>
                    <telerik:ChartArea.AxisX>
                        <telerik:AxisX Title="Time" IsDateTime="True"
                                        Step="5"
                                        LabelStep="1"
                                        DefaultLabelFormat="hh:mm"
                                        StepLabelLevelHeight="10">
                        </telerik:AxisX>
                    </telerik:ChartArea.AxisX>
                </telerik:ChartArea>
            </telerik:ChartDefaultView.ChartArea>
            <telerik:ChartDefaultView.ChartTitle>
                <telerik:ChartTitle Content="Power"/>
            </telerik:ChartDefaultView.ChartTitle>
            <telerik:ChartDefaultView.ChartLegend>
                <telerik:ChartLegend x:Name="ChartLegend1" />
            </telerik:ChartDefaultView.ChartLegend>
        </telerik:ChartDefaultView>
    </telerik:RadChart.DefaultView>
    <telerik:RadChart.DefaultSeriesDefinition>
        <telerik:StackedBarSeriesDefinition ShowItemLabels="False"/>
    </telerik:RadChart.DefaultSeriesDefinition>
</telerik:RadChart>

I hope this will help.

Kind regards,
Polina
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
David
Top achievements
Rank 1
answered on 09 Feb 2011, 05:37 PM
Perfect!  That was the missing link.
Tags
Chart
Asked by
David
Top achievements
Rank 1
Answers by
Missing User
David
Top achievements
Rank 1
Share this question
or