如何从会话存储中将数据放入Highchart中?

时间:2017-01-13 00:13:24

标签: javascript html session highcharts session-variables

我试图从会话存储变量中加载饼图高清图中的数据,但我没有得到任何结果。

我想要这个:

$(function () {
Highcharts.chart('container', {
    chart: {
        type: 'pie',
        options3d: {
            enabled: true,
            alpha: 45
        }
    },
    title: {
        text: 'Contents of Highsoft\'s weekly fruit delivery'
    },
    subtitle: {
        text: '3D donut in Highcharts'
    },
    plotOptions: {
        pie: {
            innerSize: 100,
            depth: 45
        }
    },
    series: [{
        name: 'Delivered amount',
        data: [
            ['Bananas', 8],
            ['Kiwi', 3],
            ['Mixed nuts', 1],
            ['Oranges', 6],
            ['Apples', 8],
            ['Pears', 4],
            ['Clementines', 4],
            ['Reddish (bag)', 1],
            ['Grapes (bunch)', 1]
        ]
    }]
});

});

我将信息存储在会话变量中,如下所示:

sessionStorage.data = "['Bananas', 9], ['Kiwi', 9]";

然后,我将此变量放在数据字段中:

$(function () {
Highcharts.chart('container', {
    chart: {
        type: 'pie',
        options3d: {
            enabled: true,
            alpha: 45
        }
    },
    title: {
        text: 'Contents of Highsoft\'s weekly fruit delivery'
    },
    subtitle: {
        text: '3D donut in Highcharts'
    },
    plotOptions: {
        pie: {
            innerSize: 100,
            depth: 45
        }
    },
    series: [{
        name: 'Delivered amount',
        data: sessionStorage.data
    }]
});

});

我做错了什么?

非常感谢!

2 个答案:

答案 0 :(得分:0)

更改

series : [[{name: 'Delivered amount',data: sessionStorage.data}]

series = [[{name: 'Delivered amount',data: sessionStorage.data}]

答案 1 :(得分:0)

您将会话数据存储为字符串,然后尝试将Highcharts数据设置为该字符串 - 您应该将其反序列化为javascript数组。

如果您更改sessionStorage.data的格式,请执行以下操作:

'[["Bananas", 9], ["Kiwi", 9]]'

您可以使用JSON.parse对其进行反序列化:

series: [{
    name: 'Delivered amount',
    data: JSON.parse(sessionStorage.data)
}]