line chart with ChartJS
HTML
<script src="https://rawgit.com/nnnick/Chart.js/master/Chart.min.js"></script>
<canvas id="LineExt" width="600" height="300"></canvas>
JavaScript
var data = {
labels: ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'],
datasets: [{
strokeColor: 'rgba(0, 190, 242, 1)',
fillColor: 'rgba(177, 235, 250, 1)',
data: [12, 30, 20, 10, 30, 50, 70, 76, 80, 90, 95]
}]
};
var ctx = document.getElementById("LineExt").getContext("2d");
Chart.types.Line.extend({
name: "LineExt",
initialize: function () {
Chart.types.Line.prototype.initialize.apply(this, arguments);
// hide alternate x labels on the scale
var xLabels = this.scale.xLabels;
xLabels.forEach(function (xLabel, i) {
if (i % 2)
xLabels[i] = '';
})
},
draw: function () {
Chart.types.Line.prototype.draw.apply(this, arguments);
var scale = this.scale;
var points = this.datasets[0].points;
var ctx = this.chart.ctx;
var options = this.options;
ctx.save();
var y0 = scale.calculateY(0);
this.options.points.forEach(function (point) {
// linearly extrapolate y value from nearby points
point.y = (points[Math.floor(point.xIndex)].value + points[Math.ceil(point.xIndex)].value) / 2;
var x = scale.calculateX(point.xIndex);
var y = scale.calculateY(point.y);
// draw the circle
ctx.beginPath();
ctx.arc(x, y, 5, 0, 2 * Math.PI);
ctx.fillStyle = point.color;
ctx.fill();
if (point.dropLine) {
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x, y0);
ctx.setLineDash([5, 5]);
ctx.lineWidth = 2;
ctx.strokeStyle = point.color;
ctx.stroke();
// use the same function that chart.js uses to draw the tooltip to draw our label
var text = Chart.helpers.template(options.boxTemplate, { value: point.y })
var...