Chartview with ChartDataSource and Custom Aggregate Function

5 posts, 0 answers
  1. Anders Abildgaard
    Anders Abildgaard avatar
    3 posts
    Member since:
    Mar 2016

    Posted 22 Mar Link to this post

    Hello,

    I've been working with the Chartview lately, and have a chart with a datetimecategorical axis, that utilizes a chartdatasource to group datapoints when the chart is zoomed out. My problem is that the default aggregation function being applied takes the average of grouped datapoints, and I am looking for the sum. To my knowledge there is no sumfunction of type ChartAggregateFunction, and very little information detailing how I go about creating a custom function to gain that functionality.

    I was wondering if you could show me an example detailing how to go about solving my problem.

  2. Dinko
    Admin
    Dinko avatar
    202 posts

    Posted 25 Mar Link to this post

    Hello Anders Abildgaard,

    Yes, the default aggregate function of the RadChartView that is beeing applied takes the average given values. However, you can use a different aggregate function like Sum, Max, Min etc. Keep in mind that if you need a custom aggregate function you will have to create a class that inherits the ChartSeries of your choice and override its GetValue/CategoryAggregateFunction() method.

    We have a Use custom aggregate function help section in the ChartDataSource article where you can find more information about this. If you want to use a Sum function you can create a class which inherits ChartAggregateFunction and override the AggregateMethodName to return "Sum".
    <local:MyBarSeries />

     
    public class MyBarSeries : BarSeries
    {
        protected override ChartAggregateFunction GetValueAggregateFunction()
        {
            return new MyChartSumFunction();
        }
    }
      
    public class MyChartSumFunction : ChartAggregateFunction
    {
        protected override string AggregateMethodName
        {
            get
            {
                return "Sum";
            }
        }
    }
    Regards,
    Dinko
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Anders Abildgaard
    Anders Abildgaard avatar
    3 posts
    Member since:
    Mar 2016

    Posted 29 Mar in reply to Dinko Link to this post

    Hello Dinko.

    I might be misunderstanding how to implement a custom function.

    This is my code, which is a replica of yours: 

    public class SumBarSeries : BarSeries     {
            protected override ChartAggregateFunction GetValueAggregateFunction()
            {
                return new ChartSumFunction();
            }
        }
      
        public class ChartSumFunction : ChartAggregateFunction     {
            protected override string AggregateMethodName
            {
                get             {
                    return "Sum";
                }
            }
        }

    And my Xaml:

    <controls:SumBarSeries ItemsSource="{Binding ElementName=countDataSource}"
                                       CategoryBinding="HourInterval"
                                       ValueBinding="ActionCount">
                        <telerik:BarSeries.TrackBallInfoTemplate>
                            <DataTemplate>
                                <StackPanel Background="Transparent">
                                    <TextBlock Style="{StaticResource NormalTextBlockStyle}">
                                        <TextBlock.Text>
                                            <MultiBinding StringFormat="{}{0} {1:0.#}">
                                                <Binding Path="(resources:Translation.S5400_TranslogChartView_MiscOperationShort)"/>
                                                <Binding Path="DataPoint.Value"/>
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </telerik:BarSeries.TrackBallInfoTemplate>
                    </controls:SumBarSeries>

    When I run this, my chart says there's no data to plot, despite having populated the datasource.
    Anyway, my question lies with the ChartAggregateFunction. So far we've just specified a name for it, which is "sum", but dont we need to define what the function should do to the data(Which in our case would be to sum it together, instead of averaging).

    Best regards,

    Anders

  5. Dinko
    Admin
    Dinko avatar
    202 posts

    Posted 31 Mar Link to this post

    Hi Anders,

    Let me get straight to your concerns.

    1. When I run this, my chart says there's no data to plot, despite having populated the data source.
    I am not sure why your data is not displayed but if you send me a runnable code snippets I can test it on my side.

    2. So far we've just specified a name for it, which is "sum", but dont we need to define what the function should do to the data(Which in our case would be to sum it together, instead of averaging).
     -
    The chart's aggregate functions derive from the EnumerableSelectorAggregateFunction class. When you override the ChartAggregateFunction property you can return the name ( as a String ) of the function that you want to use. You don't have to implement any other logic. In a few words, there is built-in logic that samples your data depending on the aggregate function name. 

    Regards,
    Dinko
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Anders Abildgaard
    Anders Abildgaard avatar
    3 posts
    Member since:
    Mar 2016

    Posted 01 Apr in reply to Dinko Link to this post

    Hello Dinko,

    It turns out your initial answer was sufficient to solve my problem. I suspect the reason I was not seeing my data being plotted is that we're using NoXaml binaries, so when my custom barseries inherited from barseries, it didnt get the style applied. I fixed my problem using your approach and then just applying your default style explicitly afterwards.

    Thanks for your rapid responses though. :)

    Best regards,

    Anders

Back to Top
UI for WPF is Visual Studio 2017 Ready