skip to Main Content

I’m trying to use JSCharts.

block content
| <div id="chartcontainer">This is just a replacement in case Javascript is not available or used for SEO purposes</div>

script.
    var myData=new Array()
    var myData = new Array([10, 20], [15, 10], [20, 30], [25, 10], [30, 5]);
    var myChart = new JSChart('chartcontainer', 'line');
    myChart.setDataArray(myData);
    myChart.draw();

I created an array dynamically in nodejs

[ [10, 20], [15, 10], [20, 30], [25, 10], [30, 5] ]

and then passed it to jade.

res.render('chart',{newArray:array});

However, I’m unable to use it in the inline function mentioned above.

I tried using var myData="#{newArray}" but it just gives out a single string like 10,20,15,10,20,30... which is unusable.

I also tried sending Json object. But it was also unusable after "#{myJson}" and/or stringify

What can I do? Also, is there any way I can use this script outside script. block? Because it wasn’t able to find JSChart() when I tried that even though I have included the file in head. If that is possible then I can iterate the json and do something.

2

Answers


  1. Chosen as BEST ANSWER

    Right now I'm using this solution:

    block content
    | <div id="chartcontainer">This is just a replacement in case Javascript is not available or used for SEO purposes</div>
    
    -var myJson=JSON.stringify(json);
    script.
        var array = new Array();
        var myData=JSON.parse(("#{myJson}").replace(/&quot;/g,'"'));
        myData.forEach(function(data)
        {
            array.push([data.x,data.y]);
        })
        var myChart = new JSChart('chartcontainer', 'line');
        myChart.setDataArray(array);
        myChart.draw();
    

    But it seems really stupid and unnecessary processing; Creating json in node and then sending it to jade, converting json to string and the again json and then array.


  2. You can use Unescaped String Interpolation in JADE within script tags.

    Simply use the !{} tags.

    var array = new Array(!{newArray})
    

    Should work fine

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search