Angular Sankey - Update Data

DevExtreme DataSource

NOTE
This technique requires the key specified in the store.

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

jQuery
index.js
function getDataSource() {
    return $("#sankeyContainer").dxSankey("getDataSource");
}
Angular
app.component.ts
app.module.ts
import { Component, ViewChild } from '@angular/core';
import { DxSankeyComponent } from 'devextreme-angular';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    @ViewChild(DxSankeyComponent, { static: false }) sankey: DxSankeyComponent;
    // Prior to Angular 8
    // @ViewChild(DxSankeyComponent) sankey: DxSankeyComponent;
    getDataSource() {
        return this.sankey.instance.getDataSource();
    }
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

import { DxSankeyModule } from 'devextreme-angular';

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        DxSankeyModule
    ],
    providers: [ ],
    bootstrap: [AppComponent]
})
export class AppModule { }
Vue
App.vue
<template>
    <DxSankey :ref="sankeyRefKey">
        <!-- ... -->
    </DxSankey>
</template>

<script>
import DxSankey from 'devextreme-vue/sankey';

const sankeyRefKey = "my-sankey";

export default {
    components: {
        DxSankey
    },
    data() {
        return {
            sankeyRefKey
        }
    },
    methods: {
        getDataSource: function() {
            return this.sankey.getDataSource();
        }
    },
    computed: {
        sankey: function() {
            return this.$refs[sankeyRefKey].instance;
        }
    }
}
</script>
React
App.js
import { useRef } from 'react';
import Sankey from 'devextreme-react/sankey';

export default function App() {
    const sankey = useRef(null);
    const getDataSource = () => {
        return sankey.current.instance.getDataSource();
    }

    return (
        <Sankey ref={sankey}>
            {/* ... */}
        </Sankey>
    );
}

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

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

JavaScript Array

jQuery

You can use the standard methods to make changes to the array. Then, use the option(optionName, optionValue) method to reassign the updated array to the Sankey.

JavaScript
var sankeyData = [
    { source: "Brazil", target: "Spain", weight: 4 },
    { source: "Brazil", target: "Portugal", weight: 5 },
    { source: "Brazil", target: "England", weight: 2 },
    { source: "Canada", target: "Portugal", weight: 2 },
    { source: "Canada", target: "England", weight: 1 },
    { source: "Mexico", target: "Portugal", weight: 9 },
    { source: "Mexico", target: "Spain", weight: 5 }
];

sankeyData.push({ source: "Mexico", target: "Spain", weight: 2 });
// Reassigns the "sankeyData" array to the Sankey 
$("#sankeyContainer").dxSankey("option", "dataSource", sankeyData);
See Also

Angular

Enclose the dataSource property 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 Sankey is updated accordingly.

HTML
TypeScript
<dx-sankey
    [dataSource]="sankeyData">
</dx-sankey>
import { DxSankeyModule } from "devextreme-angular";
// ...
export class AppComponent {
    sankeyData: Array<{ source: string, target: string, weight: number }> = [
        { source: "Brazil", target: "Spain", weight: 4 },
        { source: "Brazil", target: "Portugal", weight: 5 },
        { source: "Brazil", target: "England", weight: 2 },
        { source: "Canada", target: "Portugal", weight: 2 },
        { source: "Canada", target: "England", weight: 1 },
        { source: "Mexico", target: "Portugal", weight: 9 },
        { source: "Mexico", target: "Spain", weight: 5 }
    ];
}
@NgModule({
    imports: [
        // ...
        DxSankeyModule
    ],
    // ...
})

AngularJS

Declare two-way binding between the dataSource property and the scope property that contains an array. To do this, configure the bindingOptions object in the Sankey as follows:

HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-sankey="{
        bindingOptions: {
            dataSource: 'sankeyData'
        }
    }"></div>
</div>
angular.module("DemoApp", ["dx"])
    .controller("DemoController", function($scope) {
        $scope.sankeyData = [
            { source: "Brazil", target: "Spain", weight: 4 },
            { source: "Brazil", target: "Portugal", weight: 5 },
            { source: "Brazil", target: "England", weight: 2 },
            { source: "Canada", target: "Portugal", weight: 2 },
            { source: "Canada", target: "England", weight: 1 },
            { source: "Mexico", target: "Portugal", weight: 9 },
            { source: "Mexico", target: "Spain", weight: 5 }
        ];
    });

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

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

In this case, the Sankey uses the $watch listener instead of the default $watchCollection listener. Note that this can impact the Sankey's peformance.

See Also

Knockout

Declare the array observable and bind the dataSource property to it. Whenever an item is added or removed from this array, the Sankey is updated accordingly.

HTML
JavaScript
<div id="sankeyContainer" data-bind="dxSankey: {
    dataSource: sankeyData
}"></div>
var viewModel = {
    fruits: ko.observableArray([
        { source: "Brazil", target: "Spain", weight: 4 },
        { source: "Brazil", target: "Portugal", weight: 5 },
        { source: "Brazil", target: "England", weight: 2 },
        { source: "Canada", target: "Portugal", weight: 2 },
        { source: "Canada", target: "England", weight: 1 },
        { source: "Mexico", target: "Portugal", weight: 9 },
        { source: "Mexico", target: "Spain", weight: 5 }
    ])
};

ko.applyBindings(viewModel);
See Also