123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <!doctype html>
- <html>
- <head>
- <title>Bubble Chart</title>
- <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
- <script src="../dist/Chart.bundle.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 randomScalingFactor = function() {
- return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
- };
- var randomColorFactor = function() {
- return Math.round(Math.random() * 255);
- };
- var randomColor = function() {
- return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',.7)';
- };
- var bubbleChartData = {
- animation: {
- duration: 10000
- },
- datasets: [{
- label: "My First dataset",
- backgroundColor: randomColor(),
- 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: randomColor(),
- 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'
- },
- }
- });
- };
- $('#randomizeData').click(function() {
- var zero = Math.random() < 0.2 ? true : false;
- $.each(bubbleChartData.datasets, function(i, dataset) {
- dataset.backgroundColor = randomColor();
- dataset.data = dataset.data.map(function() {
- return {
- x: randomScalingFactor(),
- y: randomScalingFactor(),
- r: Math.abs(randomScalingFactor()) / 5,
- };
- });
- });
- window.myChart.update();
- });
- $('#addDataset').click(function() {
- ++addedCount;
- var newDataset = {
- label: "My added dataset " + addedCount,
- backgroundColor: randomColor(),
- 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();
- });
- $('#addData').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();
- }
- });
- $('#removeDataset').click(function() {
- bubbleChartData.datasets.splice(0, 1);
- window.myChart.update();
- });
- $('#removeData').click(function() {
- bubbleChartData.datasets.forEach(function(dataset, datasetIndex) {
- dataset.data.pop();
- });
- window.myChart.update();
- });
- </script>
- </body>
- </html>
|