Skip to content

Commit

Permalink
Add example of scriptable polar area chart
Browse files Browse the repository at this point in the history
  • Loading branch information
janelledement authored and Simon Brunel committed Feb 10, 2019
1 parent cc3829b commit b500b6d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions samples/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@
}, {
title: 'Line Chart',
path: 'scriptable/line.html'
}, {
title: 'Polar Area Chart',
path: 'scriptable/polar.html'
}]
}, {
title: 'Advanced',
Expand Down
98 changes: 98 additions & 0 deletions samples/scriptable/polar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scriptable > Polar Area | Chart.js sample</title>
<link rel="stylesheet" type="text/css" href="../style.css">
<script src="../../dist/Chart.min.js"></script>
<script src="../utils.js"></script>
</head>
<body>
<div class="content">
<div class="wrapper"><canvas id="chart-0"></canvas></div>
<div class="toolbar">
<button onclick="randomize()">Randomize</button>
<button onclick="addData()">Add Data</button>
<button onclick="removeData()">Remove Data</button>
</div>
</div>
<script>
var DATA_COUNT = 7;

var utils = Samples.utils;

utils.srand(110);

function colorize(opaque, hover, ctx) {
var v = ctx.dataset.data[ctx.dataIndex];
var c = v < 35 ? '#D60000'
: v < 55 ? '#F46300'
: v < 75 ? '#0358B6'
: '#44DE28';

var opacity = hover ? 1 - Math.abs(v / 150) - 0.2 : 1 - Math.abs(v / 150);

return opaque ? c : utils.transparentize(c, opacity);
}

function hoverColorize(ctx) {
return colorize(false, true, ctx);
}

function generateData() {
return utils.numbers({
count: DATA_COUNT,
min: 0,
max: 100
});
}

var data = {
datasets: [{
data: generateData(),
}]
};

var options = {
legend: false,
tooltips: false,
elements: {
arc: {
backgroundColor: colorize.bind(null, false, false),
hoverBackgroundColor: hoverColorize
}
}
};

var chart = new Chart('chart-0', {
type: 'polarArea',
data: data,
options: options
});

// eslint-disable-next-line no-unused-vars
function randomize() {
chart.data.datasets.forEach(function(dataset) {
dataset.data = generateData();
});
chart.update();
}

// eslint-disable-next-line no-unused-vars
var addData = function() {
var newData = Math.round(Math.random() * 100);
chart.data.datasets[0].data.push(newData);
chart.update();
};

// eslint-disable-next-line no-unused-vars
function removeData() {
chart.data.datasets[0].data.pop();
chart.update();
}

</script>
</body>
</html>

0 comments on commit b500b6d

Please sign in to comment.