chart.js in an #each loop



  • Hi
    I need to create several diagrams by placing them in a loop.
    I have looked at the example and there a trigger is fired when the animation is finished.
    Would this also work if I disable the animations and do without the trigger? Apparently not, or I'm doing something wrong.
    If I really have to work with the trigger, how do I do it with several diagrams in the loop? I don't have to trigger it until all the diagrams are done.
    I still use version 2.10.0
    Thank you.



  • Disabling animations often work, however, some libraries still do some async work so it sometimes doesn't.
    The printing trigger and some on-finished event of the library is typically the best approach.
    Firing trigger after multiple charts are printed needs just some coding.
    The idea is to have a counter you decrement when a chart is printed. When you count all the charts printed, fire the trigger.

    var numberOfChartsToPrint = datasets.length
    for (var dataset of datasets) {
        chartingLibrary.printChart({dataset}, {
             onComplete: () => {
                  if (--numberOfChartsToPrint === 0) {
                       window.JSREPORT_READY_TO_START = true                    
                  }
            }
       })
    }
    


  • Super. Thank you. I have now done it similarly but with Promise.
    I just had other errors in it and that's why I had such difficulties.


Log in to reply
 

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