Configure LegendItems using RadLegend and RadPieChart

3 posts, 1 answers
  1. Alberto
    Alberto avatar
    26 posts
    Member since:
    Nov 2012

    Posted 03 Oct 2013 Link to this post

    Hi, 

    I am trying to configure the legend items shown in a RadLegend control associated to a RadPieChart.

    This is how I load the data:

    <Window
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="WpfApplication1.MainWindow"
            Title="MainWindow" Height="594.024" Width="902.888">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
             
            <telerik:RadPieChart Name="PieChartDemo" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="543" Width="428"/>
     
            <telerik:RadLegend x:Name="PieLegend"
                                       Grid.Column="1" Items="{Binding LegendItems, ElementName=PieChartDemo}"/>
             
        </Grid>
    </Window>

    public MainWindow()
            {
                InitializeComponent();
                FillPieChart();
            }
     
            void FillPieChart()
            {
                DataTable dtData = new DataTable("DATA");
     
                dtData.Columns.Add(new DataColumn("Name", typeof(string)));
                dtData.Columns.Add(new DataColumn("Value", typeof(double)));
     
                dtData.Rows.Add(new object[] { "Scrap", 5429287.5 });
                dtData.Rows.Add(new object[] { "KWH", 713631 });
                dtData.Rows.Add(new object[] { "Electrode", 327349 });
                dtData.Rows.Add(new object[] { "Alloys", 2372722.3 });
                dtData.Rows.Add(new object[] { "Fluxes", 317157.65 });
                dtData.Rows.Add(new object[] { "Labor", 300084.1 });
     
                PieChartDemo.Palette = ChartPalettes.Arctic;
     
                PieSeries pieSer = new PieSeries();
                pieSer.ShowLabels = true;
     
                foreach (DataRow drCost in dtData.Rows)
                {
                    pieSer.DataPoints.Add(new PieDataPoint()
                    {
                        Value = double.Parse(drCost["Value"].ToString()),
                        Label = string.Format("{0}\n{1:N} K", drCost["Name"].ToString(), double.Parse(drCost["Value"].ToString()) / 1000.00)
                    }
                                        );
                }
     
                pieSer.LabelDefinitions.Add(new ChartSeriesLabelDefinition() { Margin = new Thickness(-5, 0, 0, 0) });
                pieSer.AngleRange = new AngleRange(270, 360);
     
                pieSer.LegendSettings = new DataPointLegendSettings();
     
                PieChartDemo.Series.Clear();
                PieChartDemo.Series.Add(pieSer);
            }


    I am creating a PieSeries and adding some PieDataPoint. For each data point I add the value and the label, and I think the RadLegend takes this label to show its items. Am I correct?

    In the beginning I wasn't using the RadLegend so I was showing the name and the value in the chart, assigning both values to Label:

    pieSer.DataPoints.Add(new PieDataPoint()
                   {
                       Value = double.Parse(drCost["Value"].ToString()),
                       Label = string.Format("{0}\n{1:N} K", drCost["Name"].ToString(), double.Parse(drCost["Value"].ToString()) / 1000.00)
                   }

    And then I added the RadLegend Control and it shows the same values.

    How can I configure the RadLegend control to show only the names if I change the Label shown in the RadPieChart to show only the values?




  2. Answer
    Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 08 Oct 2013 Link to this post

    Hi Alberto,

    You will need to bind the PieSeries in order to be able specify exactly what should be displayed in RadLegend and what in the series labels (the other option is to create the legend items manually).

    Both the ChartSeriesLabelDefinition class and DataPointLegendSettings class provide DataPointBindings (namely through the Binding and TitleBinding properties). You can use GeneriDataPointBinding to bind the pie series labels and the legend item labels to your data table.

    I have attached a sample project to demonstrate.
     
    Regards,
    Petar Kirov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Alberto
    Alberto avatar
    26 posts
    Member since:
    Nov 2012

    Posted 09 Oct 2013 Link to this post

    Hi Petar,

    Thanks for sample project it was very helpful, that's what I wanted to accomplish.

    Regards,

    Alberto
Back to Top