Skip to content

Commit

Permalink
Update ChartModule for Chart.js v3
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH authored and jackiekazil committed Dec 19, 2021
1 parent 8e523cd commit cda0495
Showing 1 changed file with 92 additions and 92 deletions.
184 changes: 92 additions & 92 deletions mesa/visualization/templates/js/ChartModule.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
var ChartModule = function(series, canvas_width, canvas_height) {
// Create the tag:
var canvas_tag = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
// Append it to #elements
var canvas = $(canvas_tag)[0];
$("#elements").append(canvas);
// Create the context and the drawing controller:
var context = canvas.getContext("2d");

var convertColorOpacity = function(hex) {

if (hex.indexOf('#') != 0) {
return 'rgba(0,0,0,0.1)';
}

hex = hex.replace('#', '');
r = parseInt(hex.substring(0, 2), 16);
g = parseInt(hex.substring(2, 4), 16);
b = parseInt(hex.substring(4, 6), 16);
return 'rgba(' + r + ',' + g + ',' + b + ',0.1)';
};

// Prep the chart properties and series:
var datasets = []
for (var i in series) {
var s = series[i];
var new_series = {
label: s.Label,
borderColor: s.Color,
backgroundColor: convertColorOpacity(s.Color),
data: []
};
datasets.push(new_series);
}

var chartData = {
labels: [],
datasets: datasets
};

var chartOptions = {
responsive: true,
tooltips: {
mode: 'index',
intersect: false
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true
},
ticks: {
maxTicksLimit: 11
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true
}
}]
}
};

var chart = new Chart(context, {
type: 'line',
data: chartData,
options: chartOptions
});

this.render = function(data) {
chart.data.labels.push(control.tick);
for (i = 0; i < data.length; i++) {
chart.data.datasets[i].data.push(data[i]);
}
chart.update();
};

this.reset = function() {
while (chart.data.labels.length) { chart.data.labels.pop(); }
chart.data.datasets.forEach(function(dataset) {
while (dataset.data.length) { dataset.data.pop(); }
});
chart.update();
};
};
var ChartModule = function(series, canvas_width, canvas_height) {
// Create the tag:
var canvas_tag = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
// Append it to #elements
var canvas = $(canvas_tag)[0];
$("#elements").append(canvas);
// Create the context and the drawing controller:
var context = canvas.getContext("2d");

var convertColorOpacity = function(hex) {

if (hex.indexOf('#') != 0) {
return 'rgba(0,0,0,0.1)';
}

hex = hex.replace('#', '');
r = parseInt(hex.substring(0, 2), 16);
g = parseInt(hex.substring(2, 4), 16);
b = parseInt(hex.substring(4, 6), 16);
return 'rgba(' + r + ',' + g + ',' + b + ',0.1)';
};

// Prep the chart properties and series:
var datasets = []
for (var i in series) {
var s = series[i];
var new_series = {
label: s.Label,
borderColor: s.Color,
backgroundColor: convertColorOpacity(s.Color),
data: []
};
datasets.push(new_series);
}

var chartData = {
labels: [],
datasets: datasets
};

var chartOptions = {
responsive: true,
tooltips: {
mode: 'index',
intersect: false
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
x: {
display: true,
title: {
display: true
},
ticks: {
maxTicksLimit: 11
}
},
y: {
display: true,
title: {
display: true
}
}
}
};

var chart = new Chart(context, {
type: 'line',
data: chartData,
options: chartOptions
});

this.render = function(data) {
chart.data.labels.push(control.tick);
for (i = 0; i < data.length; i++) {
chart.data.datasets[i].data.push(data[i]);
}
chart.update();
};

this.reset = function() {
while (chart.data.labels.length) { chart.data.labels.pop(); }
chart.data.datasets.forEach(function(dataset) {
while (dataset.data.length) { dataset.data.pop(); }
});
chart.update();
};
};

0 comments on commit cda0495

Please sign in to comment.