Skip to content

Commit 042920e

Browse files
committed
Update mixed chart example
1 parent a01f580 commit 042920e

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

samples/charts/mixed-chart.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,94 @@
117117
console.log(charts);
118118
});
119119
</script>
120+
<script>
121+
var parents = ['scatter', 'line', 'bubble', 'bar', 'horizontalBar', undefined];
122+
var childs = [
123+
{type:'scatter', color:'#f844'},
124+
{type:'scatter', color:'#f844'},
125+
{type:'line', color:'#4f84', fill:false},
126+
{type:'bubble', color:'#8f44'},
127+
{type:'line', color:'#f484'},
128+
//{type:'bar', color:'#48f4'},
129+
//{type:'bar', color:'#84f4'},
130+
//{type:'horizontalBar', color:'#84f4'} // not supported yet
131+
]
132+
var size = 8;
133+
var charts = [];
134+
parents.forEach(function(parent) {
135+
var datasets = [];
136+
var c = -1;
137+
var labels = [];
138+
childs.forEach(function(child) {
139+
c++;
140+
var data = [];
141+
for(var i = 0 ; i < size ; i++) {
142+
if(child.type === 'bubble') {
143+
data.push({x:randomScalingFactor(), y:randomScalingFactor(),r:Math.max(randomScalingFactor()/2, 20)});
144+
} else {
145+
data.push(randomScalingFactor());
146+
}
147+
if(!c) {
148+
labels.push('label '+(i+1));
149+
}
150+
}
151+
datasets.push({
152+
type: child.type,
153+
label: child.type,
154+
borderColor: child.color,
155+
backgroundColor: child.color,
156+
data: data,
157+
});
158+
if(child.fill !== undefined) {
159+
datasets[datasets.length-1].fill = child.fill;
160+
}
161+
});
162+
var div = document.createElement("div");
163+
var canvas = document.createElement("canvas");
164+
canvas.id = parent;
165+
div.appendChild(canvas);
166+
document.getElementById("container").appendChild(div);
167+
var str = '';
168+
childs.forEach(function(child) {
169+
str += child.type + ', ';
170+
})
171+
str = str.slice(0,-2);
172+
charts.push(new Chart(canvas.getContext("2d"), {
173+
type: parent,
174+
data: {
175+
labels: labels,
176+
datasets: datasets,
177+
},
178+
options: {
179+
responsive: true,
180+
title: {
181+
display: true,
182+
text: 'Main type : ' + parent + ' | Childs : ' + str,
183+
},
184+
tooltips: {
185+
mode: 'index',
186+
intersect: true
187+
}
188+
}
189+
}));
190+
});
191+
console.log(charts);
192+
document.getElementById('randomizeData').addEventListener('click', function() {
193+
194+
charts.forEach(function(chart) {
195+
chart.data.datasets.forEach(function(dataset) {
196+
dataset.data = dataset.data.map(function(a) {
197+
if(a.r) {
198+
return {x:randomScalingFactor(),y:randomScalingFactor(),r:Math.max(randomScalingFactor()/3, 10)};
199+
}
200+
return randomScalingFactor();
201+
});
202+
});
203+
chart.update();
204+
});
205+
console.log(charts);
206+
});
207+
</script>
120208
</body>
121209

122210
</html>

0 commit comments

Comments
 (0)