Web Services js读取json并将其加载到数组中[已关闭]

9nvpjoqh  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(109)

**已关闭。**此问题为not reproducible or was caused by typos。它目前不接受回答。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
3天前关闭。
Improve this question
从PHP中的一个简单的Web服务,我生成了一个JSON,我想传递给另一个页面来加载一个数组。生成的json如下:

[{"x":1686736391,"y":19.06},{"x":1686746674,"y":19.06},{"x":1686767695,"y":19.68}]

字符串
其中“x”是mysql日期的unix_timestamp,y是整数值
在第二页,我想用canvasjs创建一个图,然后运行以下代码:

<!DOCTYPE HTML>
<html>
    <head>  
        <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
        <script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
        <script>
            window.onload = function () {

                var dataPoints = [];

                var chart = new CanvasJS.Chart("chartContainer", {
                    animationEnabled: true,
                    title:{
                        text: "title"
                    },
                    axisY: {
                        title: "values",
                        titleFontSize: 24,
                        prefix: "$"
                    },
                    data: [{
                        type: "line",
                        yValueFormatString: "$#,##0.00",
                        dataPoints: dataPoints
                    }]
                });

                function addData(data) {
                    for (var i = 0; i < data.length; i++) {
                        dataPoints.push({
                            x: new Date(data[i][0]),
                            y: data[i][1]
                        });
                    }
                    console.log(dataPoints);
                    chart.render();
                }

                $.getJSON("https://mywebsitesite.com/webservice.php?id=1", addData);
            }
        </script>
    </head>

    <body>
        <div id="chartContainer" style="height: 300px; width: 100%;"></div>
    </body>
</html>


但是console.log(dataPoints)结果给出了x的无效数据和y的undefined:


的数据
我做错了什么?提前谢谢

zyfwsgd6

zyfwsgd61#

你有一个键为“x”和“y”的对象,但在你的代码中,你试图使用data[i][0]和data[i][1]来访问这些值。相反,您应该使用data[i].x和data[i]. y。

<!DOCTYPE HTML>
<html>
    <head>  
        <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
        <script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
        <script>
            window.onload = function () {

                var dataPoints = [];

                var chart = new CanvasJS.Chart("chartContainer", {
                    animationEnabled: true,
                    title: {
                        text: "title"
                    },
                    axisY: {
                        title: "values",
                        titleFontSize: 24,
                        prefix: "$"
                    },
                    data: [{
                        type: "line",
                        yValueFormatString: "$#,##0.00",
                        dataPoints: dataPoints
                    }]
                });

                function addData(data) {
                    for (var i = 0; i < data.length; i++) {
                        dataPoints.push({
                            x: new Date(data[i].x * 1000), // Convert Unix timestamp to milliseconds
                            y: data[i].y
                        });
                    }
                    console.log(dataPoints);
                    chart.render();
                }

                $.getJSON("https://mywebsitesite.com/webservice.php?id=1", addData);
            }
        </script>
    </head>

    <body>
        <div id="chartContainer" style="height: 300px; width: 100%;"></div>
    </body>
</html>

字符串

相关问题