Data Binding

The Chart supports the binding of its data series and category axis to arrays and Datasets of objects.

Binding Series

This section provides information on the binding methods for the Chart series in general. For more information on how to configure each Chart series, refer to the articles on the Chart series types.

Arrays of Values

The simplest form of data binding involves the supplying of each series with an array of values.

import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [1, 2, 3, 5];

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            <ChartSeriesItem data={seriesData} />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

Arrays of Arrays

Some series require more than one value per point—for example, the Scatter (x and y) and Bubble (x, y, and size) series.

The following example demonstrates how to bind a Bubble series to an array of arrays.

import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [[1, 1, 10], [2, 2, 20], [3, 3, 30]];

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            <ChartSeriesItem data={seriesData} type="bubble" />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

Objects

The Chart allows you to bind it to objects by specifying the fields you want to use—for the value, category, X value, Y value, and so on. This is the most commonly used type of binding as it allows you to use your model with little or no modification.

The following example demonstrates how to configure a Column series with bound category text and a value.

import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [{
    product: "Chai",
    sales: 200
}, {
    product: "Others",
    sales: 250
}];

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            <ChartSeriesItem data={seriesData} type="column" field="sales" categoryField="product" />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

Groups of Objects

It is often convenient to plot a separate series for each unique category in a data set. For example, a Line Chart for each product in a sales report. Typically, the exact number of categories is not known in advance.

The Data Query package offers a convenient groupBy method that you can use to split the records into groups. It takes the data and a GroupDescriptor. The output is a GroupResult that contains the groups and their items.

The following example demonstrates how to plot a Line Chart for each service.

import { groupBy, GroupResult } from '@progress/kendo-data-query';
import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const data = [{
    interval: 1, service: 'Service 1', value: 5
}, {
    interval: 2, service: 'Service 1', value: 15
}, {
    interval: 3, service: 'Service 1', value: 10
}, {
    interval: 1, service: 'Service 2', value: 10
}, {
    interval: 2, service: 'Service 2', value: 5
}, {
    interval: 3, service: 'Service 2', value: 15
}];

const series = groupBy(data, [{ field: "service" }]);

const mapSeries = (item) => (<ChartSeriesItem data={item.items} name={item.value} field="value" categoryField="interval"  type="line" />);

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            {series.map(mapSeries)}
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

Binding Categories

The category axis of Categorical Charts is a data-bound component just like the series.

It supports the following basic approaches for providing the category list:

When the Chart is bound to dates, the category axis provides dedicated functions. For more information, refer to the section on displaying time series.

Arrays of Labels

The simplest form of data binding involves the supplying of an array of labels for the categories to the axis. The list will be displayed as is, without any modifications. Series data points are positioned in sequence along the axis.

  • The order of the categories has no relation to the order of the series data points.
  • The number of the categories has to be equal to the number of the data points in the series.
  • To preserve the order, the missing values in the series have to be represented by null.
import {
    Chart,
    ChartCategoryAxis,
    ChartCategoryAxisItem,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [20, 40, 45, 30, 50];
const categories = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];

const ChartContainer = () => (
    <Chart>
        <ChartCategoryAxis>
            <ChartCategoryAxisItem categories={categories} />
        </ChartCategoryAxis>
        <ChartSeries>
            <ChartSeriesItem data={seriesData} />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

Category Fields

The previous approach is error-prone because of the necessity for you to maintain a category and a particular data order. To avoid this requirement, bind the categories to the same model object as the series. In this way, the series points and categories will always be matched automatically.

import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [{
    product: "Chai",
    sales: 200
}, {
    product: "Others",
    sales: 250
}];

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            <ChartSeriesItem data={seriesData} type="column" field="sales" categoryField="product" />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

Handling Duplicate Categories

Binding to a category field makes it possible for two data points to have the same category—the following example demonstrates two values that are declared for 'Chai'.

import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [{
    product: "Chai",
    sales: 200
}, {
    product: "Chai",
    sales: 100
}, {
    product: "Others",
    sales: 250
}];

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            <ChartSeriesItem data={seriesData} type="column" field="sales" categoryField="product" />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

In this case, the Chart takes the data points from the source data set and produces a new point by using an aggregate function.

By default, the aggregate function returns the maximum value of the value fields. If the category contains only one point, it returns it without modification. Other aggregates, such as count and sum, produce their own value even if the category contains just one data point.

import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [{
    product: "Chai",
    sales: 200
}, {
    product: "Chai",
    sales: 100
}, {
    product: "Others",
    sales: 250
}];

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            <ChartSeriesItem
                data={seriesData}
                type="column"
                field="sales"
                categoryField="product"
                aggregate="count"
            />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);

It is also possible to define your own aggregate functions, as demonstrated in the following example.

When the category binding is in use, the aggregate function is executed for all unique categories.

import {
    Chart,
    ChartSeries,
    ChartSeriesItem
} from '@progress/kendo-react-charts';

const seriesData = [{
    product: "Chai",
    sales: 200
}, {
    product: "Chai",
    sales: 100
}, {
    product: "Others",
    sales: 250
}];

const customAggregate = (values, series, dataItems, category) => (values.reduce((n, acc) => acc + n, 0));

const ChartContainer = () => (
    <Chart>
        <ChartSeries>
            <ChartSeriesItem
                data={seriesData}
                type="column"
                field="sales"
                categoryField="product"
                aggregate={customAggregate}
            />
        </ChartSeries>
    </Chart>
);

ReactDOM.render(
    <ChartContainer />,
    document.querySelector('my-app')
);
 /