【问题标题】:json in to a variable and then pass itjson 到一个变量中,然后传递它
【发布时间】:2012-05-01 21:00:00
【问题描述】:

如果我们有这样的代码

 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 但它并没有真正起作用

【问题讨论】:

  • 你想做什么?你为什么使用JSON.stringify? “将变量传递给 [an] 对象”是什么意思?

标签: javascript jquery json highcharts highstock


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2019-07-19
      • 1970-01-01
      相关资源
      最近更新 更多