Skip to content

Instantly share code, notes, and snippets.

@andregardi
Last active September 30, 2018 20:34
Show Gist options
  • Save andregardi/dd4b5a3b9c4704c3d0f72f13ec8c8e8f to your computer and use it in GitHub Desktop.
Save andregardi/dd4b5a3b9c4704c3d0f72f13ec8c8e8f to your computer and use it in GitHub Desktop.
function float2dollar(value) {
return "U$ " + (value).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}
function renderChart(data, labels) {
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'This week',
data: data[0],
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
},
{
label: 'Last week',
data: data[1],
borderColor: 'rgba(192, 192, 192, 1)',
backgroundColor: 'rgba(192, 192, 192, 0.2)',
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value, index, values) {
return float2dollar(value);
}
}
}]
},
}
});
}
function getChartData() {
$("#loadingMessage").html('<img src="./giphy.gif" alt="" srcset="">');
$.ajax({
url: "http://localhost:3000/chartdata",
success: function (result) {
$("#loadingMessage").html("");
var data = [];
data.push(result.thisWeek);
data.push(result.lastWeek);
var labels = result.labels;
renderChart(data, labels);
},
error: function (err) {
$("#loadingMessage").html("Error");
}
});
}
$("#renderBtn").click(
function () {
getChartData();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment