Last active
March 17, 2020 23:06
-
-
Save typpo/838a6a7fd6f95ca968646847973901d1 to your computer and use it in GitHub Desktop.
Build a chart.js config as string with function substitution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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