gpt4 book ai didi

php - 人力车 : data for multiple series not working

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:08 26 4
gpt4 key购买 nike

我正在尝试使用 Rickshaw 创建一些图表, 使用在 php 中生成的 ajax 导入数据。

如果我使用静态数据,图表显示;如果我复制/粘贴在 php 中生成的数据(从控制台/日志()),图表显示;如果我尝试将数据放入一个变量中,并在 js 中使用该 var,它就不起作用。 :(

这是我从 .php 获得的控制台日志:(正如我所说,如果我将该代码块复制并粘贴到 .js 中以替换“dataOutEvo”var,则图表会正常显示。因此,我认为数据不是问题所在。

[
{
name: "ligne",
data: [{x:0,y:35},{x:1,y:34},{x:2,y:36},{x:3,y:35},{x:4,y:40},{x:5,y:35},{x:6,y:37},{x:7,y:40},{x:8,y:45},{x:9,y:46},{x:10,y:55},{x:11,y:63},{x:12,y:61},{x:13,y:45},{x:14,y:48},{x:15,y:49},{x:16,y:45},{x:17,y:44},{x:18,y:52},{x:19,y:43},{x:20,y:37},{x:21,y:36},{x:22,y:37},{x:23,y:34}],
color: palette.color()
},
{
name: "ligne",
data: [{x:0,y:10},{x:1,y:15},{x:2,y:13},{x:3,y:15},{x:4,y:14},{x:5,y:16},{x:6,y:17},{x:7,y:25},{x:8,y:23},{x:9,y:24},{x:10,y:25},{x:11,y:28},{x:12,y:27},{x:13,y:21},{x:14,y:23},{x:15,y:19},{x:16,y:18},{x:17,y:16},{x:18,y:15},{x:19,y:14},{x:20,y:15},{x:21,y:16},{x:22,y:15},{x:23,y:16}],
color: palette.color()
},
{
name: "ligne",
data: [{x:0,y:45},{x:1,y:49},{x:2,y:49},{x:3,y:50},{x:4,y:54},{x:5,y:51},{x:6,y:54},{x:7,y:65},{x:8,y:68},{x:9,y:70},{x:10,y:80},{x:11,y:91},{x:12,y:88},{x:13,y:66},{x:14,y:71},{x:15,y:68},{x:16,y:63},{x:17,y:60},{x:18,y:67},{x:19,y:57},{x:20,y:52},{x:21,y:52},{x:22,y:52},{x:23,y:50}],
color: palette.color()
},
{
name: "ligne",
data: [{x:0,y:10},{x:1,y:15},{x:2,y:12},{x:3,y:5},{x:4,y:9},{x:5,y:15},{x:6,y:45},{x:7,y:125},{x:8,y:345},{x:9,y:256},{x:10,y:312},{x:11,y:345},{x:12,y:299},{x:13,y:165},{x:14,y:354},{x:15,y:368},{x:16,y:254},{x:17,y:213},{x:18,y:312},{x:19,y:165},{x:20,y:54},{x:21,y:32},{x:22,y:10},{x:23,y:5}],
color: palette.color()
},
{
name: "ligne",
data: [{x:0,y:2},{x:1,y:3},{x:2,y:2},{x:3,y:1},{x:4,y:1},{x:5,y:2},{x:6,y:3},{x:7,y:15},{x:8,y:45},{x:9,y:27},{x:10,y:40},{x:11,y:42},{x:12,y:35},{x:13,y:18},{x:14,y:42},{x:15,y:40},{x:16,y:30},{x:17,y:25},{x:18,y:40},{x:19,y:20},{x:20,y:6},{x:21,y:4},{x:22,y:2},{x:23,y:1}],
color: palette.color()
}
]

这是 js 出错的地方:

$(document).ready(function(){ 

$.ajax({
url: 'dataOutEvo.php', //le fichier qui va nous fournir la réponse
success: function(data) {
var dataOutEvo = data;
console.log(dataOutEvo);
var palette = new Rickshaw.Color.Palette( { scheme: 'spectrum2001' } );
var graph = new Rickshaw.Graph({
element: document.querySelector("#chart"),
width: 960,
height: 260,
renderer: 'line',
series: dataOutEvo
});

graph.render();

}
});

});

谁能告诉我哪里出了问题?谢谢 :) 马修


我现在问自己,我是否不应该走另一条路,使用这个:

$fp = fopen('dataoutevo.json', 'w');
fwrite($fp, json_encode($js));
fclose($fp);

还有这个:

var palette = new Rickshaw.Color.Palette();
new Rickshaw.Graph.Ajax( {

element: document.getElementById("chart"),
width: 800,
height: 500,
renderer: 'line',
dataURL: 'dataoutevo.json',
onData: function(d) {
Rickshaw.Series.zeroFill(d);
return d;
},
onComplete: function(transport) {
var graph = transport.graph;
var detail = new Rickshaw.Graph.HoverDetail({ graph: graph });
}

} );

但它仍然无法正常工作......有没有人可以帮助我并告诉我我做错了什么?

最佳答案

使用您的第一个实现应该可以正常工作。我认为你的问题是当你打电话时:

success: function(data) {   

从 PHP 返回的数据变量实际上是一个字符串(您可以使用 Javascript 函数检查)-

console.log(typeof(data));

在您的 PHP 代码中,您应该返回一个(关联)数组并确保您使用的是 json_encode() 函数 -

echo json_encode($output);

然后在您的 JS 端使用 JSON.parse 方法转换返回的数据 -

var json_data = JSON.parse(data);

希望对您有所帮助!

关于php - 人力车 : data for multiple series not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329294/

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