gpt4 book ai didi

javascript - Node.js、express、jade、highcharts 和二维数组

转载 作者:行者123 更新时间:2023-11-30 12:40:15 25 4
gpt4 key购买 nike

免责声明,我对 javascript/node.js/express/jade/highcharts 等还是很陌生...

我的问题
我有一个模板,它接收我在我的路由器代码中预处理的几个参数。参数被分成 1 个对象,代表元数据和系列数据的 Highcharts 。

当我尝试将二维数组传递到代表我的数据系列的 jade 模板时遇到问题,但在 chrome 开发工具中它看起来像一个普通数组。我的 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'
}
}
}]
});
});

#{seriesData} 参数是 res.seriesData,它应该是一个二维数组。在 delivered.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'
}
}
}]
});

抱歉这个问题太长了,提前致谢!!

最佳答案

最后,我做了别的事情。而不是在 delivered.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'
}
}
}]
});
});

关于javascript - Node.js、express、jade、highcharts 和二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com