Skip to main content

ChartControlSettings.CalculatedFields Property

Returns a collection of calculated fields.

Namespace: DevExpress.Web.Mvc

Assembly: DevExpress.Web.Mvc5.v24.1.dll

NuGet Package: DevExpress.Web.Mvc5

Declaration

public ChartCalculatedFieldCollection CalculatedFields { get; }

Property Value

Type Description
ChartCalculatedFieldCollection

A collection that stores calculated fields.

Remarks

Calculated fields allow you to evaluate values based on an expression and use them as a data source for series arguments, values, tooltips, crosshair labels, and so on.

To add a calculated field to a Chart Control, add a ChartCalculatedField object to the CalculatedFields collection and specify the following parameters:

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 Name property value is used instead.

Example

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].

Calculated Fields

View Code (Razor):

@{
    ViewBag.Title = "Index";
}
@Html.DevExpress().Chart(settings => {
    settings.Name = "chart";
    settings.CalculatedFields.Add(new ChartCalculatedField { 
        Name="Displacement", 
        Expression = "[Time.Seconds]*[Velocity]", 
        FieldType=ChartCalculatedFieldType.Double 
    });
    settings.Series.Add(s => {
        s.Name = "Series";
        s.SetDataMembers("Time", "Displacement");
        s.Views().SplineSeriesView(v => { v.MarkerVisibility = DefaultBoolean.True; });
    });
}).Bind(Model).GetHtml()

Controller code:

using MvcCalculatedFields.Models;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcCalculatedFields.Controllers {
    public class ChartController : Controller {
        // GET: Chart
        public ActionResult Index() {
            return View(GetDataPoints());
        }
        private List<DataPoint> GetDataPoints() {
            List<DataPoint> 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;
        }
    }
}

Model code:

using System;

namespace MvcCalculatedFields.Models {
    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;
        }
    }
}
See Also