mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-06 20:29:12 +02:00
b7139dbbef
Samples are supposed to show good practices and in most cases we don't use the time scale but require `Chart.bundle.js`, which is not correct. Instead, we should require the non-bundled version in its minified version (`Chart.min.js`). Paradoxically, time based examples don't use `Chart.bundle.js` but require moment manually side to `Chart.min.js`, which IMO is also the correct way since it allows users to configure and use moment globally (TZ, locales, etc.) and doesn't enforce a specific moment version. Also remove the `data-labelling.html` example because we now have an [official plugin](https://github.com/chartjs/chartjs-plugin-datalabels) that implements this feature and don't want to deal with user custom code anymore.
192 lines
5.0 KiB
HTML
192 lines
5.0 KiB
HTML
<!doctype html>
|
|
<html>
|
|
|
|
<head>
|
|
<title>Bubble Chart</title>
|
|
<script src="../../dist/Chart.min.js"></script>
|
|
<script src="../utils.js"></script>
|
|
<style type="text/css">
|
|
canvas{
|
|
-moz-user-select: none;
|
|
-webkit-user-select: none;
|
|
-ms-user-select: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div id="container" style="width: 75%;">
|
|
<canvas id="canvas"></canvas>
|
|
</div>
|
|
<button id="randomizeData">Randomize Data</button>
|
|
<button id="addDataset">Add Dataset</button>
|
|
<button id="removeDataset">Remove Dataset</button>
|
|
<button id="addData">Add Data</button>
|
|
<button id="removeData">Remove Data</button>
|
|
<script>
|
|
var DEFAULT_DATASET_SIZE = 7;
|
|
|
|
var addedCount = 0;
|
|
var color = Chart.helpers.color;
|
|
var bubbleChartData = {
|
|
animation: {
|
|
duration: 10000
|
|
},
|
|
datasets: [{
|
|
label: 'My First dataset',
|
|
backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
|
|
borderColor: window.chartColors.red,
|
|
borderWidth: 1,
|
|
data: [{
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}]
|
|
}, {
|
|
label: 'My Second dataset',
|
|
backgroundColor: color(window.chartColors.orange).alpha(0.5).rgbString(),
|
|
borderColor: window.chartColors.orange,
|
|
borderWidth: 1,
|
|
data: [{
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}, {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
}]
|
|
}]
|
|
};
|
|
|
|
window.onload = function() {
|
|
var ctx = document.getElementById('canvas').getContext('2d');
|
|
window.myChart = new Chart(ctx, {
|
|
type: 'bubble',
|
|
data: bubbleChartData,
|
|
options: {
|
|
responsive: true,
|
|
title: {
|
|
display: true,
|
|
text: 'Chart.js Bubble Chart'
|
|
},
|
|
tooltips: {
|
|
mode: 'point'
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
document.getElementById('randomizeData').addEventListener('click', function() {
|
|
bubbleChartData.datasets.forEach(function(dataset) {
|
|
dataset.data = dataset.data.map(function() {
|
|
return {
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
};
|
|
});
|
|
});
|
|
window.myChart.update();
|
|
});
|
|
|
|
var colorNames = Object.keys(window.chartColors);
|
|
document.getElementById('addDataset').addEventListener('click', function() {
|
|
++addedCount;
|
|
var colorName = colorNames[bubbleChartData.datasets.length % colorNames.length];
|
|
var dsColor = window.chartColors[colorName];
|
|
var newDataset = {
|
|
label: 'My added dataset ' + addedCount,
|
|
backgroundColor: color(dsColor).alpha(0.5).rgbString(),
|
|
borderColor: dsColor,
|
|
borderWidth: 1,
|
|
data: []
|
|
};
|
|
|
|
for (var index = 0; index < DEFAULT_DATASET_SIZE; ++index) {
|
|
newDataset.data.push({
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
});
|
|
}
|
|
|
|
bubbleChartData.datasets.push(newDataset);
|
|
window.myChart.update();
|
|
});
|
|
|
|
document.getElementById('addData').addEventListener('click', function() {
|
|
if (bubbleChartData.datasets.length > 0) {
|
|
for (var index = 0; index < bubbleChartData.datasets.length; ++index) {
|
|
bubbleChartData.datasets[index].data.push({
|
|
x: randomScalingFactor(),
|
|
y: randomScalingFactor(),
|
|
r: Math.abs(randomScalingFactor()) / 5,
|
|
});
|
|
}
|
|
|
|
window.myChart.update();
|
|
}
|
|
});
|
|
|
|
document.getElementById('removeDataset').addEventListener('click', function() {
|
|
bubbleChartData.datasets.splice(0, 1);
|
|
window.myChart.update();
|
|
});
|
|
|
|
document.getElementById('removeData').addEventListener('click', function() {
|
|
bubbleChartData.datasets.forEach(function(dataset) {
|
|
dataset.data.pop();
|
|
});
|
|
|
|
window.myChart.update();
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|