XK8ER
XK8ER

Reputation: 780

Re-Render chart with a button

I would like to know how can I have a button that will re-render the chart when I click it! so that I dont have to refresh every 5 minutes

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highstock Example</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
$(function() {
    var seriesOptions = [],
        yAxisOptions = [],
        seriesCounter = 0,
        names = ['MSFT', 'AAPL', 'GOOG'],
        colors = Highcharts.getOptions().colors;

    $.each(names, function(i, name) {

        $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename='+ name.toLowerCase() +'-c.json&callback=?',   function(data) {

            seriesOptions[i] = {
                name: name,
                data: data
            };

            // As we're loading the data asynchronously, we don't know what order it will arrive. So
            // we keep a counter and create the chart when all the data is loaded.
            seriesCounter++;

            if (seriesCounter == names.length) {
                createChart();
            }
        });
    });



    // create the chart when all data is loaded
    function createChart() {

        $('#container').highcharts('StockChart', {
            chart: {
            },

            rangeSelector: {
                selected: 4
            },

            yAxis: {
                labels: {
                    formatter: function() {
                        return (this.value > 0 ? '+' : '') + this.value + '%';
                    }
                },
                plotLines: [{
                    value: 0,
                    width: 2,
                    color: 'silver'
                }]
            },

            plotOptions: {
                series: {
                    compare: 'percent'
                }
            },

            tooltip: {
                pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
                valueDecimals: 2
            },

            series: seriesOptions
        });
    }

});
        </script>
    </head>
    <body>
<script src="../../js/highstock.js"></script>
<script src="../../js/modules/exporting.js"></script>

<div id="container" style="height: 500px; min-width: 600px"></div>
    </body>
</html>

Upvotes: 0

Views: 357

Answers (1)

hendrikbeck
hendrikbeck

Reputation: 670

You could just take the first part of your Javascript code (everything above your function createChart(), basically) and wrap it in a named function like this:

function refreshData() {
  ...
}

Then you need to add a call to this function somewhere, otherwise it doesn't get executed when your page loads. Add the following call, e.g. at the beginning of your script:

refreshData();

Now you can add a button that simply calls this new function:

<button onclick="refreshData();">Refresh</button>

Upvotes: 2

Related Questions