Skip to content

Commit 8d8f4e9

Browse files
authored
Add support for custom data structure (#75)
* Add support for custom data structure * Use proper array for length
1 parent fcfd781 commit 8d8f4e9

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,37 @@ const chart = new Chart(ctx, {
9494
});
9595
```
9696

97+
### Custom data structure
98+
99+
Custom data structure can be used by specifying the custom data keys in `options.parsing`.
100+
For example:
101+
102+
```js
103+
const chart = new Chart(ctx, {
104+
type: 'sankey',
105+
data: {
106+
datasets: [
107+
{
108+
data: [
109+
{source: 'a', destination: 'b', value: 20},
110+
{source: 'c', destination: 'd', value: 10},
111+
{source: 'c', destination: 'e', value: 5},
112+
],
113+
colorFrom: 'red',
114+
colorTo: 'green'
115+
}
116+
]
117+
},
118+
options: {
119+
parsing: {
120+
from: 'source',
121+
to: 'destination',
122+
flow: 'value'
123+
}
124+
}
125+
});
126+
```
127+
97128
## Example
98129

99130
![Sankey Example Image](sankey.png)

src/controller.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ export default class SankeyController extends DatasetController {
8181
* @return {Array<SankeyParsedData>}
8282
*/
8383
parseObjectData(meta, data, start, count) {
84+
const {from: fromKey = 'from', to: toKey = 'to', flow: flowKey = 'flow'} = this.options.parsing;
85+
const sankeyData = data.map(({[fromKey]: from, [toKey]: to, [flowKey]: flow}) => ({from, to, flow}));
8486
const {xScale, yScale} = meta;
8587
const parsed = []; /* Array<SankeyParsedData> */
86-
const nodes = this._nodes = buildNodesFromRawData(data);
88+
const nodes = this._nodes = buildNodesFromRawData(sankeyData);
8789
/* getDataset() => SankeyControllerDatasetOptions */
8890
const {column, priority, size} = this.getDataset();
8991
if (priority) {
@@ -102,13 +104,13 @@ export default class SankeyController extends DatasetController {
102104
}
103105
}
104106

105-
const {maxX, maxY} = layout(nodes, data, !!priority, validateSizeValue(size));
107+
const {maxX, maxY} = layout(nodes, sankeyData, !!priority, validateSizeValue(size));
106108

107109
this._maxX = maxX;
108110
this._maxY = maxY;
109111

110-
for (let i = 0, ilen = data.length; i < ilen; ++i) {
111-
const dataPoint = data[i];
112+
for (let i = 0, ilen = sankeyData.length; i < ilen; ++i) {
113+
const dataPoint = sankeyData[i];
112114
const from = nodes.get(dataPoint.from);
113115
const to = nodes.get(dataPoint.to);
114116
const fromY = from.y + getAddY(from.to, dataPoint.to, i);

test/fixtures/parsing.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
2+
config: {
3+
type: 'sankey',
4+
data: {
5+
datasets: [
6+
{
7+
data: [
8+
{source: 'a', destination: 'b', value: 20},
9+
{source: 'c', destination: 'd', value: 10},
10+
{source: 'c', destination: 'e', value: 5},
11+
],
12+
colorFrom: 'red',
13+
colorTo: 'green'
14+
}
15+
]
16+
},
17+
options: {
18+
parsing: {
19+
from: 'source',
20+
to: 'destination',
21+
flow: 'value'
22+
}
23+
}
24+
},
25+
options: {
26+
canvas: {
27+
height: 256,
28+
width: 512
29+
}
30+
}
31+
};

test/fixtures/parsing.png

21.5 KB
Loading

0 commit comments

Comments
 (0)