Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 1bf6496

Browse files
committed
fix #15 scriptable example
1 parent 97b08ba commit 1bf6496

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

samples/scriptable.html

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<title>Hierarchical Bar Chart</title>
6+
<script src="https://unpkg.com/chart.js/dist/Chart.bundle.js"></script>
7+
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3.min.js"></script>
8+
<script src="../build/Chart.Hierarchical.js" type="text/javascript"></script>
9+
<style>
10+
canvas {
11+
-moz-user-select: none;
12+
-webkit-user-select: none;
13+
-ms-user-select: none;
14+
}
15+
16+
</style>
17+
</head>
18+
19+
<body>
20+
<div id="container" style="width: 75%;">
21+
<canvas id="canvas"></canvas>
22+
</div>
23+
<script>
24+
const data = {
25+
// define label tree
26+
labels: [
27+
'A',
28+
{
29+
label: 'B1',
30+
expand: false, // 'focus', // expand level
31+
children: [
32+
'B1.1',
33+
{
34+
label: 'B1.2',
35+
children: ['B1.2.1', 'B1.2.2']
36+
},
37+
'B1.3'
38+
]
39+
},
40+
{
41+
label: 'C1',
42+
children: ['C1.1', 'C1.2', 'C1.3', 'C1.4']
43+
},
44+
'D'
45+
],
46+
datasets: [{
47+
label: 'Test',
48+
// store as the tree attribute for reference, the data attribute will be automatically managed
49+
tree: [
50+
1,
51+
{
52+
value: 2,
53+
children: [
54+
3,
55+
{
56+
value: 4,
57+
children: [4.1, 4.2]
58+
},
59+
5
60+
]
61+
},
62+
{
63+
value: 6,
64+
children: [7, 8, 9, 10]
65+
},
66+
11
67+
]
68+
}]
69+
};
70+
71+
const scale = d3.scaleSequential(d3.interpolateBlues).domain([0, 11]);
72+
73+
window.onload = () => {
74+
const ctx = document.getElementById("canvas").getContext("2d");
75+
window.myBar = new Chart(ctx, {
76+
type: 'bar',
77+
data: data,
78+
options: {
79+
responsive: true,
80+
title: {
81+
display: true,
82+
text: 'Chart.js Hierarchical Bar Chart'
83+
},
84+
layout: {
85+
padding: {
86+
// add more space at the bottom for the hierarchy
87+
bottom: 60
88+
}
89+
},
90+
scales: {
91+
xAxes: [{
92+
type: 'hierarchical',
93+
// offset setings, for centering the categorical axis in the bar chart case
94+
offset: true,
95+
96+
// grid line settings
97+
gridLines: {
98+
offsetGridLines: true
99+
},
100+
101+
attributes: {
102+
backgroundColor: (ctx) => {
103+
const v = ctx.dataset.data[ctx.dataIndex];
104+
return scale(v);
105+
},
106+
}
107+
}],
108+
yAxes: [{
109+
ticks: {
110+
beginAtZero: true
111+
}
112+
}]
113+
}
114+
}
115+
});
116+
117+
};
118+
119+
</script>
120+
</body>
121+
122+
</html>

src/plugin/hierarchical.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const HierarchicalPlugin = {
131131

132132
Object.keys(attributes).forEach((attr) => {
133133
chart.data.datasets.forEach((d) => {
134-
d[attr] = nodes.map((n) => {
134+
const v = nodes.map((n) => {
135135
while (n) {
136136
if (n.hasOwnProperty(attr)) {
137137
return n[attr];
@@ -141,6 +141,9 @@ const HierarchicalPlugin = {
141141
}
142142
return attributes[attr]; // default value
143143
});
144+
145+
// check if all values are the same, if so replace with a single value
146+
d[attr] = v.length >= 1 && v.every((vi) => vi === v[0]) ? v[0] : v;
144147
});
145148
});
146149
},

0 commit comments

Comments
 (0)