wpf-devexpress-dot-xpf-dot-charts-bb50df17.md
A calculated field.
Namespace : DevExpress.Xpf.Charts
Assembly : DevExpress.Xpf.Charts.v25.2.dll
NuGet Package : DevExpress.Wpf.Charts
public class ChartCalculatedField :
ChartElement,
IChartCalculatedField,
ICalculatedField
Public Class ChartCalculatedField
Inherits ChartElement
Implements IChartCalculatedField,
ICalculatedField
Calculated fields allow you to evaluate values based on an expression and use them as a data source for series arguments,values, tooltips, crosshair label, and so on.
To add a calculated field to a Chart Control, add a ChartCalculatedField object to the ChartControl.CalculatedFields collection and specify the following parameters:
Expression - An expression is a string that, when parsed and processed, evaluates a value. Expressions can consist of column/field names in brackets, constants, operators, and functions. Refer to Criteria Language Syntax for information on expression syntax.
FieldType - Specifies the type of resulting values. The ChartCalculatedFieldType enumeration lists supported types.
FieldName - The calculated field’s unique name. Avoid dots in names because the Chart uses dots to access data source members. Use the name to specify series data members (for example, Series.ArgumentDataMember, Series.ValueDataMember, Series.ToolTipHintDataMember, or Series.ColorDataMember). You can also use calculated field names to configure text patterns (for example, XYSeries2D.CrosshairLabelPattern).
Optional parameters:
DataSource - Allows you to specify a custom data source to evaluate the specified expression. The calculated field and series must use the same data source. Series can obtain data from the chart’s data source or can be bound to a separate data source (see the Series.DataSource property). If the calculated field’s DataSource property is not specified, the field uses the corresponding series’s data source.
DataMember - Allows you to specify the name of the sub-list or table if the data source contains more than one distinct list of data items.
DisplayName - The calculated field’s display name that appears in the Chart Designer‘s Chart Data tab. If DisplayName is not specified, the FieldName property value is used instead.
The following example shows how to create a calculated field and then use this field as a data source for a series. The field’s values are calculated by the following expression: [Time.Seconds] * [Velocity].
<dxc:ChartControl x:Name="chartControl">
<dxc:ChartControl.DataContext>
<local:ChartViewModel/>
</dxc:ChartControl.DataContext>
<dxc:ChartControl.CalculatedFields>
<dxc:ChartCalculatedField FieldName="Displacement"
Expression="[Time.Seconds] * [Velocity]"
FieldType="Double"/>
</dxc:ChartControl.CalculatedFields>
<dxc:XYDiagram2D>
<dxc:SplineSeries2D DisplayName="Series"
DataSource="{Binding DataPoints}"
ArgumentDataMember="Time"
ValueDataMember="Displacement"
CrosshairLabelPattern="{}{Displacement:f1}"
MarkerVisible="True"/>
...
</dxc:XYDiagram2D>
</dxc:ChartControl>
using System;
using System.Collections.Generic;
using System.Windows;
namespace CalculatedFields {
public class ChartViewModel {
List<DataPoint> dataPoints;
public List<DataPoint> DataPoints {
get {
if (dataPoints == null) {
dataPoints = new List<DataPoint> {
new DataPoint (new TimeSpan(0, 0, 0), 10),
new DataPoint (new TimeSpan(0, 0, 1), 11.46),
new DataPoint (new TimeSpan(0, 0, 2), 11.90),
new DataPoint (new TimeSpan(0, 0, 3), 12.1),
new DataPoint (new TimeSpan(0, 0, 4), 12.236),
new DataPoint (new TimeSpan(0, 0, 5), 12.325),
new DataPoint (new TimeSpan(0, 0, 6), 12.625),
new DataPoint (new TimeSpan(0, 0, 7), 13.896),
new DataPoint (new TimeSpan(0, 0, 8), 14.123),
new DataPoint (new TimeSpan(0, 0, 9), 14.623),
new DataPoint (new TimeSpan(0, 0, 10), 15.007),
new DataPoint (new TimeSpan(0, 0, 11), 15.452),
new DataPoint (new TimeSpan(0, 0, 12), 15.756)
};
}
return dataPoints;
}
}
}
public class DataPoint {
public TimeSpan Time { get; set; }
public double Velocity { get; set; }
public DataPoint(TimeSpan time, double velocity) {
this.Time = time;
this.Velocity = velocity;
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Windows
Namespace CalculatedFields
Public Class ChartViewModel
Private f_DataPoints As List(Of DataPoint)
Public ReadOnly Property DataPoints As List(Of DataPoint)
Get
If f_DataPoints Is Nothing Then
f_DataPoints = New List(Of DataPoint) From {
New DataPoint(New TimeSpan(0, 0, 0), 10),
New DataPoint(New TimeSpan(0, 0, 1), 11.46),
New DataPoint(New TimeSpan(0, 0, 2), 11.90),
New DataPoint(New TimeSpan(0, 0, 3), 12.1),
New DataPoint(New TimeSpan(0, 0, 4), 12.236),
New DataPoint(New TimeSpan(0, 0, 5), 12.325),
New DataPoint(New TimeSpan(0, 0, 6), 12.625),
New DataPoint(New TimeSpan(0, 0, 7), 13.896),
New DataPoint(New TimeSpan(0, 0, 8), 14.123),
New DataPoint(New TimeSpan(0, 0, 9), 14.623),
New DataPoint(New TimeSpan(0, 0, 10), 15.007),
New DataPoint(New TimeSpan(0, 0, 11), 15.452),
New DataPoint(New TimeSpan(0, 0, 12), 15.756)
}
End If
Return f_DataPoints
End Get
End Property
End Class
Public Class DataPoint
Public Property Time As TimeSpan
Public Property Velocity As Double
Public Sub New(ByVal time As TimeSpan, ByVal velocity As Double)
Me.Time = time
Me.Velocity = velocity
End Sub
End Class
End Namespace
Object DispatcherObject DependencyObject Visual UIElement FrameworkElement Control ChartElementBase ChartElement ChartCalculatedField
See Also