Problem with ChartYearGroupDescriptor

8 posts, 1 answers
  1. Maxence
    Maxence avatar
    21 posts
    Member since:
    Aug 2011

    Posted 12 Dec 2011 Link to this post

    Hello Telerik Team,

    i try to reproduce the sample here : http://demos.telerik.com/silverlight/#Chart/FirstLook
    especially the first chart (top left):

    here the code in my view :

    <chart:RadChart Grid.Row="2" Grid.ColumnSpan="2" x:Name="barChartCobitProcess" ItemsSource="{Binding CobitProcess}" SeriesMappings="{Binding BarChartProcessCobitSeriesMappings}" telerik:StyleManager.Theme="Metro" >
        <chart:RadChart.DefaultView>
            <charting:ChartDefaultView>
                <charting:ChartDefaultView.ChartLegend>
                    <charting:ChartLegend Name="legend1" />
                </charting:ChartDefaultView.ChartLegend>
                <charting:ChartDefaultView.ChartArea>
                    <charting:ChartArea Grid.ColumnSpan="2"
                               LegendName="legend1"
                               ItemWidthPercent="50"
                               Command="{Binding ChartArea1ClickCommand}">
                        <charting:ChartArea.AxisX>
                            <charting:AxisX DefaultLabelFormat="{Binding AxisXLabelFormatCobitProcess}" />
                        </charting:ChartArea.AxisX>
                        <charting:ChartArea.AxisY>
                            <charting:AxisY IsZeroBased="True" ExtendDirection="None" />
                        </charting:ChartArea.AxisY>
                    </charting:ChartArea>
                </charting:ChartDefaultView.ChartArea>
            </charting:ChartDefaultView>
        </chart:RadChart.DefaultView>
    </chart:RadChart>

    and here the key functions in my view model (also trying the same thing than the sample) :
    private void InitializeBarChartCobitProcessSeriesMappings()
            {
                SeriesMappingCollection seriesMappings = new SeriesMappingCollection();
     
                foreach (string categorie in this.ListCobitProcess)
                {
                    StackedBarSeriesDefinition seriesDefinition1 = CreateBarSeriesDefinition();
                    SeriesMapping seriesMapping1 = this.CreateSeriesMapping(categorie, seriesDefinition1, "Heures", ChartAggregateFunction.Sum);
                    //seriesMapping1.GroupingSettings.GroupDescriptors.Add(new ChartYearGroupDescriptor("Date"));
                    seriesMappings.Add(seriesMapping1);
                }
     
                this.BarChartProcessCobitSeriesMappings = seriesMappings;
            }
     
            private static StackedBarSeriesDefinition CreateBarSeriesDefinition()
            {
                StackedBarSeriesDefinition seriesDefinition = new StackedBarSeriesDefinition();
                seriesDefinition.ShowItemLabels = false;
                seriesDefinition.LabelSettings.LabelDisplayMode = LabelDisplayMode.Inside;
                return seriesDefinition;
            }
     
            private SeriesMapping CreateSeriesMapping(string nom, SeriesDefinition seriesDefinition, string yValueField, ChartAggregateFunction aggFunction)
            {
                SeriesMapping result = new SeriesMapping();
                result.LegendLabel = nom;
                result.SeriesDefinition = seriesDefinition;
                result.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
                result.ItemMappings.Add(new ItemMapping(yValueField, DataPointMember.YValue, aggFunction));
                result.FilterDescriptors.Add(new FilterDescriptor("Nom", FilterOperator.IsEqualTo, nom));
                result.GroupingSettings.ShouldFlattenSeries = true;
                result.GroupingSettings.GroupDescriptors.Add(new ChartYearGroupDescriptor("Date"));
     
                return result;
            }

    Here the data send to the chart :
    Date(with format : dd/mm/yyyy)       Durée       Nom
    12/12/2011                                      0,5           Acquérir
    12/12/2011                                      0,5           Acquérir
    12/12/2011                                      0,5           Assurer les corrections
    11/12/2011                                      0,5           Assurer le service continu
    13/12/2011                                      10,0         Assurer la sécurité
    14/12/2011                                      4,0           Assurer la sécurité
    16/12/2011                                      1,5           Assurer la sécurité
    21/12/2011                                      2,0           Assurer la sécurité

    but i obtain the result in first attached file...
    it looks good but it's not because they are all from 2011 but dispatched between 3 bars. normally only one must display in this case.
    i have the same behavior when clicking for the details of month : i get 3 bars, all marked "december".

    when i remove DefaultLabelFormat="{Binding AxisXLabelFormatCobitProcess}"  from charting:axisX, i have the second picture attached where clearly we see 3 differents labels so... is the chartyeargroupdescriptor works well ? where is my mistake ?

    i'm in default culture : french culture, don't know if there is an incidence.

    thanks by advance for your help,

    Regards,

    Maxence
  2. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 14 Dec 2011 Link to this post

    Hi Maxence,

    From the code you share I understand that you are adding a separate series mapping for each Nom from your data. The sample data that you shared has 4 different nom categories and that's why you get 4 different bars. The group descriptor is used within the series mapping to group its items. If you want your grouping to be applied to all your data items, add only a single series mapping with year group descriptor.

    Greetings,
    Yavor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Maxence
    Maxence avatar
    21 posts
    Member since:
    Aug 2011

    Posted 14 Dec 2011 Link to this post

    Hi Yavor,

    thanks for your reply. Unfortunately, i can't read the silverlight project into your zip file.
    I understand the solution you propose but..
    • is it possible to have both a "general" grouping (only one stackbar per year) and also 4 series (one per different "Nom") ?
    • or must i add additionnal parameters (like "year" and "month", calculated from the date) to change my XCategory field ?

    thanks by advance for your information,

    Regards,

    Maxence

  4. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 19 Dec 2011 Link to this post

    Hello Maxence,

    You are right, the project I sent to you was created with Silverlight 5 by mistake.

    Back to the problem, yes you can create different series by grouping by "Nom". Here is some code:

    private SeriesMapping CreateSeriesMapping(SeriesDefinition seriesDefinition, string yValueField, ChartAggregateFunction aggFunction)
    {
        SeriesMapping result = new SeriesMapping();
        //result.LegendLabel = nom;
        result.SeriesDefinition = seriesDefinition;
        result.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
        result.ItemMappings.Add(new ItemMapping(yValueField, DataPointMember.YValue, aggFunction));
        //result.FilterDescriptors.Add(new FilterDescriptor("Nom", FilterOperator.IsEqualTo, nom));
        //result.GroupingSettings.ShouldFlattenSeries = true;
        result.GroupingSettings.GroupDescriptors.Add(new ChartGroupDescriptor("Nom"));
        result.GroupingSettings.GroupDescriptors.Add(new ChartYearGroupDescriptor("Date"));
     
        return result;
    }

    This series mapping configuration will produce 1 bar for each Nom for each year. If the resulting chart doesn't meet your requirements can you please share more information on what the expected result is? A sample image that shows the expected result will be very helpful.

    Kind regards,
    Yavor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 19 Dec 2011 Link to this post

    Hi Maxence,

    I forgot to attach the test application to my previous post. Please try it now, it should be ok.

    Best wishes,
    Yavor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Maxence
    Maxence avatar
    21 posts
    Member since:
    Aug 2011

    Posted 23 Dec 2011 Link to this post

    Hi Yavor,

    thanks a lot for your answer and test application.
    sorry for my late answer (i was in free days these times)
    i think what is wrong is the X-axis that creates multiple bars for a same year :
    it looks like "chartyeargroupdescriptor" keep in memory the first date of the year, that's why some are stacked in your sample and the others not.
    i would like one stacked bar per year
    here attached twopictures : the one given by your sample and the graph i wish to obtain.

    I wish you a merry christmas !

    Regards,

    Maxence
  7. Answer
    Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 23 Dec 2011 Link to this post

    Hello Maxence,

    Bars get stacked by their XValue / XCategory. In your case you have 2 bars that have the same date for their XCategory and that's why they are stacked. The grouping by Nom produces 1 bar for each Nom and sums the Y-values, and takes the first X-value to produce a bar. The bottom line is that you get 4 bars for the different Noms, 2 with the same XCategory and 2 with a different one. Grouping by year produces the same result because all data items are within the same year, however no grouping can change the X-value of your data items.

    Having this in mind you can stack all 4 bars by setting the same XCategory on all of them. To do that simply remove the year group descriptor and use only the year component as a XValue. Here is some code:

    public class MyData
    {
        public DateTime Date { get; set; }
        public int Annee
        {
            get
            {
                return this.Date.Year;
            }
        }
    ....
    }

    Hope this helps and merry Christmas! :)

    Kind regards,
    Yavor
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  8. Maxence
    Maxence avatar
    21 posts
    Member since:
    Aug 2011

    Posted 25 Dec 2011 Link to this post

    Hi Yavor,

    it works "almost" perfect now ^^. thanks for your help.

    it's "almost" and not totally perfect due to another issue. i have the same behavior than Ralph, sometimes, my stacked bars are clipped. looks like a bug. i will register to his post (http://www.telerik.com/community/forums/silverlight/chart/stacked-bar-chart-gets-clipped.aspx) and hope to see solution coming ^^

    Regards,

    Maxence
Back to Top