Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fusioncharts - Update FusionCharts data without changing chart settings

Is there a way to set Fusionchart graph "data" property only. Because currently when you set the the data at a latter stage, you need to pass the full json object which has both "data" and "chart" properties.

Below is my sample code;

FusionCharts.ready(function () {
    var ageGroupChart = new FusionCharts({
        id: 'total-users',
        type: 'pie2d',
        renderAt: 'chart-container',
        width: '450',
        height: '300',
        dataFormat: 'json',
            "chart": {
                "caption": "Sample Graph",
                "enableSmartLabels": "0",
                "showPercentValues": "1",
                "showTooltip": "0",
                "decimals": "1",
                "theme": "ocean"
            },
            "data":null
        });
    ageGroupChart.setJSONUrl( "http://www.example.com/GetData.php" );
    ageGroupChart.render(); 
});

As you can see I'm setting the data from a online source. I'd prefer if the online source does not need to send the "chart" property along with the data. So I can separate the UI look and feel from data-source.

Any Ideas?

like image 382
melaka Avatar asked Aug 26 '14 09:08

melaka


1 Answers

I'm using the following function throught XLMhttpRequest calls:

function updateChart(data) {
    var jsonData = {
        "chart": {
            // Some rendering options
            "caption": caption,
            "subcaption": ""
        },
        "data": data
    };

    // First time I initialize my chart
    if (FusionCharts("myChartId") === undefined) {
        var chart = new myChartId({
            // Some rendering options
            swfUrl: "Charts/MSLine.swf",
            id: "myChartId",
            width: "100%",
            height: "280px",
            dataFormat: 'json'
        });
        chart.setJSONData(jsonData);
        chart.render("myContainerId");
    } else
        // Then I just update
        FusionCharts("myChartId").setJSONData(jsonData);
}

I call the updateChart function into my success callback:

success: function(data, request) {
    try {
        var d = JSON.parse(data);

        updateChart(d.chart);

        // Other job...
        var event = new CustomEvent("dataReceivedFromAjax", {"detail": d});
        document.dispatchEvent(event);

    } catch (e) {
        window.console.warn("Wrong format JSON data received");
        window.console.warn(data);
    }
}

Of course you may adapt my code to your case (for instance I'm using JsonData rather than JsonUrl).

like image 73
Fractaliste Avatar answered Oct 12 '22 20:10

Fractaliste