簡體   English   中英

在JavaScript數據中傳遞JSON值

[英]passing JSON value in JavaScript data

我有一個外部JavaScript文件,該文件以JSON格式返回PHP響應,並且嘗試並檢查了其輸出,是否希望將其傳遞給canvasjs的數據點

這是我的json輸出經過檢查並測試返回的JavaScript所期望的

{ x: 1, y: 59 },{ x: 2, y: 93 },{ x: 3, y: 477 },{ x: 4, y: 506 }

被存儲在sthtml變量中。 這是代碼的一部分

var html = '';
var sthtml = '';
var ndhtml = '', downlo;
for (var i = 0; i < data.length; i++) {
    z = i + 1;
    downlo = data[i];
    html += '<tr id="' + i + '"><td>' + z + '</td></tr>';
    sthtml += '' + downlo.stchart + '';
    ndhtml += '' + downlo.ndchart + '';
}
$('#down-btn').html(html);

現在我將此ndhtml和sthtml將此sthtml和ndhtml變量值傳遞到頁面javascript中。 我嘗試了``+ sthtml +''和所有其他可能的值,但是數據沒有返回並且我的圖形為空

<script type="text/javascript">
window.onload = function() {
    var sthtml = [];
    var ndhtml = [];
    var chart = new CanvasJS.Chart("chartContainer", {
        axisY: {},

        data: [{
            dataPoints: [sthtml]
        }, {
            dataPoints: [ndhtml]
        }],

        legend: {
            cursor: "pointer",
            itemclick: function(e) {
                chart.render();
            }
        }
    });
    chart.render();
}
</script>

我認為當我加載該頁面的時間為空時,稍后在用戶提交表單時填充這些值,因此我們需要通過一些推送再次更新這些值

任何幫助將非常有用,我如何在上述JavaScript數據點中傳遞sthtml和ndhtml值

用這個

<script type="text/javascript">
window.onload = function() {
    var sthtml = '';
    var ndhtml = '';
    var chart = new CanvasJS.Chart("chartContainer", {
        axisY: {},

        data: [{
            dataPoints: [sthtml]
        }, {
            dataPoints: [ndhtml]
        }],

        legend: {
            cursor: "pointer",
            itemclick: function(e) {
                chart.render();
            }
        }
    });
    chart.render();
}
</script>

在onload函數中,您將變量設置為數組,但是如果您閱讀該文檔,則需要Joson數據

data: [
        {
            type: "column", //change type to bar, line, area, pie, etc
            dataPoints: [
                { x: 10, y: 71 },
                { x: 20, y: 55 },
                { x: 30, y: 50 },
                { x: 40, y: 65 },
                { x: 50, y: 95 },
                { x: 60, y: 68 },
                { x: 70, y: 28 },
                { x: 80, y: 34 },
                { x: 90, y: 14 }
            ]
        }
        ]

在數據對象內部,您需要一個dataPoints對象,在dataPoints對象中,您的json數據應放在其中。 試試這個,我希望它能起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM