json进入变量然后传递它

时间:2012-05-01 21:00:00

标签: javascript jquery json highcharts highstock

如果我们有这样的代码

 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'
                }
            }]
        },

我怎样才能将plotlines json字符串并将其赋值给变量并将此变量传递给plotline对象? 我试过这样的事情

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

然后将mylines传递给plotlines,但它没有真正起作用

2 个答案:

答案 0 :(得分:1)

假设此对象位于名为data的变量中,只需执行:

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

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

答案 1 :(得分:0)

这是一个例子,如果它适合你 “

$(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);
        });
    });'