Chart.js - difficulty populating arrays from dataset



  • Hi all ... I am trying to use chart.js in my report and am having difficulty populating the arrays for labels and data. Additionally, when I hard-code the arrays the chart runs, but not to completion.

    I thought I could just use a handlebars #each statement to populate the arrays, but that doesn't appear to be the case, and examples of how to do this are sparse and don't appear to fit my use case, which is primarily doing this in the designer so I can just call the render from node. Seems a lot of examples are doing things like calling handlebars.compile and using JQuery ... not necessarily the solution I'm after unless it's the ONLY one.

    Here's a snippet of my code between the script tags

            let customersArr = [];
    //        let customersDataArr = [51, 325402, 524, 23565];
            let customersDataArr = [];
            {{#each smsMain.data}}
                customersArr.push({{this.company}});
                customersDataArr.push({{this.deliveredCount}})
            {{/each}}
    
            new Chart("CustomerSummary", {
                type: "bar",
                data: {
    

    Here is a link to my playground example - https://playground.jsreport.net/w/dqueubed/vkEiLVmu

    As always, thanks for much for your assist!

     - Roger




  • Thanks @jan_blaha ... I'll pour over these and see what I can find. Always much appreciated :-)



  • @jan_blaha ... just a quick follow up, this worked great

            let customersArr = [{{#each smsMain.data}}"{{this.company}}",{{/each}}];
            let customersDataArr = [{{#each smsMain.data}}{{this.deliveredCount}},{{/each}}];
    

    As you can see, for the string array I had to force quotation marks and commas, but after that, everything worked great!

    0_1686150619460_upload-f8dfbe40-4a99-4cd2-bc82-2623f497ba27

    As always, you guys are right on the money with your solutions. Can't thank you guys enough, and I'm certain I'll have more. :-)



  • As you can see, for the string array I had to force quotation marks and commas, but after that, everything worked great!

    There are some string chars that can break your code. Therefore toJS helper is recommended.

    As always, you guys are right on the money with your solutions. Can't thank you guys enough, and I'm certain I'll have more. :-)

    You are more than welcome.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.