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

Setting stacked bar color

3 Answers 117 Views
Chart
This is a migrated thread and some comments may be shown as answers.
JR
Top achievements
Rank 1
JR asked on 02 Oct 2010, 01:41 PM

Hi,

    I have a staked bar that i would want to set the color of each series base from the value passed to it. For example, for the series definition of a Stacked bar Chart below, say i would like to make the bar for seriesMapping and seriesMapping2 go red if the value of the two series goes below 5 and go green if the value is 5 and over. Note that they're independent meaning seriesMapping can be red while seriesMapping2 is green.
           

            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.SeriesDefinition = new StackedBarSeriesDefinition();
            seriesMapping.SeriesDefinition.ShowItemToolTips = false;
            ItemMapping itemMapping = new ItemMapping();
            itemMapping.DataPointMember = DataPointMember.YValue;
            itemMapping.FieldName = "MyValue1"; 

            seriesMapping.ItemMappings.Add(itemMapping);

            SeriesMapping seriesMapping2 = new SeriesMapping();
            seriesMapping2.SeriesDefinition = new StackedBarSeriesDefinition();
            seriesMapping2.SeriesDefinition.ShowItemToolTips = false;
            seriesMapping2.ItemMappings.Add(new ItemMapping("MyValue2", DataPointMember.YValue));
           
            SeriesMappingCollection map = new SeriesMappingCollection();
            map.Add(seriesMapping);
            map.Add(seriesMapping2);
            MyChart.SeriesMappings.AddRange(map);

3 Answers, 1 is accepted

Sort by
0
JR
Top achievements
Rank 1
answered on 04 Oct 2010, 03:35 AM
Does anyone have an idea on my inquiry?
0
Accepted
Fredrik
Top achievements
Rank 1
answered on 04 Oct 2010, 07:20 AM
Hello !!

Not an expert on this but I had a simular problem that I solved with this http://www.telerik.com/help/silverlight/radchart-how-to-mvvm-support.html .

Hope the link helps

Regards

Fredrik
0
JR
Top achievements
Rank 1
answered on 04 Oct 2010, 04:28 PM
Hi Fredrik, 

    That is one way to implement the update base on the some of the sites i've also visited but since im using an old version of the control i dont have the CreateItemStyleDelegate. As a result we just made 2 templates, one for the Bar of the first series and a second one for the second series, and just bind a member of the class to the template. Below is the part of the template with the binding.

Section of the template: 
                              ....
                                  <Rectangle x:Name="PART_DefiningGeometry"
                                    Height="{TemplateBinding ItemActualWidth}"
                                    Width="{TemplateBinding ItemActualHeight}"
                                    Fill="{Binding DataItem.BarColor2}"  //BarColor2 is the member of my class binded to the Chart
                                    Stroke="Black"
                                    Canvas.Top="{TemplateBinding ItemActualHeight}" >
                                <Rectangle.RenderTransform>
                                    <TransformGroup>
                                        <RotateTransform Angle="-90"/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                              ....

Then set the style to the series mapping.

        seriesMapping.SeriesDefinition = new StackedBarSeriesDefinition() { ItemStyle = Application.Current.Resources["ChartBarStyle2"] as Style };

Marking your reply as the answer since yours is the much cleaner way to implement this. Thanks for the reply appreciate it. :)
Tags
Chart
Asked by
JR
Top achievements
Rank 1
Answers by
JR
Top achievements
Rank 1
Fredrik
Top achievements
Rank 1
Share this question
or