Node.js,express,jade,highcharts和2D array

时间:2014-07-12 15:37:22

标签: javascript node.js express highcharts pug

免责声明,我是javascript / node.js / express / jade / highcharts等新手......

对我的问题 我有一个模板,接收我在路由器代码中预处理的几个参数。 这些参数被分为1个对象,并代表元数据和系列数据的高图表图形。

当我尝试将2D数组传递给代表我的数据系列的jade模板时,我遇到了问题,但在chrome dev工具中,它看起来像普通数组。 我的Highcharts代码基于:Highcharts column graph

这是我的代码:

delivered.js

var callback = function process_results(result){
    var res = new Object()
    res.title = "Delivery Count"
    res.yAxisTitle = 'Delivered'
    res.seriesName = "Delivered per month"
    res.seriesData = []
    for (var i = 0; i < result.rows.length; i++) {
        //process the query results
        row = result.rows[i]
        date = "\'" + row.month_delivered + '/' + row.year_delivered + "\'"
        count = row.count
        res.seriesData.push([date,count])
    };
    console.log(res)
    response.render('column-graph', res)
}

column-graph.jade

html
    head
        link(rel='stylesheet', href='/stylesheets/style.css')
        script(type='text/javascript', src="http://code.jquery.com/jquery-1.9.1.min.js")
        script(type='text/javascript', src="http://code.highcharts.com/highcharts.js")
        script(type='text/javascript', src="http://code.highcharts.com/modules/exporting.js")
        title Delivered Leads
   body
    div#container(style="min-width: 500px; height: 500px; margin: 0 auto")
    script.
        $(function () {
            $('#container').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: "#{title}"
            },
            subtitle: {
                text: 'Source: Internal DB'
            },
            xAxis: {
                type: 'category',          
            labels: {
                rotation: -45,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                    }
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: "#{yAxisTitle}"
                }
            },
            legend: {
                enabled: false
            },
            series: [{
                name: '#{seriesName}',
                data: [#{seriesData}],
                dataGrouping: {
                    enabled: true
                },
                dataLabels: {
                    enabled: true,
                    rotation: -90,
                    color: '#FFFFFF',
                    align: 'right',
                        x: 4,
                        y: 10,
                        style: {
                            fontSize: '13px',
                            fontFamily: 'Verdana, sans-serif',
                            textShadow: '0 0 3px black'
                        }
                    }
                }]
            });
        });

#DataData}参数是res.seriesData,它应该是一个2D数组。 在deliver.js中,我有一个console.log(res),显示了这个:

{ title: 'Delivery Count',
  yAxisTitle: 'Delivered',
  seriesName: 'Delivered per month',
  seriesData:
   [ [ '\'7/2014\'', '3000' ],
     [ '\'6/2014\'', '5163' ],
     [ '\'5/2014\'', '23882' ],
     [ '\'4/2014\'', '26471' ],
     [ '\'3/2014\'', '82172' ],
     [ '\'2/2014\'', '31283' ],
     [ '\'1/2014\'', '637400' ],
     [ '\'12/2013\'', '86420' ],
     [ '\'11/2013\'', '119150' ],
     [ '\'10/2013\'', '49093' ] ] }

但是当我在浏览器中查看它时,我明白了这一点:

  series: [{
    name: 'Delivered per month',
    data: ['7/2014',3000,'6/2014',5163,'5/2014',23882,'4/2014',26471,'3/2014',82172,'2/2014',31283,'1/2014',637400,'12/2013',86420,'11/2013',119150,'10/2013',49093],
    dataGrouping: {
        enabled: true
    },
    dataLabels: {
        enabled: true,
        rotation: -90,
        color: '#FFFFFF',
        align: 'right',
        x: 4,
        y: 10,
        style: {
            fontSize: '13px',
            fontFamily: 'Verdana, sans-serif',
            textShadow: '0 0 3px black'
           }
       }
   }]
});

很抱歉这个问题很长, 并提前感谢!!

2 个答案:

答案 0 :(得分:3)

最终,我做了别的事情。 而不是在deliver.js中预处理数组,而不是 我将数组原样传递给jade模板并执行以下操作:

html
  head
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(type='text/javascript', src="http://code.jquery.com/jquery-1.9.1.min.js")
    script(type='text/javascript', src="http://code.highcharts.com/highcharts.js")
    script(type='text/javascript', src="http://code.highcharts.com/modules/exporting.js")
    title Delivered Leads      
  body
    div#container(style="min-width: 500px; height: 500px; margin: 0 auto")
    script.
        var arrToMultiArr = function(arr){
        var result = new Array()
        for (var i=0; i < arr.length; i+=2){
          var date = arr[i]
          var count = arr[i + 1]
          result.push([date, count])
        }
        return result
    };
    var mySeries = arrToMultiArr([#{seriesData}])

    $(function () {
        $('#container').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: "#{title}"
        },
        subtitle: {
            text: 'Source: Internal DB'
        },
        xAxis: {
            type: 'category',          
            labels: {
                rotation: -45,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: "#{yAxisTitle}"
            }
        },
        legend: {
            enabled: false
        },
        series: [{
            name: '#{seriesName}',
            data: mySeries,
            dataGrouping: {
                enabled: true
            },
            dataLabels: {
                enabled: true,
                rotation: -90,
                color: '#FFFFFF',
                align: 'right',
                x: 4,
                y: 10,
                style: {
                    fontSize: '11px',
                    fontFamily: 'Verdana, sans-serif',
                    textShadow: '0 0 3px black'
                }
            }
        }]
      });
    });

答案 1 :(得分:0)

尝试更改:

data: [#{seriesData}],

为:

data: #{seriesData},