Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <title>Test graph</title>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
- <style>
- canvas {
- -moz-user-select: none;
- -webkit-user-select: none;
- -ms-user-select: none;
- }
- </style>
- </head>
- <body>
- <div style="width:75%;">
- <canvas id="myChart" width="920px" height="320px"></canvas>
- </div>
- <button id="randomizeData">Randomize Data</button>
- <script>
- window.chartColors = {
- red: 'rgb(255, 99, 132)',
- orange: 'rgb(255, 159, 64)',
- yellow: 'rgb(255, 205, 86)',
- green: 'rgb(75, 192, 192)',
- blue: 'rgb(54, 162, 235)',
- purple: 'rgb(153, 102, 255)',
- grey: 'rgb(201, 203, 207)'
- };
- window.randomScalingFactor = function() {
- return Math.random() * 200 - 100;
- };
- window.randomData = function() {
- var arr = [];
- for (var i=0;i<12;i++){
- arr.push(randomScalingFactor());
- }
- return arr;
- };
- var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
- var config = {
- type: 'line',
- data: {
- labels: MONTHS,
- datasets: [{
- label: 'My First dataset',
- backgroundColor: window.chartColors.red,
- borderColor: window.chartColors.red,
- data: randomData(),
- fill: false,
- }, {
- label: 'My Second dataset',
- fill: false,
- backgroundColor: window.chartColors.blue,
- borderColor: window.chartColors.blue,
- data: randomData(),
- }]
- },
- options: {
- responsive: false,
- maintainAspectRatio: false,
- title: {
- display: true,
- text: 'Test graph'
- },
- tooltips: {
- mode: 'index',
- intersect: false,
- },
- hover: {
- mode: 'nearest',
- intersect: false
- },
- scales: {
- xAxes: [{
- display: true,
- scaleLabel: {
- display: true,
- labelString: 'Month'
- }
- }],
- yAxes: [{
- display: true,
- scaleLabel: {
- display: true,
- labelString: 'Value'
- }
- }]
- }
- }
- };
- window.onload = function() {
- var ctx = document.getElementById('myChart').getContext('2d');
- window.myLine = new Chart(ctx, config);
- };
- document.getElementById('randomizeData').addEventListener('click', function() {
- config.data.datasets.forEach(function(dataset) {
- dataset.data = dataset.data.map(function() {
- return randomScalingFactor();
- });
- });
- window.myLine.update();
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment