charts legends

2 posts, 0 answers
  1. Mohamed
    Mohamed avatar
    1 posts
    Member since:
    Oct 2017

    Posted 02 Oct Link to this post

    hi, i have a problem in adding legend to my charts, im using charts for xamarin formskindly find my code below.

    var chart = new RadCartesianChart
                    {
                        BindingContext = new DepartmentalViewModel()
                    };
                    
                     
     
     
                    DepartmentalViewModel model = null;
                    await Task.Run(() => { model = new DepartmentalViewModel(); });
                    var departments = model.Data.GroupBy(test => test.Detpartment).Select(grp => grp.First()).ToList();
                    foreach (var dept in departments)
                    {
                        Xamarin.Forms.ToolbarItem item = new Xamarin.Forms.ToolbarItem();
                        item.Text = dept.Detpartment;
                        item.Order = ToolbarItemOrder.Secondary;
                        item.Clicked += DeptFilter_Clicked;
                        this.ToolbarItems.Add(item);
                    }
     
                    ObservableCollection<DepartmentalData>  FilteredData = new ObservableCollection<DepartmentalData>(model.Data.Where(m => m.Detpartment == "OT").OrderBy(m=>m.Year).ToList());
                     
                    var TargetSeries = new BarSeries();
                    var ActualSeries = new LineSeries();
                    CategoricalAxis HorizontalAxis = new CategoricalAxis();
                    NumericalAxis VerticalAxis = new NumericalAxis();
                    HorizontalAxis.ShowLabels = true;
                    VerticalAxis.Minimum=0;
                    VerticalAxis.MajorStep = 5;
                    VerticalAxis.ShowLabels = true;
                    chart.VerticalAxis = VerticalAxis;
                    chart.HorizontalAxis = HorizontalAxis;
     
     
     
                    TargetSeries.ItemsSource = new ObservableCollection<DepartmentalData>();
                    ActualSeries.ItemsSource = new ObservableCollection<DepartmentalData>();
     
                    foreach (var item in FilteredData)
                    {
                        TargetSeries.ItemsSource.Add(item);
                        ActualSeries.ItemsSource.Add(item);
                    }
                     
                    TargetSeries.ValueBinding = new PropertyNameDataPointBinding("Target");
                    TargetSeries.ShowLabels = true;
                    ActualSeries.ShowLabels = true;
                    ActualSeries.DisplayName = "Actual";
                    TargetSeries.DisplayName = "Target";
                    ActualSeries.ValueBinding = new PropertyNameDataPointBinding("Actual");
                    TargetSeries.CategoryBinding = new PropertyNameDataPointBinding("Year");
                    ActualSeries.CategoryBinding = new PropertyNameDataPointBinding("Year");
                     
                    chart.Series.Add(TargetSeries);
                    chart.Series.Add(ActualSeries);
     
     
                    chart.Grid = new CartesianChartGrid();
     
                    chart.Grid.YStripeColor = Color.Black;
                    chart.Grid.YStripeAlternativeColor = Color.Black;
                    chart.Grid.MajorLineThickness = 5;
                    chart.Grid.MajorLineColor = Color.Black;
                    chart.Grid.MajorXLineDashArray = new double[] { 2, 3 };
     
     
     
                    var legend = new RadLegend
                    {
                        LegendProvider = chart,
                        HeightRequest = 200
                        //LegendItemFontColor = Color.DarkGreen,
                        //Orientation = LegendOrientation.Horizontal
                    };
                    ChartTooltipBehavior tooltip = new ChartTooltipBehavior();
                    tooltip.TriggerMode = ToolTipTriggerMode.Tap;
                    chart.Behaviors.Add(tooltip);
     
                     
     
                    Content = chart;
  2. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    501 posts

    Posted 03 Oct Link to this post

    Hi, Mohamed,

    As advised in the Chart Legend article, the legend is used as a separate control. For example:

    <telerikChart:RadPieChart x:Name="pieChart" HeightRequest="300">
        <telerikChart:RadCartesianChart.BindingContext>
            <local:ViewModel />
        </telerikChart:RadCartesianChart.BindingContext>
        <telerikChart:RadPieChart.Series>
            <telerikChart:PieSeries DisplayName="Value" LegendTitleBinding="Category" ItemsSource="{Binding Data1}" ValueBinding="Value"/>
        </telerikChart:RadPieChart.Series>
    </telerikChart:RadPieChart>
    <telerikChart:RadLegend HeightRequest="200"
                            LegendItemFontColor="DarkGreen"
                            LegendItemFontSize="20"
                            LegendProvider="{x:Reference Name=pieChart}"/>

    In your case, I cannot see a point where you are adding the Legend in the visual tree of the page. I have attached a sample for your reference.

    Regards,
    Stefan Nenchev
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top