Back to Devexpress

ChartIndicatorCalculateFunction Delegate

dashboard-devexpress-dot-dashboardcommon-c412a8d9.md

latest9.3 KB
Original Source

ChartIndicatorCalculateFunction Delegate

Calculates data points that are used to draw a custom trend indicator in the Chart dashboard item.

Namespace : DevExpress.DashboardCommon

Assembly : DevExpress.Dashboard.v25.2.Core.dll

NuGet Package : DevExpress.Dashboard.Core

Declaration

csharp
public delegate Dictionary<AxisPoint, object> ChartIndicatorCalculateFunction(
    Dictionary<AxisPoint, decimal?> points
);
vb
Public Delegate Function ChartIndicatorCalculateFunction(
    points As Dictionary(Of AxisPoint, Decimal?)
) As Dictionary(Of AxisPoint, Object)

Parameters

NameTypeDescription
pointsDictionary<AxisPoint, Nullable<Decimal>>

A collection of axis data points in the Chart dashboard item.

|

Returns

TypeDescription
Dictionary<AxisPoint, Object>

A collection of resulting data points that are used to draw the indicator.

|

Remarks

The following example shows how to create a custom “Moving Average” indicator for a chart dashboard item:

View Example

Create a Custom Indicator Type

Create a ChartCustomIndicator descendant (the MovingIndicator class in this example). MovingIndicator accepts a collection of data points, evaluates the values, and returns the resulting points. These points are used to draw the indicator.

Register the Custom Indicator Type

Register the MovingIndicator type in IndicatorFactory to make this type available as the indicator type in the Trend Indicators editor.

Call the Register method in your application before you save and load a dashboard to serialize and deserialize the indicator within the dashboard XML.

Configure and Display the Indicator

Create an instance of MovingIndicator and specify the indicator settings:

This property is required to display the indicator in the Chart dashboard item when you launch the application:

ValueSpecifies the measure data item that is used to calculate the trend indicator.

If you do not specify the following properties, their default values are used:

NameSpecifies the name of the trend indicator within the indicators collection.ValueLevelGets or sets the value that specifies which series point value should be used to calculate the indicator.ShowInLegendSpecifies whether to display the trend indicator in the legend.LegendTextSpecifies the text that identifies the trend indicator within the legend.ThicknessSpecifies the thickness of the indicator line.ColorSpecifies the trend indicator’s color.DashStyleSpecifies the dash style used to paint the line.VisibleSpecifies whether to display the trend indicator.

Add MovingIndicator to the chart indicators collection to display the configured indicator in the UI.

cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.DashboardCommon;
using DevExpress.DashboardCommon.ViewerData;

namespace WinForm {
    public partial class FormDesigner : Form {
        public FormDesigner() {
            try {
                IndicatorFactory.Register<MovingIndicator>("Moving average");
            } catch(Exception e) {
                MessageBox.Show(e.Message);
            }
            InitializeComponent();
            var dashboard = new Dashboard1();
            ChartDashboardItem chartItem = dashboard.Items.First(x => x.ComponentName == "chartDashboardItem1") as ChartDashboardItem;
            MovingIndicator trendLine = new MovingIndicator() {
                Name = "MovingLine1",
                Value = "DataItem1",
                ValueLevel = DevExpress.XtraCharts.ValueLevel.Value,
                Color = Color.Orange,
                LegendText = "Moving Average"
            };
            chartItem.Indicators.Add(trendLine);
            dashboardDesigner.Dashboard = dashboard;
            dashboardDesigner.CreateRibbon();
            dashboardDesigner.CreateCustomItemBars();
        }
    }

    public class MovingIndicator : ChartCustomIndicator {
        protected override Dictionary<AxisPoint, object> Calculate(Dictionary<AxisPoint, decimal?> values) {
            var items = new Dictionary<AxisPoint, object>(values.Count);

            var sum = decimal.Zero;
            var count = 0;
            foreach(KeyValuePair<AxisPoint, decimal?> point in values) {
                if(count == 0) {
                    items.Add(point.Key, null);
                } else {
                    items.Add(point.Key, sum / count);
                }
                sum += point.Value ?? 0;
                count++;
            }

            return items;
        }
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Linq
Imports System.Windows.Forms
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardCommon.ViewerData

Namespace WinForm
    Partial Public Class FormDesigner
        Inherits Form

        Public Sub New()
            Try
                IndicatorFactory.Register(Of MovingIndicator)("Moving average")
            Catch e As Exception
                MessageBox.Show(e.Message)
            End Try
            InitializeComponent()
            Dim dashboard = New Dashboard1()
            Dim chartItem As ChartDashboardItem = TryCast(dashboard.Items.First(Function(x) x.ComponentName = "chartDashboardItem1"), ChartDashboardItem)
            Dim trendLine As New MovingIndicator() With {
                .Name = "MovingLine1",
                .Value = "DataItem1",
                .ValueLevel = DevExpress.XtraCharts.ValueLevel.Value,
                .Color = Color.Orange,
                .LegendText = "Moving Average"
            }
            chartItem.Indicators.Add(trendLine)
            dashboardDesigner.Dashboard = dashboard
            dashboardDesigner.CreateRibbon()
            dashboardDesigner.CreateCustomItemBars()
        End Sub
    End Class

    Public Class MovingIndicator
        Inherits ChartCustomIndicator

        Protected Overrides Function Calculate(ByVal values As Dictionary(Of AxisPoint, Decimal?)) As Dictionary(Of AxisPoint, Object)
            Dim items = New Dictionary(Of AxisPoint, Object)(values.Count)

            Dim sum = Decimal.Zero
            Dim count = 0
            For Each point As KeyValuePair(Of AxisPoint, Decimal?) In values
                If count = 0 Then
                    items.Add(point.Key, Nothing)
                Else
                    items.Add(point.Key, sum / count)
                End If
                sum += If(point.Value, 0)
                count += 1
            Next point

            Return items
        End Function
    End Class
End Namespace

The following image illustrates the result:

More Examples

View Example: Dashboard for ASP.NET Core - Custom Trend Indicator

See Also

DevExpress.DashboardCommon Namespace