charts legends

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

    Posted 02 Oct 2017 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;
                    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.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.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.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;
                    Content = chart;
  2. Stefan Nenchev
    Stefan Nenchev avatar
    605 posts

    Posted 03 Oct 2017 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">
            <local:ViewModel />
            <telerikChart:PieSeries DisplayName="Value" LegendTitleBinding="Category" ItemsSource="{Binding Data1}" ValueBinding="Value"/>
    <telerikChart:RadLegend HeightRequest="200"
                            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.

    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