Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I remove the legend from a Silverlight 4 PieSeries Pie Chart

Posted on 2011-04-20
5
Medium Priority
?
1,849 Views
Last Modified: 2013-11-12
I have created a Chart using a PieSeries in Silverlight 4 via c# code.  I need to remove the legend (via code).  Nothing I have tried has worked, any suggestions?
0
Comment
Question by:chester427
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:politex
ID: 35439563
Hi, it's dirty hack, but works:
myChart.LegendStyle.Setters.Add(new Setter(HeightProperty, 0));
myChart.LegendStyle.Setters.Add(new Setter(WidthProperty, 0));

Open in new window

0
 

Author Comment

by:chester427
ID: 35439820
Thanks for responding Politex, but I get a null reference exception.  I had tried something similar with the PieSeries that is attached to the chart by creating a stye and setting the LegendItem.HeightProperty and LegenItem.WidthProperty to 0 (with a setter) and then assigning the style object to the PieSeries.  This worked fine, but only removed(hid) the items from the legend - it still left an empty legend - basically a gray box.  Any suggestions?
0
 
LVL 3

Accepted Solution

by:
politex earned 2000 total points
ID: 35440097
It's working sample, Legent class is in the System.Windows.Controls.DataVisualization namespace. Let me know if you have any questions
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;


namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        public List<MyItem> data = new List<MyItem>();
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Chart myChart = new Chart();            
            PieSeries pe = new PieSeries();
            myChart.Series.Add(pe);
            data.Add(new MyItem { Name = "item1", Val = 3 });
            data.Add(new MyItem { Name = "item2", Val = 5 });
            data.Add(new MyItem { Name = "item3", Val = 4 });
            
            pe.DependentValueBinding = new Binding("Val");
            pe.ItemsSource= data;
            myChart.LegendStyle= new Style(typeof(Legend));
            myChart.LegendStyle.Setters.Add(new Setter(HeightProperty, 0));
            myChart.LegendStyle.Setters.Add(new Setter(WidthProperty, 0));
            LayoutRoot.Children.Add(myChart);
        }
      
    }
    public class MyItem
    {
        public string Name { get; set; }
        public int Val { get; set; }
    }
}

Open in new window

0
 

Author Comment

by:chester427
ID: 35440359
Thanks for posting the example.  I was just missing a reference.  After adding it -it all fell into place.

Super!
0
 

Author Closing Comment

by:chester427
ID: 35440374
I appreciated the help I received.  Politex, thanks for the help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question