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

StackedBar Legend item not showing.

3 Answers 92 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Kristjan Einarsson
Top achievements
Rank 1
Kristjan Einarsson asked on 26 Jan 2011, 10:10 AM
Hi, I have a problem when populating stacked bar charts. My code is following:
XMAL:
<telerik:RadChart x:Name="chart" PaletteBrushesRepeat="False" telerik:Theming.Theme="Summer">
 </telerik:RadChart>

C#:
chart.SeriesMappings.Clear();
            chart.DefaultView.ChartArea.DataSeries.Clear();
            chart.DefaultView.ChartLegend.UseAutoGeneratedItems = true;
            chart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.SeriesLabel;
             
            for (int i = 0; i < model.Captions.Count; i++ )
            {
                SeriesMapping sm = new SeriesMapping();
                StackedBarSeriesDefinition dd = new StackedBarSeriesDefinition();
                 
                dd.StackGroupName = "Stack1";
                sm.SeriesDefinition = dd;
                sm.LegendLabel = "Stacked Bar " + i;
                sm.CollectionIndex = i;
                 
                ItemMapping im1 = new ItemMapping();
                im1.DataPointMember = DataPointMember.YValue;
                sm.ItemMappings.Add(im1);
                chart.SeriesMappings.Add(sm);
            }
 
            List<double[]> itemsSource = new List<double[]>();
            for (int i = 0; i < model.Children.Count; i++)
            {
                double[] d =  model.Children[i].Values.Select(s => s.AdjustedValue).ToArray();
                itemsSource.Add(d);
            }

I'm not sure where I'm going wrong here but I'd thought "sm.LegendLabel = "Stacked Bar " + i; " would provide my with legend items.

Best regards
Kristján.

3 Answers, 1 is accepted

Sort by
0
Kristjan Einarsson
Top achievements
Rank 1
answered on 31 Jan 2011, 12:46 PM
anyone ?
0
Accepted
Evgenia
Telerik team
answered on 31 Jan 2011, 05:01 PM
Hello Kristjan,

To be able to create Stacked Bar chart databound using Manual Series Mappings there are some things you should have in mind:
Each SeriesMapping has ItemMappings property. For each ItemMapping the following properties are set - DataPointMember and FieldName which specifies from where the data should be taken.
The following sample code creates Stacked bar chart where the data for the chart is stored in List of business objects:
public class Company 
        
            public string Name { get; set; } 
            public double Value1 { get; set; } 
    
            public double Value2 { get; set; } 
            public double Value3 { get; set; } 
    
            public Company(string name, double value1, double value2, double value3) 
            
                Name = name; 
                Value1 = value1; 
                Value2 = value2; 
                Value3 = value3; 
            
        
    
        public MainWindow() 
        
            InitializeComponent(); 
    
            List<Company> sampleData = new List<Company>(); 
            sampleData.Add(new Company("Physical", 100, 400, 300)); 
            sampleData.Add(new Company("Virtualized", 600, 200, 240)); 
               
            SeriesMapping seriesMapping = new SeriesMapping { LegendLabel = "Series 1" }; 
            seriesMapping.ItemMappings.Add(new ItemMapping("Value1", DataPointMember.YValue)); 
            seriesMapping.ItemMappings.Add(new ItemMapping("Name", DataPointMember.XCategory)); 
            seriesMapping.SeriesDefinition = new  StackedBarSeriesDefinition() { ShowItemLabels = true}; 
    
            SeriesMapping seriesMapping2 = new SeriesMapping { LegendLabel = "Series 2" }; 
            seriesMapping2.ItemMappings.Add(new ItemMapping("Value2", DataPointMember.YValue)); 
            seriesMapping2.ItemMappings.Add(new ItemMapping("Name", DataPointMember.XCategory)); 
            seriesMapping2.SeriesDefinition = new StackedBarSeriesDefinition() {ShowItemLabels = true}; 
    
            SeriesMapping seriesMapping3 = new SeriesMapping { LegendLabel = "Series 3" }; 
            seriesMapping3.ItemMappings.Add(new ItemMapping("Value3", DataPointMember.YValue)); 
            seriesMapping3.ItemMappings.Add(new ItemMapping("Name", DataPointMember.XCategory)); 
            seriesMapping3.SeriesDefinition = new StackedBarSeriesDefinition() { ShowItemLabels = true }; 
                
            RadChart1.ItemsSource = sampleData; 
            RadChart1.DefaultView.ChartArea.AxisY.AutoRange = false; 
            RadChart1.DefaultView.ChartArea.AxisY.AddRange(0,1200,100); 
                
            RadChart1.SeriesMappings.Add(seriesMapping); 
            RadChart1.SeriesMappings.Add(seriesMapping2); 
            RadChart1.SeriesMappings.Add(seriesMapping3); 
        }

If you need to add the Legend items manually you can follow our help topic (Manual Legend Items Generation).

Regards,
Evgenia
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Kristjan Einarsson
Top achievements
Rank 1
answered on 02 Feb 2011, 11:55 AM
Ok, thanks, I tried your code it did not work until I created an new clean Telerik project.
The problem was that the project was referring to some older Telerik dll's in my project.

Thanks for the help :)
Tags
Chart
Asked by
Kristjan Einarsson
Top achievements
Rank 1
Answers by
Kristjan Einarsson
Top achievements
Rank 1
Evgenia
Telerik team
Share this question
or