|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>Box Plot Chart</title> |
| 5 | + <script src="https://unpkg.com/chart.js@3.0.0-alpha/dist/Chart.js"></script> |
| 6 | + <script src="../build/Chart.BoxPlot.js" type="text/javascript"></script> |
| 7 | + <script src="https://unpkg.com/d3-random@latest/dist/d3-random.js"></script> |
| 8 | + <script src="./utils.js"></script> |
| 9 | + <style> |
| 10 | + canvas { |
| 11 | + -moz-user-select: none; |
| 12 | + -webkit-user-select: none; |
| 13 | + -ms-user-select: none; |
| 14 | + } |
| 15 | + </style> |
| 16 | + </head> |
| 17 | + |
| 18 | + <body> |
| 19 | + <div id="container" style="width: 75%;"> |
| 20 | + <canvas id="canvas"></canvas> |
| 21 | + </div> |
| 22 | + <button id="randomizeData">Randomize Data</button> |
| 23 | + <button id="addDataset">Add Dataset</button> |
| 24 | + <button id="removeDataset">Remove Dataset</button> |
| 25 | + <button id="addData">Add Data</button> |
| 26 | + <button id="removeData">Remove Data</button> |
| 27 | + <script> |
| 28 | + const samples = this.Samples.utils; |
| 29 | + var color = Chart.helpers.color; |
| 30 | + var b = d3.randomNormal(); |
| 31 | + var random = (min, max) => () => b() * ((max || 1) - (min || 0)) + (min || 0); |
| 32 | + var boxplotData = { |
| 33 | + labels: samples.months({ count: 7 }), |
| 34 | + datasets: [ |
| 35 | + { |
| 36 | + label: 'Dataset 1', |
| 37 | + backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(), |
| 38 | + borderColor: window.chartColors.red, |
| 39 | + borderWidth: 1, |
| 40 | + itemRadius: 2, |
| 41 | + data: samples.boxplots({ count: 7, random: random }), |
| 42 | + outlierColor: '#999999', |
| 43 | + }, |
| 44 | + { |
| 45 | + label: 'Dataset 2', |
| 46 | + backgroundColor: color(window.chartColors.blue).alpha(0.5).rgbString(), |
| 47 | + borderColor: window.chartColors.blue, |
| 48 | + borderWidth: 1, |
| 49 | + itemRadius: 2, |
| 50 | + data: samples.boxplotsArray({ count: 7, random: random }), |
| 51 | + outlierColor: '#999999', |
| 52 | + lowerColor: '#461e7d', |
| 53 | + }, |
| 54 | + ], |
| 55 | + }; |
| 56 | + |
| 57 | + window.onload = function () { |
| 58 | + var ctx = document.getElementById('canvas').getContext('2d'); |
| 59 | + window.myBar = new Chart(ctx, { |
| 60 | + type: 'boxplot', |
| 61 | + data: boxplotData, |
| 62 | + options: { |
| 63 | + responsive: true, |
| 64 | + legend: { |
| 65 | + position: 'top', |
| 66 | + }, |
| 67 | + title: { |
| 68 | + display: true, |
| 69 | + text: 'Chart.js Box Plot Chart', |
| 70 | + }, |
| 71 | + }, |
| 72 | + }); |
| 73 | + }; |
| 74 | + |
| 75 | + document.getElementById('randomizeData').addEventListener('click', function () { |
| 76 | + boxplotData.datasets.forEach(function (dataset) { |
| 77 | + dataset.data = samples.boxplots({ count: 7 }); |
| 78 | + }); |
| 79 | + window.myBar.update(); |
| 80 | + }); |
| 81 | + |
| 82 | + var colorNames = Object.keys(window.chartColors); |
| 83 | + document.getElementById('addDataset').addEventListener('click', function () { |
| 84 | + var colorName = colorNames[boxplotData.datasets.length % colorNames.length]; |
| 85 | + var dsColor = window.chartColors[colorName]; |
| 86 | + var newDataset = { |
| 87 | + label: 'Dataset ' + boxplotData.datasets.length, |
| 88 | + backgroundColor: color(dsColor).alpha(0.5).rgbString(), |
| 89 | + borderColor: dsColor, |
| 90 | + borderWidth: 1, |
| 91 | + data: samples.boxplots({ count: boxplotData.labels.length }), |
| 92 | + }; |
| 93 | + |
| 94 | + boxplotData.datasets.push(newDataset); |
| 95 | + window.myBar.update(); |
| 96 | + }); |
| 97 | + |
| 98 | + document.getElementById('addData').addEventListener('click', function () { |
| 99 | + if (boxplotData.datasets.length > 0) { |
| 100 | + var month = samples.nextMonth(boxplotData.labels.length); |
| 101 | + boxplotData.labels.push(month); |
| 102 | + |
| 103 | + for (var index = 0; index < boxplotData.datasets.length; ++index) { |
| 104 | + //window.myBar.addData(randomBoxPlot(), index); |
| 105 | + boxplotData.datasets[index].data.push(samples.randomBoxPlot()); |
| 106 | + } |
| 107 | + |
| 108 | + window.myBar.update(); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + document.getElementById('removeDataset').addEventListener('click', function () { |
| 113 | + boxplotData.datasets.splice(0, 1); |
| 114 | + window.myBar.update(); |
| 115 | + }); |
| 116 | + |
| 117 | + document.getElementById('removeData').addEventListener('click', function () { |
| 118 | + boxplotData.labels.splice(-1, 1); // remove the label first |
| 119 | + |
| 120 | + boxplotData.datasets.forEach(function (dataset, datasetIndex) { |
| 121 | + dataset.data.pop(); |
| 122 | + }); |
| 123 | + |
| 124 | + window.myBar.update(); |
| 125 | + }); |
| 126 | + </script> |
| 127 | + </body> |
| 128 | +</html> |
0 commit comments