Skip to content

Instantly share code, notes, and snippets.

@jeetu7
Created July 17, 2016 13:55
Show Gist options
  • Save jeetu7/e9ae9deec68ed0d5b94b11a5e7a0ff6e to your computer and use it in GitHub Desktop.
Save jeetu7/e9ae9deec68ed0d5b94b11a5e7a0ff6e to your computer and use it in GitHub Desktop.
mesa ChartModule.js for upgrading to v2 of Chart.js. I am not able to go any further.
// This function creates the Chart widget. If updating the Chart.js library
// shipped with mesa, check the compatibility and modify the function as
// required.
//
// Chart.js version: 2.1.6
// input arguments:
// series: Data to be plotted
// canvas_width: the width of the html5 canvas
// canvas_height: the height of the html5 canvas
var ChartModule = function(series, canvas_width, canvas_height) {
// Create the elements
// Create the tag:
var canvas_tag = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
// Append it to body:
var canvas = $(canvas_tag)[0];
$("body").append(canvas);
// Create the context and the drawing controller:
var context = canvas.getContext("2d");
// Prep the chart properties and series:
//var datasets = [{
// // The label(title) of the whole data
// label: "MyData",
// fillColor: "rgba(151,187,205,0.5)",
// strokeColor: "rgba(151,187,205,0.8)",
// highlightFill: "rgba(151,187,205,0.75)",
// highlightStroke: "rgba(151,187,205,1)",
// // The y value in in y = f(x) graph
// data: [10,2,3,4,5]
// }];
var datasets = []
for (var i in series) {
var s = series[i];
var new_series = {label: s.Label, strokeColor: s.Color, data: []};
datasets.push(new_series);
}
//var datasets = []
//for (var i in series) {
// var s = series[i];
// var new_series = {label: s.Label, strokeColor: s.Color, data: []};
// datasets.push(new_series);
//}
var lineChartData = {
// The x-axis labels in y=f(x) plots.
// These are just labels, not the actual values.
// Its our responsibility to keep the mapping.
//labels: [5, 4, 3, 20, 1],
labels: [],
datasets: datasets,
};
//var options = {
// animation: false,
// datasetFill: false,
// pointDot: false,
// bezierCurve : false
//};
// Options to be passed to the Chart. These are known as config in the
// documentation of Chart.js
var options = {
type: 'line',
responsive: true,
tension: 0.1,
fill: false,
data: lineChartData,
scales: {
xAxes: [{
ticks: {
beginAtZero: false,
//suggestedMin: 1,
}
}]
}
};
var chart = new Chart(context, options)
this.render = function(data) {
for (var i in data){
lineChartData.datasets[0].data.push(data[i])
}
lineChartData.labels.push(control.tick)
chart.update()
};
//this.render = function(data) {
// chart.addData(data, control.tick);
//};
this.reset = function() {
chart.destroy();
data.labels = [];
chart = new Chart(context, options)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment