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

Stacked Bar items float

5 Answers 128 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Ken Spencer
Top achievements
Rank 1
Ken Spencer asked on 14 Sep 2009, 03:44 PM
Below is an example chart, all declarative XAML that I am prototyping, can someone tell me a few things...

  • Why do the values represented by one of the stack elements float in space instead of stacking onto the other bars in their column?
  • How can I get my XValues (which are years) to sort ascending left to righ

    <UserControl 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Class="SilverlightApplication3.MainPage" 
        xmlns:control="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Charting" 
        xmlns:chart="clr-namespace:Telerik.Windows.Controls.Charting;assembly=Telerik.Windows.Controls.Charting" 
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
        Width="1024" Height="768"
        <Grid x:Name="LayoutRoot" Background="White"
            <control:RadChart x:Name="RadChart1" Grid.Row="0" Grid.Column="0"
                <control:RadChart.DefaultView> 
                    <chart:ChartDefaultView > 
                        <chart:ChartDefaultView.ChartArea> 
                            <chart:ChartArea LegendName="CustomLegend"
                                <chart:ChartArea.AnimationSettings> 
                                    <chart:AnimationSettings/> 
                                </chart:ChartArea.AnimationSettings> 
                                <chart:ChartArea.AxisX> 
                                    <chart:AxisX MajorGridLinesVisibility="Visible" LabelRotationAngle="90" StripLinesVisibility="Visible" Step="1" LabelStep="1" /> 
                                </chart:ChartArea.AxisX> 
                                <chart:ChartArea.AxisY> 
                                    <chart:AxisY MajorGridLinesVisibility="Visible"  
                                                   MinorTicksVisibility="Visible" 
                                                   Title="Item Count" /> 
                                </chart:ChartArea.AxisY> 
                                <chart:ChartArea.DataSeries > 
                                    <chart:DataSeries Label="US"  > 
                                        <chart:DataSeries.Definition> 
                                            <chart:StackedBarSeriesDefinition ShowItemLabels="False" ShowItemToolTips="True"/> 
                                        </chart:DataSeries.Definition> 
                                        <chart:DataPoint YValue="1" XValue="2001"/> 
                                        <chart:DataPoint YValue="2" XValue="2002"/> 
                                        <chart:DataPoint YValue="4" XValue="2003"/> 
                                        <chart:DataPoint YValue="3" XValue="2004"/> 
                                        <chart:DataPoint YValue="6" XValue="2005"/> 
                                        <chart:DataPoint YValue="9" XValue="2006"/> 
                                        <chart:DataPoint YValue="18" XValue="2007"/> 
                                        <chart:DataPoint YValue="9" XValue="2008"/> 
                                    </chart:DataSeries> 
                                    <chart:DataSeries Label="AU" > 
                                        <chart:DataSeries.Definition> 
                                            <chart:StackedBarSeriesDefinition ShowItemLabels="False" ShowItemToolTips="True"/> 
                                        </chart:DataSeries.Definition> 
                                        <chart:DataPoint YValue="1" XValue="1987"/> 
                                        <chart:DataPoint YValue="1" XValue="1988"/> 
                                        <chart:DataPoint YValue="2" XValue="1989"/> 
                                        <chart:DataPoint YValue="1" XValue="1996"/> 
                                        <chart:DataPoint YValue="3" XValue="2000"/> 
                                        <chart:DataPoint YValue="2" XValue="2002"/> 
                                        <chart:DataPoint YValue="1" XValue="2003"/> 
                                        <chart:DataPoint YValue="1" XValue="2007"/> 
                                    </chart:DataSeries> 
                                    <chart:DataSeries Label="CA" > 
                                        <chart:DataSeries.Definition> 
                                            <chart:StackedBarSeriesDefinition ShowItemLabels="False" ShowItemToolTips="True"/> 
                                        </chart:DataSeries.Definition> 
                                        <chart:DataPoint YValue="1" XValue="1978"/> 
                                        <chart:DataPoint YValue="1" XValue="1985"/> 
                                        <chart:DataPoint YValue="1" XValue="1994"/> 
                                        <chart:DataPoint YValue="2" XValue="2004"/> 
                                        <chart:DataPoint YValue="1" XValue="2006"/> 
                                        <chart:DataPoint YValue="5" XValue="2007"/> 
                                        <chart:DataPoint YValue="2" XValue="2008"/> 
                                    </chart:DataSeries> 
                                    <chart:DataSeries Label="DE" > 
                                        <chart:DataSeries.Definition> 
                                            <chart:StackedBarSeriesDefinition ShowItemLabels="False" ShowItemToolTips="True"/> 
                                        </chart:DataSeries.Definition> 
                                        <chart:DataPoint YValue="1" XValue="1976"/> 
                                        <chart:DataPoint YValue="1" XValue="1978"/> 
                                        <chart:DataPoint YValue="1" XValue="1994"/> 
                                        <chart:DataPoint YValue="1" XValue="1996"/> 
                                        <chart:DataPoint YValue="1" XValue="1996"/> 
                                        <chart:DataPoint YValue="1" XValue="1998"/> 
                                        <chart:DataPoint YValue="1" XValue="2002"/> 
                                        <chart:DataPoint YValue="1" XValue="2004"/> 
                                        <chart:DataPoint YValue="1" XValue="2006"/> 
                                    </chart:DataSeries> 
                                    <chart:DataSeries Label="EP" > 
                                        <chart:DataSeries.Definition> 
                                            <chart:StackedBarSeriesDefinition ShowItemLabels="False" ShowItemToolTips="True"/> 
                                        </chart:DataSeries.Definition> 
                                        <chart:DataPoint YValue="1" XValue="2001"/> 
                                        <chart:DataPoint YValue="2" XValue="2002"/> 
                                        <chart:DataPoint YValue="2" XValue="2003"/> 
                                        <chart:DataPoint YValue="5" XValue="2004"/> 
                                        <chart:DataPoint YValue="1" XValue="2005"/> 
                                        <chart:DataPoint YValue="4" XValue="2006"/> 
                                        <chart:DataPoint YValue="4" XValue="2006"/> 
                                        <chart:DataPoint YValue="1" XValue="2007"/> 
                                        <chart:DataPoint YValue="19" XValue="2008"/> 
                                        <chart:DataPoint YValue="3" XValue="2008"/> 
                                        <chart:DataPoint YValue="2" XValue="2009"/> 
                                        <chart:DataPoint YValue="1" XValue="2009"/> 
                                    </chart:DataSeries> 
                                    <chart:DataSeries Label="ES" > 
                                        <chart:DataSeries.Definition> 
                                            <chart:StackedBarSeriesDefinition ShowItemLabels="False" ShowItemToolTips="True"/> 
                                        </chart:DataSeries.Definition> 
                                        <chart:DataPoint YValue="1" XValue="1996"/> 
                                        <chart:DataPoint YValue="2" XValue="1998"/> 
                                        <chart:DataPoint YValue="1" XValue="2000"/> 
                                        <chart:DataPoint YValue="1" XValue="2002"/> 
                                        <chart:DataPoint YValue="1" XValue="2004"/> 
                                        <chart:DataPoint YValue="3" XValue="2005"/> 
                                        <chart:DataPoint YValue="1" XValue="2006"/> 
                                        <chart:DataPoint YValue="2" XValue="2007"/> 
                                        <chart:DataPoint YValue="1" XValue="2008"/> 
                                    </chart:DataSeries>          
                                    <chart:DataSeries Label="FR" > 
                                        <chart:DataSeries.Definition> 
                                            <chart:StackedBarSeriesDefinition ShowItemLabels="False" ShowItemToolTips="True"/> 
                                        </chart:DataSeries.Definition> 
                                        <chart:DataPoint YValue="1" XValue="1976"/> 
                                        <chart:DataPoint YValue="1" XValue="1995"/> 
                                        <chart:DataPoint YValue="1" XValue="1998"/> 
                                        <chart:DataPoint YValue="1" XValue="1999"/> 
                                        <chart:DataPoint YValue="1" XValue="2000"/> 
                                        <chart:DataPoint YValue="1" XValue="2001"/> 
                                    </chart:DataSeries>                                                                                      
                                </chart:ChartArea.DataSeries> 
                            </chart:ChartArea> 
                        </chart:ChartDefaultView.ChartArea> 
                        <chart:ChartDefaultView.ChartLegend> 
                            <chart:ChartLegend x:Name="CustomLegend" UseAutoGeneratedItems="True"   /> 
                        </chart:ChartDefaultView.ChartLegend> 
                        <chart:ChartDefaultView.ChartTitle> 
                            <chart:ChartTitle> 
                                <TextBlock Text="Values Over Time"/> 
                            </chart:ChartTitle> 
                        </chart:ChartDefaultView.ChartTitle> 
                    </chart:ChartDefaultView> 
                </control:RadChart.DefaultView> 
            </control:RadChart> 
        </Grid> 
    </UserControl> 

5 Answers, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 17 Sep 2009, 09:03 AM
Hello Ken,

I am afraid this scenario is not supported by RadChart out of the box. The bars float as there is actually another bar in another series that is taken into account . Unfortunately, in strict mode that bar might be at another position along the X axis, so the problem arises.

Still, there is a simple workaround -- add zero items to the series, so that all the series have items for all X values. Here is an example:

 foreach (DataSeries series in RadChart1.DefaultView.ChartArea.DataSeries) 
    for (int i = 1975; i < 2010; i++) 
        if (!series.Any(item => item.XValue == i)) 
        { 
            DataPoint p = new DataPoint(i, 0); 
            series.Insert(i - 1975, p); 
        } 

Of course, you may adjust the code to avoid the hard-coded values. In addition, your markup shows multiple items with the same XValue in the same series, which is not supported either.

Best regards,
Ves
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Nick Wood
Top achievements
Rank 1
answered on 17 May 2010, 01:23 PM
Hi There

I have the same issue with data comming from a solution that is built from the aggregates example in the demo. here is the code:
SeriesMapping mapping1 = new SeriesMapping(); 
                ChartAggregateFunction aggFunct = (ChartAggregateFunction)Enum.Parse(typeof(ChartAggregateFunction), (this.cmbAggregation.SelectedItem as RadComboBoxItem).Content as stringtrue); 
                mapping1.ItemMappings.Add(new ItemMapping(_filterColumnName, DataPointMember.YValue, aggFunct)); 
                mapping1.SeriesDefinition = ChartType(); 
 
                foreach (GroupDescriptor descriptor in RadGridView1.GroupDescriptors) 
                { 
                    mapping1.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor(descriptor.Member)); 
                } 
 
                if (RadGridView1.GroupDescriptors.Count > 1 && (cmbAggregation.SelectedItem as RadComboBoxItem).Content.ToString().ToLower() != "none"
                { 
                    GroupDescriptor lastGroupDescriptor = (GroupDescriptor)RadGridView1.GroupDescriptors[RadGridView1.GroupDescriptors.Count - 1]; 
                    mapping1.ItemMappings.Add(new ItemMapping(lastGroupDescriptor.Member, DataPointMember.XCategory)); 
                } 
 
                RadChart1.SeriesMappings.Add(mapping1); 
                RadChart1.ItemsSource = RadGridView1.ItemsSource; 

if there are null values in the series the x axis is not sorted. So I added a method based on the post above:
void FixDataSeries(int? min, int? max) 
        { 
            if (min != null && max != null
            { 
                foreach (DataSeries series in RadChart1.DefaultView.ChartArea.DataSeries) 
                { 
                    for (int i = (int)min; i < ((int)max + 1); i++) 
                        if (!series.Any(item => item.XValue == i)) 
                        { 
                            DataPoint p = new DataPoint(i, 16000); 
                            series.Insert(i - (int)min, p); 
                        } 
                } 
            } 
        } 

I have added a value of 16000 so that the inserted points can be highlighted. As you can see by the attachment, it does not insert the new datapoints in the correct location.

What could be the cause of this?

Regards 

Nick


0
Ves
Telerik team
answered on 20 May 2010, 11:37 AM
Hi Nick,

The issue seems to come from the XValue <-> XCategory mismatch. The XValue is numeric i.e. it defines a position along the X axis. That is -- if you have 3 items with XValues 1, 5 and 6 you will have a larger gap between the first and the second item as there are no items for 2, 3 and 4.

On the other hand, when XCategory is used, items are added next to each other and the X axis items are labeled with the corresponding value, so 3 items with XCategory set to 1, 5 and 6 will appear next to each other.

That said, the SeriesMapping has an ItemMapping for XCategory, but later when you fill the data series, I can see you create a datapoint with XValue set, but not XCategory.

Best regards,
Ves
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.
0
Nick Wood
Top achievements
Rank 1
answered on 20 May 2010, 01:46 PM
Hi Ves

Thanks for your help. I think I understand. I just am not quite sure if I know of the solution.

How would I fix this gived the current scenario?

Nick
0
Ves
Telerik team
answered on 25 May 2010, 11:53 AM
Hi Nick Wood,

In this case you would need to know all the possible categories (for examples - store them in a list). Then, in the inner loop, check all of them and create DataPoints for the missing categories. The code would be similar to this:

List<string> myCategories = ....
foreach (DataSeries series in RadChart1.DefaultView.ChartArea.DataSeries)
            {
                foreach (string category in myCategories)
                    if (!series.Any(item => item.XCategory == category))
                    {
                        DataPoint p = new DataPoint(16000);
                        p.XCategory = category;
                        series.Add(p);
                    }
            }



Sincerely,
Ves
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
Ken Spencer
Top achievements
Rank 1
Answers by
Ves
Telerik team
Nick Wood
Top achievements
Rank 1
Share this question
or