Data Binding
The Angular Chart supports the binding of its data series and category axis to arrays and Observables. This article explains the basic code approaches to data bind a chart.
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.
Binding to Arrays of Values
The simplest form of data binding involves supplying each series with an array of values. The values are plotted in the provided order.
Binding to 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.
@Component({
selector: 'my-app',
template: `
<kendo-chart>
<kendo-chart-series>
<kendo-chart-series-item type="bubble" [data]="seriesData">
</kendo-chart-series-item>
</kendo-chart-series>
</kendo-chart>
`
})
export class AppComponent {
public seriesData: number[] = [[1, 1, 10], [2, 2, 20], [3, 3, 30]];
}
Objects
The Angular 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.
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.
Binding Categories
The category axis of Categorical Charts is a data-bound component just like the series.
When the Angular Chart is bound to dates, the category axis provides dedicated functions. For more information, refer to the section on displaying time series.
Binding to Series Data Objects
To bind the categories to the same model object as the series, specify a categoryField
. In this way, the series points and categories will always be matched automatically.
Binding to Series Value Arrays
As an alternative to binding to objects, it's also possible to bind the series to an array of [value, category]
arrays.
Binding to a Category Array
The simplest form of data binding involves the supplying of an array of category labels to the axis. The list will be displayed as is, without any modifications. Series data points are positioned in sequence along the axis.
This data binding approach is error-prone as it does not guarantee that the series data points match the intended categories. Keep in mind that:
- 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
.
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'
.
In this case, the Angular 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.
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.
Updating Data
Replacing the Array Instance
To improve its browser performance, the Chart uses the OnPush change-detection strategy. This means that the component does not detect changes to the array instance—for example, when an element is added. To trigger the change detection, create a new array instance for the collection—for example, instead of the this.data.push({new item})
array, set a this.data = [...this.data, {new item}]
one.
Binding to Observables
When you bind the Chart to an observable, the component automatically updates the information it renders every time new data comes from the observable. To bind the Chart to an observable, use an async pipe.
No-Data Overlay
When no series data is available, the Chart will display an overlay element that contains a message with the text "No data available".
The "No data available" message can be customized to display any desired text. For more information, refer to the article about customizing the built-in messages of the Chart.
Disabling the No-Data Overlay
To disable the built-in no-data overlay, set the noData
property of the Chart component to false
.
Configuring a No-Data Template
The Chart enables you to configure a custom template for the no-data overlay element. To define a no-data template, nest an <ng-template>
tag with the kendoChartNoDataTemplate
directive inside the <kendo-chart>
component.