简体   繁体   English

在JavaScript数据中传递JSON值

[英]passing JSON value in JavaScript data

i have one external JavaScript file which returns PHP response as JSON and i tried and check and its output is as desired i want to pass this data to datapoints of canvasjs 我有一个外部JavaScript文件,该文件以JSON格式返回PHP响应,并且尝试并检查了其输出,是否希望将其传递给canvasjs的数据点

here is my json output checked and tested returning as desired by JavaScript 这是我的json输出经过检查并测试返回的JavaScript所期望的

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

being stored in sthtml variable. 被存储在sthtml变量中。 here is that part of code 这是代码的一部分

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);

now i am passing this sthtml and ndhtml variable value to in page javascript with this ndhtml and sthtml. 现在我将此ndhtml和sthtml将此sthtml和ndhtml变量值传递到页面javascript中。 i tried ''+sthtml+'' and all other probable values but data is not returning and my graph is blank 我尝试了``+ 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>

i think when i load the page that time its empty and later these values are populated when users submit form so we need to update these again through some push 我认为当我加载该页面的时间为空时,稍后在用户提交表单时填充这些值,因此我们需要通过一些推送再次更新这些值

any help will be useful how can i pass sthtml and ndhtml values in above JavaScript datapoints 任何帮助将非常有用,我如何在上述JavaScript数据点中传递sthtml和ndhtml值

Use this 用这个

<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>

Inside onload function you are setting your variable as array but if you read the documentation on it requires a Joson data 在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 }
            ]
        }
        ]

Inside the data object you need an dataPoints object, Inside the dataPoints object your json data should go. 在数据对象内部,您需要一个dataPoints对象,在dataPoints对象中,您的json数据应放在其中。 Try this I hope it will work. 试试这个,我希望它能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM