DevExtreme React - Update Data

DevExtreme DataSource

NOTE
This technique requires the key specified in the store.

To get the DataSource instance, call the PieChart's getDataSource() method:

jQuery
JavaScript
var ds = $("#pieChartContainer").dxPieChart("getDataSource");
AngularJS
JavaScript
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function ($scope) {
        $scope.ds = {};
        $scope.pieChartOptions = {
            // ...
            onInitialized: function (e) {
                $scope.ds = e.component.getDataSource();  
            }
        };
    });
Knockout
JavaScript
var viewModel = {
    ds: {},
    pieChartOptions: {
        // ...
        onInitialized: function (e) {
            viewModel.ds = e.component.getDataSource();    
        }
    }
};

ko.applyBindings(viewModel);
Angular
TypeScript
import { ..., ViewChild } from "@angular/core";
import { DxPieChartModule, DxPieChartComponent } from "devextreme-angular";
// ...
export class AppComponent {
    @ViewChild(DxPieChartComponent) pieChart: DxPieChartComponent;
    ds: any = {};
    getDataSource() {
        this.ds = this.pieChart.instance.getDataSource();
    }
}
@NgModule({
    imports: [
        // ...
        DxPieChartModule
    ],
    // ...
})

Then, access the underlying store with the store() method, and call the store's push(changes) method to modify data. The PieChart will be updated automatically.

JavaScript
ds.store().push([
    { type: "update", key: "Oranges", data: { count: 10 } },
    { type: "remove", key: "Apples" }
]);
See Also

JavaScript Array

jQuery

Make changes to the array using standard methods. Then, reassign the updated array to the PieChart using the option(optionName, optionValue) method.

JavaScript
var fruits = [
    { fruit: 'Apples', count: 10 },
    { fruit: 'Oranges', count: 12 },
    { fruit: 'Lemons', count: 15 }
];

fruits.push({ fruit: 'Pineapples', count: 3 });
// Reassigns the "fruits" array to the PieChart 
$("#pieChartContainer").dxPieChart("option", "dataSource", fruits);
See Also

Angular

Enclose the dataSource option in square brackets to bind it to an array using one-way binding. Now, whenever an item is added or removed from the array, the PieChart will be updated accordingly.

HTML
TypeScript
<dx-pie-chart [dataSource]="fruits"></dx-pie-chart>
import { DxPieChartModule } from "devextreme-angular";
// ...
export class AppComponent {
    fruits = [
        { fruit: 'Apples', count: 10 },
        { fruit: 'Oranges', count: 12 },
        { fruit: 'Lemons', count: 15 }
    ];
}
@NgModule({
    imports: [
        // ...
        DxPieChartModule
    ],
    // ...
})
See Also

AngularJS

Declare two-way binding between the dataSource option and the scope property that contains an array. For this, configure the bindingOptions object of the PieChart as follows.

HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-pie-chart="{
        bindingOptions: {
            dataSource: 'fruits'
        }
    }"></div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function ($scope) {
        $scope.fruits = [
            { fruit: 'Apples', count: 10 },
            { fruit: 'Oranges', count: 12 },
            { fruit: 'Lemons', count: 15 }
        ];
    });

Now, whenever an item is added or removed from the fruits array, the PieChart will be updated accordingly. If you need to track changes in objects, configure the bindingOptions object as follows.

HTML
<div ng-controller="DemoController">
    <div dx-pie-chart="{
        bindingOptions: {
            dataSource: {
                dataPath: 'fruits',
                deep: true
            }
        }
    }"></div>
</div>

In this case, the PieChart will use the $watch listener instead of the default $watchCollection listener. Note that the use of the $watch listener may impact the PieChart's peformance.

See Also

Knockout

Declare the array observable and bind the dataSource option to it. Whenever an item is added or removed from this array, the PieChart will be updated accordingly.

HTML
JavaScript
<div id="pieChartContainer" data-bind="dxPieChart: {
    dataSource: fruits
}"></div>
var viewModel = {
    fruits: ko.observableArray([
        { fruit: 'Apples', count: 10 },
        { fruit: 'Oranges', count: 12 },
        { fruit: 'Lemons', count: 15 }
    ])
};

ko.applyBindings(viewModel);
See Also