Skip to content

Instantly share code, notes, and snippets.

@typpo
Last active March 17, 2020 23:06
Show Gist options
  • Save typpo/838a6a7fd6f95ca968646847973901d1 to your computer and use it in GitHub Desktop.
Save typpo/838a6a7fd6f95ca968646847973901d1 to your computer and use it in GitHub Desktop.
Build a chart.js config as string with function substitution
const chartObj = {
type: 'bar',
data: {
labels: ['January', 'February', 'March', 'April', 'May'],
datasets: [{
label: 'Dogs',
data: [ 50, 60, 70, 180, 190 ]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
callback: '{{CALLBACK_PLACEHOLDER}}',
}
}],
},
},
};
const myFormatterFunction = function(value) {
return "$" + value
};
const chartStr = JSON.stringify(chartObj).replace('"{{CALLBACK_PLACEHOLDER}}"', myFormatterFunction.toString());
console.log(encodeURIComponent(chartStr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment