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

charts legends

3 Answers 159 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Mohamed
Top achievements
Rank 1
Mohamed asked on 02 Oct 2017, 10:14 AM

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;

3 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 03 Oct 2017, 01:36 PM
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
0
Salvador Martinez
Top achievements
Rank 1
answered on 24 Mar 2018, 07:33 PM

Hi,

I'm trying to add Legends to a Pie Chart using XAML. Without the Legends, the app works great. When I add the Legend Control, I get an error saying: "Sequence containing no elements." Pointing to Line 1 on my XAML file.

Here is my XAML code:

<ContentPage.Content>
    <telerikChart:RadPieChart x:Name="pieChart" HeightRequest="300"  SelectionPaletteName="LightSelected">
        <telerikChart:RadPieChart.Behaviors>
            <telerikChart:ChartSelectionBehavior DataPointSelectionMode="Single" SeriesSelectionMode="None" />
        </telerikChart:RadPieChart.Behaviors>
        <telerikChart:RadPieChart.Series>
            <telerikChart:PieSeries ItemsSource="{Binding ExceptionsList}"
                                    ShowLabels="true"
                                    DisplayName="Value"
                                    AllowSelect="True"
                                    LegendTitleBinding="Category">
                <telerikChart:PieSeries.ValueBinding>
                    <telerikChart:PropertyNameDataPointBinding  PropertyName="Value"/>
                </telerikChart:PieSeries.ValueBinding>
            </telerikChart:PieSeries>
        </telerikChart:RadPieChart.Series>
    </telerikChart:RadPieChart>
    <telerikChart:RadLegend LegendProvider="{x:Reference Name=pieChart}"
                            HeightRequest="200"
                            LegendItemFontColor="DarkGreen"
                            LegendItemFontSize="20" />
</ContentPage.Content>

 

Am I missing something? A dependencie?

 

Thanks.

Regards

Salvador

 

 

0
Stefan Nenchev
Telerik team
answered on 27 Mar 2018, 11:31 AM
Hello, Salvador,

In which platform is the undesired behavior observed? Did you test with the sample project I have attached in my previous reply? If not, please try running it and consider the same approach from your end as well. You can check all the references in the example itself or review the following link - Chart - Required Telerik Assemblies. It will be great if you can attach a sample that shows your scenario and the undesired effect so we can investigate it.

Have a great rest of the week.

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
Tags
Chart
Asked by
Mohamed
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Salvador Martinez
Top achievements
Rank 1
Share this question
or