【问题标题】:Highcharts Options to stringified JSON字符串化 JSON 的 Highcharts 选项
【发布时间】:2014-10-28 06:03:10
【问题描述】:

我需要将 highcharts options 转换为 JSON 字符串。我真的在为此苦苦挣扎。

因为它有cuircual结构。

示例:http://jsfiddle.net/8TEKD/

我也尝试使用 douglascrockford cycle.js 对其进行回收:https://github.com/douglascrockford/JSON-js

如果我尝试回收它,我会得到这个堆栈跟踪

'Attr.ownerElement' is deprecated and has been removed from DOM4 (http://w3.org/tr/dom). cycle.js:92
'Attr.textContent' is deprecated. Please use 'value' instead. cycle.js:92
'Attr.nodeValue' is deprecated. Please use 'value' instead. cycle.js:92
'HTMLHtmlElement.manifest' is deprecated. The manifest attribute only has an effect during the early stages of document load. cycle.js:92
InvalidStateError: Failed to read the 'selectionDirection' property from 'HTMLInputElement': The input element's type ('checkbox') does not support selection.

我需要这个,因为我想保存我的图表的配置,然后用那个配置加载它。

有没有办法做到这一点?

谢谢

【问题讨论】:

    标签: javascript json highcharts stringify


    【解决方案1】:

    问题在于$('#container').highcharts(options);renderTo 元素添加到作为DOM 元素的对象中。在添加之前,您没有循环结构。而且,如果您真的想保存配置选项以供再次使用,那么保存它们此时呈现的 DOM 元素对您没有用处。

    我建议在创建图表之前保存 json:

    $(function () {
        var options = {
    
            chart: {
                marginRight: 120
            },
    
            legend: {
                align: 'right',
                verticalAlign: 'top',
                x: 0,
                y: 100
            },
    
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
    
            series: [{
                data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
            }]
        };
        jsonstring = JSON.stringify(options);
    
        $('#container').highcharts(JSON.parse(jsonstring));  // just to prove it works, obviously you would use the options variable here
    
    });
    

    http://jsfiddle.net/6y2gg6oy/

    但是,如果这不是一个选项,您可以使用JSON.stringifyreplacer 选项来排除您不想要的选项对象部分。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter

    【讨论】:

    • 感谢您的精彩回答,我想这就是我现在正在做的事情。好吧,就我而言,它有点复杂,因为我试图构建一个动态图表来更新和改变配置。但是我已经定义了一些更新方法来更新我自己的配置,然后将其转换为 JSON,再次存储和加载。
    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多