CryoFusion87
CryoFusion87

Reputation: 796

Google Visualization API - save and retrieve ChartWrapper object

Looking to save the ChartWrapper object after a chart has been modified in the Google Visualization Chart Editor so that the page reloads the last chart they selected is shown rather the default set in the options.

I have managed to save a JSON.stringfy representation of the ChartWrapper object using the code below:

function getWrapper() {
            chart = chartEditor.getChartWrapper();
            localStorage.setItem('chartWrapper', JSON.stringify(chart));
            redrawChart();
    }

The code for the default chart is provided below:

chart = new google.visualization.ChartWrapper({
        chartType: 'LineChart',
        containerId: 'chart_div',
        dataTable: data,
        options: {
            title: '@Html.Raw(Model.KeyIndicatorName)',
            hAxis: {
                slantedText: true,
                slantedTextAngle: 30,
                title: 'Reporting Period'
            },
            vAxis: {
                title: '@Model.XAxis'
            },
            chartArea: {
                top: 40,
                width: "65%"
            },
            pointSize: 5
        }
    });

on reload if the localStorage item is available I want default options to be superseded with the those values.

How I go about implementing this?

Upvotes: 1

Views: 41

Answers (1)

WhiteHat
WhiteHat

Reputation: 61212

need to use chart wrapper method toJSON method, not JSON.stringify...

function getWrapper() {
    chart = chartEditor.getChartWrapper();
    localStorage.setItem('chartWrapper', chart.toJSON());
    redrawChart();
}

then you can create the chart directly from the json...

var chart = new google.visualization.ChartWrapper(localStorage.getItem('chartWrapper'));

Upvotes: 1

Related Questions