|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<head> |
| 5 | + <title>Time Scale Point Data</title> |
| 6 | + <script src="https://cdn.jsdelivr.net/npm/moment@2.24.0/moment.min.js"></script> |
| 7 | + <script src="../../../dist/Chart.min.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 style="width:75%;"> |
| 20 | + <canvas id="canvas"></canvas> |
| 21 | + </div> |
| 22 | + <br> |
| 23 | + <br> |
| 24 | + <button id="randomizeData">Randomize Data</button> |
| 25 | + <button id="addData">Add Data</button> |
| 26 | + <button id="removeData">Remove Data</button> |
| 27 | + <script> |
| 28 | + function newDate(days) { |
| 29 | + return moment().add(days, 'd').toDate(); |
| 30 | + } |
| 31 | + |
| 32 | + function newDateString(days) { |
| 33 | + return moment().add(days, 'd').format(); |
| 34 | + } |
| 35 | + |
| 36 | + var color = Chart.helpers.color; |
| 37 | + var config = { |
| 38 | + type: 'line', |
| 39 | + data: { |
| 40 | + datasets: [{ |
| 41 | + label: 'Dataset with string point data', |
| 42 | + backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(), |
| 43 | + borderColor: window.chartColors.red, |
| 44 | + fill: false, |
| 45 | + data: [{ |
| 46 | + x: newDateString(0), |
| 47 | + y: randomScalingFactor() |
| 48 | + }, { |
| 49 | + x: newDateString(2), |
| 50 | + y: randomScalingFactor() |
| 51 | + }, { |
| 52 | + x: newDateString(4), |
| 53 | + y: randomScalingFactor() |
| 54 | + }, { |
| 55 | + x: newDateString(6), |
| 56 | + y: randomScalingFactor() |
| 57 | + }], |
| 58 | + }, { |
| 59 | + label: 'Dataset with date object point data', |
| 60 | + backgroundColor: color(window.chartColors.blue).alpha(0.5).rgbString(), |
| 61 | + borderColor: window.chartColors.blue, |
| 62 | + fill: false, |
| 63 | + data: [{ |
| 64 | + x: newDate(0), |
| 65 | + y: randomScalingFactor() |
| 66 | + }, { |
| 67 | + x: newDate(2), |
| 68 | + y: randomScalingFactor() |
| 69 | + }, { |
| 70 | + x: newDate(5), |
| 71 | + y: randomScalingFactor() |
| 72 | + }, { |
| 73 | + x: newDate(6), |
| 74 | + y: randomScalingFactor() |
| 75 | + }] |
| 76 | + }] |
| 77 | + }, |
| 78 | + options: { |
| 79 | + spanGaps: 1000 * 60 * 60 * 24 * 2, // 2 days |
| 80 | + responsive: true, |
| 81 | + title: { |
| 82 | + display: true, |
| 83 | + text: 'Chart.js Time - spanGaps: 172800000 (2 days in ms)' |
| 84 | + }, |
| 85 | + scales: { |
| 86 | + x: { |
| 87 | + type: 'time', |
| 88 | + display: true, |
| 89 | + scaleLabel: { |
| 90 | + display: true, |
| 91 | + labelString: 'Date' |
| 92 | + }, |
| 93 | + ticks: { |
| 94 | + major: { |
| 95 | + fontStyle: 'bold', |
| 96 | + fontColor: '#FF0000' |
| 97 | + } |
| 98 | + } |
| 99 | + }, |
| 100 | + y: { |
| 101 | + display: true, |
| 102 | + scaleLabel: { |
| 103 | + display: true, |
| 104 | + labelString: 'value' |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + window.onload = function() { |
| 112 | + var ctx = document.getElementById('canvas').getContext('2d'); |
| 113 | + window.myLine = new Chart(ctx, config); |
| 114 | + }; |
| 115 | + |
| 116 | + document.getElementById('randomizeData').addEventListener('click', function() { |
| 117 | + config.data.datasets.forEach(function(dataset) { |
| 118 | + dataset.data.forEach(function(dataObj) { |
| 119 | + dataObj.y = randomScalingFactor(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + window.myLine.update(); |
| 124 | + }); |
| 125 | + document.getElementById('addData').addEventListener('click', function() { |
| 126 | + if (config.data.datasets.length > 0) { |
| 127 | + config.data.datasets[0].data.push({ |
| 128 | + x: newDateString(config.data.datasets[0].data.length + 2), |
| 129 | + y: randomScalingFactor() |
| 130 | + }); |
| 131 | + config.data.datasets[1].data.push({ |
| 132 | + x: newDate(config.data.datasets[1].data.length + 2), |
| 133 | + y: randomScalingFactor() |
| 134 | + }); |
| 135 | + |
| 136 | + window.myLine.update(); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + document.getElementById('removeData').addEventListener('click', function() { |
| 141 | + config.data.datasets.forEach(function(dataset) { |
| 142 | + dataset.data.pop(); |
| 143 | + }); |
| 144 | + |
| 145 | + window.myLine.update(); |
| 146 | + }); |
| 147 | + </script> |
| 148 | +</body> |
| 149 | + |
| 150 | +</html> |
0 commit comments