Comments (4)
Hi @TheSnoozer,
You can POST chart definitions (including shorturl creation) that include Javascript functions by sending a JSON object while serializing the "chart" param as a string.
{
"chart": "{type: \"bar\", ..., function(x) { return y }",
"myOtherOption": "xyz"
}
The key is that the POSTed object is valid JSON, and the type of the "chart" value is string. Note that you'll have to escape the chart config accordingly.
I'm closing this issue because the original question is really a matter of developer preference as to how one might serialize their chart config as a string. I've included a few recommendations above, but ultimately string templating etc is beyond the scope of the service and could make things more confusing. In the end, if the chart is not JSON, the definition has to come through as a string and then Quickchart will attempt to safely eval it.
from quickchart.
Hi Taylor,
I realize it's a bit tricky to construct strings containing functions. I'll think about this problem and other potential solutions, but here are a few options to construct config strings. See options 1 and 3 in particular:
- Build the config as a string, not an object. This is the most straightforward way:
const chartStr = `{
type: 'bar',
data: {
labels: ['January', 'February', 'March', 'April', 'May'],
datasets: [{
label: 'Dogs',
data: [ 50, 60, 70, 180, 190 ]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
callback: function(value) {
return '$' + value;
}
}
}],
},
},
}`;
console.log(encodeURIComponent(chartStr));
- The string approach makes it more difficult to build and modify the object. Another option is to construct it in object format excluding the functions, and then substitute the functions later. This is basically what you are doing:
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}}',
}
}],
},
},
};
// ... modify chartObj as a normal javascript object ...
const chartStr = JSON.stringify(chartObj).replace('"{{CALLBACK_PLACEHOLDER}}"', 'function(value) { return "$" + value }');
console.log(encodeURIComponent(chartStr));
- Unfortunately the above is a bit ugly. You can make it all happen neatly by using a library like javascript-stringify:
const { stringify } = require('javascript-stringify');
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: function(value) {
return '$' + value;
}
}
}],
},
},
};
console.log(encodeURIComponent(stringify(chartObj)));
from quickchart.
Thanks for your reply. Would it be easy-ish to parse the input as a regular JSON object and then go back, looking explicitly for where there is a callback which is currently a string and then EVAL'ing it there?
To be honest I'd call the string method the more technically correct solution in this situation but I think I'll continue using the 'hack'-ey one with the replacer because loosing the Object is not compatible with what I'm doing nor would it make it very pleasant to edit.
JS-stringify actually looks good too but it's another package too add. Not that bad in the scheme of things I guess.
from quickchart.
Thank you for this awesome service...not sure if it is obvious, but when creating short URLs (via POST to https://quickchart.io/chart/create) the service seems to require a valid JSON and thus short URLs currently would lack the ability to define call-back functions.
E.g. posting a RFC 4627 valid Json results in an Image telling that function(value, index, values){return "foo bar";}
is not a valid function (yes it's a string).
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"chart": {"type": "bar", "data": {"labels": ["Hello", "World"], "datasets": [{"label": "Foo", "data": [1, 2]}]}, "options": {"scales": {"yAxes": [{"type": "linear", "ticks": {"beginAtZero": true, "callback": "function(value, index, values){return \"foo bar\";}" } }] } } }}' \
https://quickchart.io/chart/create
When attempting to post it as actual function (invalid Json as per RFC 4627) one gets a 500 Internal Server Error
curl -X POST \
-H 'Content-Type: application/json' \
-d '{"chart": {"type": "bar", "data": {"labels": ["Hello", "World"], "datasets": [{"label": "Foo", "data": [1, 2]}]}, "options": {"scales": {"yAxes": [{"type": "linear", "ticks": {"beginAtZero": true, "callback": function(value, index, values){return "foo bar";} } }] } } }}' \
https://quickchart.io/chart/create
Not sure what would the best option for this.
from quickchart.
Related Issues (20)
- CI feature: integrate pull request preview environments
- Cannot POST /graphviz
- How to install quickchart locally HOT 1
- Failed to run on GCP HOT 1
- Unable to install this on Ubuntu 23.04 nodeJS 18 LTS
- Unable to change scale using min/max in options HOT 1
- FEATURE Request: Export To PDF
- linear regression
- Support for datalabels in v3+ charts? HOT 4
- Transparant table background HOT 1
- tickFormat not working in self-hosted instance
- JSON Integration? HOT 1
- de-facto quickchart docker image ianw/quickchart appears abandoned HOT 1
- Unique tags for inline SVG HOT 2
- Adding Elestio as deployment option
- Docker image randomly stopped working
- Feature Request for QR Codes. Add the text option HOT 2
- Horizontal Grid Lines Not Working on Line Charts HOT 1
- gauge charts working in main domain, but not self-hosted instance
- Still maintained ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quickchart.