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

Chart labeling problem

1 Answer 58 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Gary Blakely
Top achievements
Rank 1
Gary Blakely asked on 25 Mar 2010, 12:13 AM

The code pasted below produces a chart that looks ok but has two problems:

1. The legend works as a tooltip ok but I can't figure out how to put a label on the two series in the legend.  I've tried everything I can think of.  With the new 2010 version it shows the two colors but has no label.

2. The Category doesn't print on the x axis (just like it doesn't on your "binding to objects totorial")  How can I get the category to print.

I've been through a lot of your chart totorials but didn't find the answer there.  Should I have?
Thanks,
Gary Blakely

Helper class...  
 public class CompareSet
    {
        public CompareSet(string category, double? you, double? others)
        { 
            this.Category = category;
            this.You = you;
            this.Others = others;
        }
        public string Category { get; set; }
        public double? You { get; set; }
        public double? Others { get; set; }
    }

 Code that produces chart...
      private void ProcessPar345Chart(GCService.GCServicePlayerDataClass myPlayerDataClass)
        {
            var Par345data = myPlayerDataClass.par345Data;
            List<CompareSet> data = new List<CompareSet>();
            data.Add(new CompareSet("Par3's", Par345data[0].P3sY, Par345data[0].P3sG));
            data.Add(new CompareSet("Par4's", Par345data[0].P4sY, Par345data[0].P4sG));
            data.Add(new CompareSet("Par5's", Par345data[0].P5sY, Par345data[0].P5sG));
            CreatePar345Chart(data);
        }
        private void CreatePar345Chart(List<CompareSet> data)
        {
            Par345Chart.Width = chartWidth;
            Par345Chart.Height = chartHeight;
            Par345Chart.DefaultView.ChartTitle.Content = "Par 345";
            //The legend takes up too much space. Remove it and make it appear as a tooltip...
            ChartLegend toolTipLegend = new ChartLegend()
            {
                Background = new SolidColorBrush(Colors.Black),
                Height = 100,
                Width = 200,
              
            };
            ChartLegendItem CItem1 = new ChartLegendItem();  //that don't work
            CItem1.Label = "You";
            //toolTipLegend.Items[0].Label = "You";
            //toolTipLegend.Items[1].Label = "Others";
            ToolTip toolTip = new ToolTip()
            {
                Content = toolTipLegend,
                Style = this.LayoutRoot.Resources["CustomToolTipStyle"] as Style,
            };
            ToolTipService.SetToolTip(Par345Chart, toolTip);
            Par345Chart.DefaultView.ChartLegend.Visibility = Visibility.Collapsed;
            Par345Chart.DefaultView.ChartArea.Legend = toolTipLegend;
            Par345Chart.ItemsSource = data;

        }

1 Answer, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 29 Mar 2010, 10:45 AM
Hello Gary,

Generally, if you do not want to use the autogenerated legend items and you want to create them manually instead, you need to set the ChartLegend.UseAutoGeneratedItems to false (and then add the custom legend items to the ChartLegend.Items collection).

However, a better approach would be to use the built-in databinding capabilities of the control like this:
private void CreatePar345Chart(List<CompareSet> data)
{
    RadChart1.DefaultView.ChartTitle.Content = "Par 345";
 
    //The legend takes up too much space. Remove it and make it appear as a tooltip...
    ChartLegend toolTipLegend = new ChartLegend()
    {
        Height = 100,
        Width = 200,
    };
 
    SeriesMapping sm = new SeriesMapping();
    sm.LegendLabel = "You";
    sm.ItemMappings.Add(new ItemMapping("You", DataPointMember.YValue));
 
    SeriesMapping sm2 = new SeriesMapping();
    sm2.LegendLabel = "Others";
    sm2.ItemMappings.Add(new ItemMapping("Others", DataPointMember.YValue));
 
    RadChart1.SeriesMappings.Add(sm);
    RadChart1.SeriesMappings.Add(sm2);
 
    ToolTip toolTip = new ToolTip()
    {
        Content = toolTipLegend,
        Style = this.LayoutRoot.Resources["CustomToolTipStyle"] as Style,
    };
    ToolTipService.SetToolTip(RadChart1, toolTip);
    RadChart1.DefaultView.ChartLegend.Visibility = Visibility.Collapsed;
    RadChart1.DefaultView.ChartArea.Legend = toolTipLegend;
    RadChart1.ItemsSource = data;
}

Hope this helps.


All the best,
Freddie
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
Tags
Chart
Asked by
Gary Blakely
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Share this question
or