JSReport - chart not rendering with dynamic values
I am able to fetch data through REST API using JSReport Script. I am able to display pie chart using template engine(recipe: html, engine: handlebars) with hardcoded values. Now i have to pass dynamic data from JSreport Script to Template engine. I am not sure how to do it.
Script
async function prepareDataSource() {
const extracts = await fetchStats() . // REST API Call
// console.log(extracts.data);
let arr = extracts.data
let statsbySale = d3.nest()
.rollup(function(d) {
return {
A : d3.sum(d, function (g) { return g.a; }),
B : d3.sum(d, function (g) { return g.b; }),
C : d3.sum(d, function (g) { return g.c; }),
D : d3.sum(d, function (g) { return g.d; })
};
}).object(arr);
console.log(JSON.stringify(statsbySale)); ==> returns ["A" : 10, "B" : 20, "C" : 30, "D" : 40]
return (JSON.stringify(statsbySale))
}
async function beforeRender(req, res, done) {
req.data.sales = await prepareDataSource()
console.log(req.data.sales======>Printed the same JSON object as above
done();
}
==================================
Template engine
<script>
Chart.defaults.global.legend.display = false;
new Chart(document.getElementById('myChart').getContext("2d"),
type: 'pie',
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
backgroundColor: [
'green',
'orange',
'yellow',
'purple'
]
borderColor: "rgba(27,161,226,1)",
borderWidth: 1,
hoverBackgroundColor: "rgba(27,161,226,0.4)",
hoverBorderColor: "rgba(27,161,226,1)",
data: [10,20,30,40] ===>Hardcoded value
}]
},
options: {
animation: {
duration:
}
}
});
</script>
How to pass the dynamic value from Script file to template engine to render the chart properly.
javascript node.js reactjs reporting jsreport
add a comment |
I am able to fetch data through REST API using JSReport Script. I am able to display pie chart using template engine(recipe: html, engine: handlebars) with hardcoded values. Now i have to pass dynamic data from JSreport Script to Template engine. I am not sure how to do it.
Script
async function prepareDataSource() {
const extracts = await fetchStats() . // REST API Call
// console.log(extracts.data);
let arr = extracts.data
let statsbySale = d3.nest()
.rollup(function(d) {
return {
A : d3.sum(d, function (g) { return g.a; }),
B : d3.sum(d, function (g) { return g.b; }),
C : d3.sum(d, function (g) { return g.c; }),
D : d3.sum(d, function (g) { return g.d; })
};
}).object(arr);
console.log(JSON.stringify(statsbySale)); ==> returns ["A" : 10, "B" : 20, "C" : 30, "D" : 40]
return (JSON.stringify(statsbySale))
}
async function beforeRender(req, res, done) {
req.data.sales = await prepareDataSource()
console.log(req.data.sales======>Printed the same JSON object as above
done();
}
==================================
Template engine
<script>
Chart.defaults.global.legend.display = false;
new Chart(document.getElementById('myChart').getContext("2d"),
type: 'pie',
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
backgroundColor: [
'green',
'orange',
'yellow',
'purple'
]
borderColor: "rgba(27,161,226,1)",
borderWidth: 1,
hoverBackgroundColor: "rgba(27,161,226,0.4)",
hoverBorderColor: "rgba(27,161,226,1)",
data: [10,20,30,40] ===>Hardcoded value
}]
},
options: {
animation: {
duration:
}
}
});
</script>
How to pass the dynamic value from Script file to template engine to render the chart properly.
javascript node.js reactjs reporting jsreport
add a comment |
I am able to fetch data through REST API using JSReport Script. I am able to display pie chart using template engine(recipe: html, engine: handlebars) with hardcoded values. Now i have to pass dynamic data from JSreport Script to Template engine. I am not sure how to do it.
Script
async function prepareDataSource() {
const extracts = await fetchStats() . // REST API Call
// console.log(extracts.data);
let arr = extracts.data
let statsbySale = d3.nest()
.rollup(function(d) {
return {
A : d3.sum(d, function (g) { return g.a; }),
B : d3.sum(d, function (g) { return g.b; }),
C : d3.sum(d, function (g) { return g.c; }),
D : d3.sum(d, function (g) { return g.d; })
};
}).object(arr);
console.log(JSON.stringify(statsbySale)); ==> returns ["A" : 10, "B" : 20, "C" : 30, "D" : 40]
return (JSON.stringify(statsbySale))
}
async function beforeRender(req, res, done) {
req.data.sales = await prepareDataSource()
console.log(req.data.sales======>Printed the same JSON object as above
done();
}
==================================
Template engine
<script>
Chart.defaults.global.legend.display = false;
new Chart(document.getElementById('myChart').getContext("2d"),
type: 'pie',
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
backgroundColor: [
'green',
'orange',
'yellow',
'purple'
]
borderColor: "rgba(27,161,226,1)",
borderWidth: 1,
hoverBackgroundColor: "rgba(27,161,226,0.4)",
hoverBorderColor: "rgba(27,161,226,1)",
data: [10,20,30,40] ===>Hardcoded value
}]
},
options: {
animation: {
duration:
}
}
});
</script>
How to pass the dynamic value from Script file to template engine to render the chart properly.
javascript node.js reactjs reporting jsreport
I am able to fetch data through REST API using JSReport Script. I am able to display pie chart using template engine(recipe: html, engine: handlebars) with hardcoded values. Now i have to pass dynamic data from JSreport Script to Template engine. I am not sure how to do it.
Script
async function prepareDataSource() {
const extracts = await fetchStats() . // REST API Call
// console.log(extracts.data);
let arr = extracts.data
let statsbySale = d3.nest()
.rollup(function(d) {
return {
A : d3.sum(d, function (g) { return g.a; }),
B : d3.sum(d, function (g) { return g.b; }),
C : d3.sum(d, function (g) { return g.c; }),
D : d3.sum(d, function (g) { return g.d; })
};
}).object(arr);
console.log(JSON.stringify(statsbySale)); ==> returns ["A" : 10, "B" : 20, "C" : 30, "D" : 40]
return (JSON.stringify(statsbySale))
}
async function beforeRender(req, res, done) {
req.data.sales = await prepareDataSource()
console.log(req.data.sales======>Printed the same JSON object as above
done();
}
==================================
Template engine
<script>
Chart.defaults.global.legend.display = false;
new Chart(document.getElementById('myChart').getContext("2d"),
type: 'pie',
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
backgroundColor: [
'green',
'orange',
'yellow',
'purple'
]
borderColor: "rgba(27,161,226,1)",
borderWidth: 1,
hoverBackgroundColor: "rgba(27,161,226,0.4)",
hoverBorderColor: "rgba(27,161,226,1)",
data: [10,20,30,40] ===>Hardcoded value
}]
},
options: {
animation: {
duration:
}
}
});
</script>
How to pass the dynamic value from Script file to template engine to render the chart properly.
javascript node.js reactjs reporting jsreport
javascript node.js reactjs reporting jsreport
edited Nov 26 '18 at 10:47
user3362908
asked Nov 26 '18 at 10:30
user3362908user3362908
667
667
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The solution can be found here.
You need to serialize json data using custom templating engine helper and write them to the inline script.
Define custom helper
function toJSON(data) {
return JSON.stringify(data);
}
Write data to the inline script
<script>
var data = {{{toJSON sales}}}
... your chart using data
</script>
It worked. Thank you so much.
– user3362908
Nov 26 '18 at 18:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479185%2fjsreport-chart-not-rendering-with-dynamic-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution can be found here.
You need to serialize json data using custom templating engine helper and write them to the inline script.
Define custom helper
function toJSON(data) {
return JSON.stringify(data);
}
Write data to the inline script
<script>
var data = {{{toJSON sales}}}
... your chart using data
</script>
It worked. Thank you so much.
– user3362908
Nov 26 '18 at 18:33
add a comment |
The solution can be found here.
You need to serialize json data using custom templating engine helper and write them to the inline script.
Define custom helper
function toJSON(data) {
return JSON.stringify(data);
}
Write data to the inline script
<script>
var data = {{{toJSON sales}}}
... your chart using data
</script>
It worked. Thank you so much.
– user3362908
Nov 26 '18 at 18:33
add a comment |
The solution can be found here.
You need to serialize json data using custom templating engine helper and write them to the inline script.
Define custom helper
function toJSON(data) {
return JSON.stringify(data);
}
Write data to the inline script
<script>
var data = {{{toJSON sales}}}
... your chart using data
</script>
The solution can be found here.
You need to serialize json data using custom templating engine helper and write them to the inline script.
Define custom helper
function toJSON(data) {
return JSON.stringify(data);
}
Write data to the inline script
<script>
var data = {{{toJSON sales}}}
... your chart using data
</script>
answered Nov 26 '18 at 16:42
Jan BlahaJan Blaha
1,8591227
1,8591227
It worked. Thank you so much.
– user3362908
Nov 26 '18 at 18:33
add a comment |
It worked. Thank you so much.
– user3362908
Nov 26 '18 at 18:33
It worked. Thank you so much.
– user3362908
Nov 26 '18 at 18:33
It worked. Thank you so much.
– user3362908
Nov 26 '18 at 18:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479185%2fjsreport-chart-not-rendering-with-dynamic-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown