我想用javascript在我的页面上显示一个线条图。显示图表的示例代码如下所示
new Chartist.Line('#chart-with-area', {
labels: ["d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"],
series: [
[5, 9, 7, 8, 5, 3, 5, 4]
]
}, {
low: 0,
showArea: true,
plugins: [
Chartist.plugins.tooltip()
]
});
我正在使用laravel框架,我需要将数据从php发送到javascript。
我创建了两个数组,一个用于标签,另一个用于数据。
问题是,当我为标签添加数组时,我面临以下错误
htmlspecialchars():参数#1 ($string)必须是字符串类型,数组给定
当我发送json时,javascript会更改双quot
new Chartist.Line('#chart-with-area', {
labels: ["2021\/7\/5","2018\/11\/17","2019\/1\/8","2018\/10\/25","2019\/4\/9","2018\/11\/18","2019\/3\/11","2019\/1\/3","2019\/1\/5","2018\/12\/17","2021\/5\/25","2018\/12\/31"],
series: [[1,1,1,1,1,1,1,3,1,1,2,6]]
}, {
low: 0,
showArea: true,
plugins: [
Chartist.plugins.tooltip()
]
});
这是我的代码:
new Chartist.Line('#chart-with-area', {
labels: {{json_encode($requestLineChartLabel)}},
series: [{{json_encode($requestLineChartData,JSON_NUMERIC_CHECK)}}]
}, {
low: 0,
showArea: true,
plugins: [
Chartist.plugins.tooltip()
]
});
我找了很多次,但找不到任何解决办法。
发布于 2021-07-06 07:20:34
如果使用的是Laravel 7或更高版本,则可以使用@json刀片指令正确地输出json:
labels: @json($requestLineChartLabel),
series: [@json($requestLineChartData,JSON_NUMERIC_CHECK)]
只需确保这些变量已经不是json,否则您将对其进行双重编码。
https://stackoverflow.com/questions/68273179
复制相似问题