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

Stacked Bar = Confused

1 Answer 66 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 12 May 2011, 08:21 PM
Most of the chart types I have used so far are pretty straight forward.  However, I just can't get my mind around how the Stacked Bar chart works.  I have downloaded  a couple of exameples and I still don't get it.  My current needs are very simple, but I can't figure out how to get it working.

I want to show one bar with 5 or 6 stacked segments.  Each segment needs to be a different color and show in the legend along with a description of what it is referencing.  I am using MVVM and plan to bind the control to a property in my ViewModel.  One bar is all I need at this point - no grouping or anything.

The last example I saw had a Stacked Bar chart bound to a ObservableCollection<ObservableCollection<Model>>.  Is a nested list necessary to show a stacked bar chart?  Is there any resource (other than the online help, because I have read and read) that you can point me to where I can learn once and for all how this works?

What should have taken me a few hours is taking me days...

-Scott

1 Answer, 1 is accepted

Sort by
0
Evgenia
Telerik team
answered on 17 May 2011, 01:50 PM
Hi Scott,

The following code snippet demonstrates how to create StackedBar chart without the need of Nested Collections:

public partial class MainPage : UserControl 
    
        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 MainPage() 
        
            InitializeComponent(); 
    
            List<Company> sampleData = new List<Company>(); 
            sampleData.Add(new Company("Physical", 100, 400, 300));
               
            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}; 
                    
            RadChart1.ItemsSource = sampleData; 
                
            RadChart1.SeriesMappings.Add(seriesMapping);
        }     
    
    }

The chart is bound using  a list of business objects. List has one element (that means you'll have one stacked bar). In order to map to such kind of business objects, you have to create a new instance of SeriesMapping. For more information please take a look at our documentation. Each SeriesMapping has ItemMapping property. For each ItemMappings the following properties are set - DataPointMember and FieldName which specifies from where the data should be taken. To be able to show custom labels for Axis X tickpoints - chart uses Categories. In above XAML each XCategory is mapped to Name property of the class Company.

Kind regards,
Evgenia
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
Scott
Top achievements
Rank 1
Answers by
Evgenia
Telerik team
Share this question
or