【问题标题】:json file offline can read by chart.js, but online json can't readjson文件离线可以被chart.js读取,但是在线json无法读取
【发布时间】:2017-07-12 07:08:29
【问题描述】:

我的chart.js有问题,如果我读取离线json文件,图表会出现,但如果我使用Firebase的在线json,它不会显示任何东西,只是白屏。

我在在线模式下使用它之前从 firebase 导出 json 文件,但它不会显示任何内容。

这是我的代码,只有一页。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Chart JSON</title>
    <!--<style>
        canvas{
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
        }
    </style>-->
    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    <script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
    </head>
    <body>
        <!--<div class="container" style="width: 90%;">-->
        <div class="container" style="width: 400px, height 400px;">
            <canvas id="canvas">
            </canvas>
        </div>
        <p id="jsjs"></p>
        <script>
            var lm = document.getElementById('jsjs');
            //$.getJSON('sensormini.json', function (json) {
            //$.getJSON('sensor.json', function (json) {
            //$.getJSON('export.json', function (json) {
            $.getJSON('https://skripsi-adeguntoro.firebaseio.com/sensor.json', function (json) {
                //console.log([json]);
                console.log(json);
                lm.innerText = JSON.stringify(json);
                var suhu = json.map(function(item) {
                    return item.temp;
                });
                var time = json.map(function(item) {
                    return item.time;
                });
                //console.log([suhu]);
                console.log(suhu);
                var time2 = [time]
                var suhu2 = [suhu]
                var config = {
                    type: 'line',
                    data: {
                        labels: time,
                        datasets: [{
                            label: "Suhu",
                            backgroundColor: "rgba(220,220,220,1)",
                            fill: false,
                            data: suhu
                        }]
                    },
                    options: {
                        responsive: true,
                        title:{
                            display:true,
                            text:'Suhu Ruang Server'
                        },
                        events: ['mousemove'],
                        hover: {
                            mode: 'nearest',
                            intersect: true
                        },
                        scales: {
                            xAxes: [{
                                display: true,
                                scaleLabel: {
                                    display: true,
                                    labelString: 'Jam'
                                },
                                ticks:{
                                    //autoSkip: true,
                                    //maxTicksLimit: 5
                                    //showXLabels: 10
                                }
                            }],
                            yAxes: [{
                                display: true,
                                scaleLabel: {
                                    display: true,
                                    labelString: 'Suhu'
                                },
                                ticks: {
                                    //beginAtZero: false,
                                    //reverse:false,
                                    stepSize: 5
                                    //autoSkip: true
                                }
                            }]
                        }
                    }
                };
                window.onload = function() {
                    var ctx = document.getElementById("canvas").getContext("2d");
                    window.myLine = new Chart(ctx, config);
                };
            });
        </script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
    </body>
</html>

在控制台上,一切都是一样的输出 Online JSON

这里是离线 JSONOffline JSON

【问题讨论】:

    标签: javascript jquery json firebase


    【解决方案1】:

    只需删除 window.onload = function() {}$.getJSON()

    因为窗口在获取 JSON 之前已经加载

    【讨论】:

    • 啊,谢谢你救了我。但是限制数据怎么样?如您所见,我有很多数据,如何在chart.js中限制我的数据?例如我只想显示10个数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多