JSON 输入到变量中,然后将其传递

json in to a variable and then pass it

本文关键字:然后 输入 变量 JSON      更新时间:2023-09-26

如果我们有这样的代码

 yAxis : {
            title : {
                text : 'Exchange rate'
            },
            plotLines : [{
                value : 0.6738,
                color : 'green',
                dashStyle : 'shortdash',
                width : 2,
                label : {
                    text : 'Last quarter minimum'
                }
            }, {
                value : 0.7419,
                color : 'red',
                dashStyle : 'shortdash',
                width : 2,
                label : {
                    text : 'Last quarter maximum'
                }
            }]
        },

我怎样才能获取绘图线 JSON 字符串并将其分配给一个变量并将该变量传递给绘图线对象???我尝试过这样的事情

 var jsonObj = [];
 jsonObj.push({ "value": 1256211571000, "color": '#D2691E', "width": 1, "label": {   "text": 'rinse'} });
        var myLines = JSON.stringify(jsonObj);

然后将Mylines传递给情节线,但它并没有真正起作用

假设这个对象在一个名为 data 的变量中,只需执行以下操作:

data.yAxis.plotLines.push({
    "value": 1256211571000,
    "color": '#D2691E',
    "width": 1,
    "label": {
        "text": 'rinse'
    }
});

这只是一个JavaScript对象,而不是JSON。 JSON 是对象(或数组)的字符串表示形式。

这是一个示例,如果它适合您'

$(document).ready(function() {

        $.getJSON('yourpage.php?getdata=y&compid=<?php echo $comp_details[0]['id']; ?>', function(data) {
            // split the data set into ohlc and volume
            var ohlc = [],
            volume = [],
            dataLength = data.length;
            for (i = 0; i < dataLength; i++) {
                //console.log(data[i][2]);
                ohlc.push([
                    data[i][0], // the date
                    data[i][1], // open
                    data[i][2], // high
                    data[i][3], // low
                    data[i][4] // close
                ]);
                volume.push([
                    data[i][0], // the date
                    data[i][5] // the volume
                ])
            }
            // set the allowed units for data grouping
            var groupingUnits = [[
                    'week',                         // unit name
                    [1]                             // allowed multiples
                ], [
                    'month',
                    [1, 2, 3, 4, 6]
                ]];
            // create the chart
              chart = new Highcharts.StockChart({
                chart: {
                    renderTo: 'container',
                    alignTicks: false,
                    zoomType: 'x'
                },
                rangeSelector: {
                    selected: 0
                },
                title: {
                    text:  '<?php echo strtoupper($_GET['CompanySymbol']); ?>'+' Historical'
                },
                yAxis: [{
                        title: {
                            text: 'OHLC'
                        },
                        height: 200,
                        lineWidth: 2,
                        min:0
                    }, {
                        title: {
                            text: 'Volume'
                        },
                        top: 300,
                        height: 100,
                        offset: 0,
                        lineWidth: 2
                    },
                    {
                        height: 200,
                        min: 0,
                        lineWidth: 2,
                        opposite: true
                    }],
                tooltip: {
                    crosshairs: [true,true],
                    shared: true
                },  
                series: [{
                        type: 'ohlc',
                        name: '<?php echo strtoupper($_GET['CompanySymbol']); ?>',
                        data: ohlc,
                        dataGrouping: {
                            units: groupingUnits
                        }
                    },
                    {
                        type: 'line',
                        name:'Fuerte',
                        yAxis: 2,
                        data:[<?php echo $newdata; ?>]
                    },
                    {
                        type: 'line',
                        name:'Debil',
                        yAxis: 2,
                        data:[<?php echo $newdata1; ?>]
                    },
                    {
                        type: 'column',
                        name: 'Volume',
                        data: volume,
                        yAxis: 1,
                        dataGrouping: {
                            units: groupingUnits
                        }
                    }]
            });
                chart.yAxis[2].setExtremes(10,90);
        });
    });'