React PieChart - Custom Sources

Access to a custom data source is configured using the CustomStore component. DevExtreme provides ASP.NET and PHP extensions that help configure it and implement server-side data processing. You can also use the third-party extension for MongoDB.

You need to configure the CustomStore in detail for accessing a server built on another technology. Data in this situation can be processed on the client or server. In the former case, switch the CustomStore to the raw mode and load all data from the server in the load function as shown in the next example.

jQuery
JavaScript
$(function() {
    $("#pieChartContainer").dxPieChart({
        dataSource: new DevExpress.data.DataSource({
            store: new DevExpress.data.CustomStore({
                loadMode: "raw",   
                load: function () {
                    return $.getJSON('https://mydomain.com/MyDataService');
                }
            }), 
            paginate: false
        })
    });
});
Angular
TypeScript
HTML
import { ..., Inject } from "@angular/core";
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { DxPieChartModule } from "devextreme-angular";
import DataSource from "devextreme/data/data_source";
import CustomStore from "devextreme/data/custom_store";
import "rxjs/add/operator/toPromise";
// ...
export class AppComponent {
    pieChartDataSource: any = {};
    constructor(@Inject(HttpClient) httpClient: HttpClient) {
        this.pieChartDataSource = new DataSource({
            store: new CustomStore({
                loadMode: "raw",   
                load: () => {
                    return httpClient.get('http://mydomain.com/MyDataService')
                        .toPromise();
                }
            }),
            paginate: false
        });
    }
}
@NgModule({
    imports: [
        // ...
        DxPieChartModule,
        HttpClientModule
    ],
    // ...
})
<dx-pie-chart ...
    [dataSource]="pieChartDataSource">
</dx-pie-chart>
Vue
<template>
    <DxPieChart ... 
        :data-source="dataSource" />
</template>
<script>
import { DxPieChart } from "devextreme-vue/pie-chart";
import CustomStore from "devextreme/data/custom_store";
import DataSource from "devextreme/data/data_source";
import 'whatwg-fetch';
// ...
function handleErrors(response) {
    if (!response.ok)
        throw Error(response.statusText);
    return response;
}
const pieChartDataSource = new DataSource({
    store: new CustomStore({
        loadMode: "raw",   
        load: () => {
            return fetch("https://mydomain.com/MyDataService")
                    .then(handleErrors);
        }
    }),
    paginate: false
});
export default {
    // ...
    data() {
        return {
            dataSource: pieChartDataSource
        };
    },
    components: {
        // ...
        DxPieChart
    }
}
</script>
React
import React from "react";
import PieChart from "devextreme-react/pie-chart";
import CustomStore from "devextreme/data/custom_store";
import DataSource from "devextreme/data/data_source";
import 'whatwg-fetch';
// ...
function handleErrors(response) {
    if (!response.ok)
        throw Error(response.statusText);
    return response;
}
const pieChartDataSource = new DataSource({
    store: new CustomStore({
        loadMode: "raw",   
        load: () => {
            return fetch("https://mydomain.com/MyDataService")
                    .then(handleErrors);
        }
    }),
    paginate: false
});
class App extends React.Component {
    render() {
        return (
            <PieChart ...
                dataSource={pieChartDataSource}>
            </PieChart>
        );
    }
}
export default App;

In the latter case, use the CustomStore's load function to send data processing settings to the server. These settings are passed as a parameter to the load function and depend on the operations (filtering, sorting, etc.) that you have enabled in the DataSource. The following settings are relevant for the PieChart:

After receiving these settings, the server should apply them to data and send back an object with the following structure:

{
    data: [ ... ] // result data objects
}

This example shows how to make a query for data.

jQuery
JavaScript
$(function() {
    $("#pieChartContainer").dxPieChart({
        dataSource: new DevExpress.data.DataSource({
            store: new DevExpress.data.CustomStore({
                load: function(loadOptions) {
                    var d = $.Deferred(),
                        params = {};
                    [
                        "sort", 
                        "filter", 
                        "searchExpr", 
                        "searchOperation", 
                        "searchValue"
                    ].forEach(function(i) {
                        if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                            params[i] = JSON.stringify(loadOptions[i]);
                    });
                    $.getJSON("http://mydomain.com/MyDataService", params)
                        .done(function(result) {
                            // Here, you can perform operations unsupported by the server
                            // or any other operations on the retrieved data
                            d.resolve(result.data);
                        });
                    return d.promise();
                }
            }),
            paginate: false
        })
    });
});
function isNotEmpty(value) {
    return value !== undefined && value !== null && value !== "";
}
Angular
TypeScript
HTML
import { ..., Inject } from "@angular/core";
import { HttpClient, HttpClientModule, HttpParams } from "@angular/common/http";
import { DxPieChartModule } from "devextreme-angular";
import DataSource from "devextreme/data/data_source";
import CustomStore from "devextreme/data/custom_store";
import "rxjs/add/operator/toPromise";
// ...
export class AppComponent {
    pieChartDataSource: any = {};
    constructor(@Inject(HttpClient) httpClient: HttpClient) {
        function isNotEmpty(value: any): boolean {
            return value !== undefined && value !== null && value !== "";
        }
        this.pieChartDataSource = new DataSource({
            store: new CustomStore({
                load: (loadOptions) => {
                    let params: HttpParams = new HttpParams();
                    [
                        "sort", 
                        "filter", 
                        "searchExpr", 
                        "searchOperation", 
                        "searchValue"
                    ].forEach(function(i) {
                        if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                            params = params.set(i, JSON.stringify(loadOptions[i]));
                    });
                    return httpClient.get("http://mydomain.com/MyDataService", { params: params })
                        .toPromise()
                        .then(result => {
                            // Here, you can perform operations unsupported by the server
                            // or any other operations on the retrieved data
                            return result.data;
                        });
                }
            }),
            paginate: false
        });
    }
}
@NgModule({
    imports: [
        // ...
        DxPieChartModule,
        HttpClientModule
    ],
    // ...
})
<dx-pie-chart ...
    [dataSource]="pieChartDataSource">
</dx-pie-chart>
Vue
<template>
    <DxPieChart ... 
        :data-source="dataSource" />
</template>
<script>
import DxPieChart from "devextreme-vue/pie-chart";
import CustomStore from "devextreme/data/custom_store";
import DataSource from "devextreme/data/data_source";
import 'whatwg-fetch';
// ...
function isNotEmpty(value) {
    return value !== undefined && value !== null && value !== "";
}
function handleErrors(response) {
    if (!response.ok)
        throw Error(response.statusText);
    return response;
}
const pieChartDataSource = new DataSource({
    store: new CustomStore({
        load: (loadOptions) => {
            let params = "?";
            [
                "sort", 
                "filter", 
                "searchExpr", 
                "searchOperation", 
                "searchValue"
            ].forEach(function(i) {
                if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                    params += `${i}=${JSON.stringify(loadOptions[i])}&`;
            });
            params = params.slice(0, -1);
            return fetch(`https://mydomain.com/MyDataService${params}`)
                .then(handleErrors)
                .then(response => response.json())
                .then((result) => {
                    // Here, you can perform operations unsupported by the server
                    // or any other operations on the retrieved data
                    return result.data;
                });
        }
    }),
    paginate: false
});
export default {
    // ...
    data() {
        return {
            dataSource: pieChartDataSource
        };
    },
    components: {
        // ...
        DxPieChart
    }
}
</script>
React
import React from "react";
import PieChart from "devextreme-react/pie-chart";
import CustomStore from "devextreme/data/custom_store";
import DataSource from "devextreme/data/data_source";
import 'whatwg-fetch';
// ...
function isNotEmpty(value) {
    return value !== undefined && value !== null && value !== "";
}
function handleErrors(response) {
    if (!response.ok) 
        throw Error(response.statusText);
    return response;
}
const pieChartDataSource = new DataSource({
    store: new CustomStore({
        load: (loadOptions) => {
            let params = "?";
            [
                "sort", 
                "filter", 
                "searchExpr", 
                "searchOperation", 
                "searchValue"
            ].forEach(function(i) {
                if(i in loadOptions && isNotEmpty(loadOptions[i])) 
                    params += `${i}=${JSON.stringify(loadOptions[i])}&`;
            });
            params = params.slice(0, -1);
            return fetch(`https://mydomain.com/MyDataService${params}`)
                .then(handleErrors)
                .then(response => response.json())
                .then((result) => {
                    // Here, you can perform operations unsupported by the server
                    // or any other operations on the retrieved data
                    return result.data;
                });
        }
    }),
    paginate: false
});
class App extends React.Component {
    render() {
        return (
            <PieChart ...
                dataSource={pieChartDataSource}>
            </PieChart>
        );
    }
}
export default App;
See Also