Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get data to load on my chart from an API. All the data is getting to the chart correctly, but the chart doesn't load and I get the unresponsive script error. I'm using Highcharts. Any suggestions? Thanks. My code is below.

Model

public function ajax_get_chart() {

    $quotes = $this->rest->get('api/1/BTCUSD/trades/fetch');
    $series_data = array();
    $results = $quotes->return;
    $i = 0;

    foreach ($results as $quote)
    {
        $series_tmp = array(
            'date'      =>  $quote->date,
            'price'     =>  $quote->price
        );
        $series_data[]= $series_tmp;
        $i= $i+1;
    }
    die (json_encode($series_data));
    return TRUE;
} 

Javascript

$(document).ready(function() {
    var chart;
    $.ajax({
        url: "/chart/ajax_get_chart", // the URL of the controller action method
        dataType: "json",
        success: function(result) 
        {
            chart = new Highcharts.Chart({
                chart: {
                    renderTo: 'container',
                    defaultSeriesType: 'bar'
                },
                title: {
                    text: 'Stacked bar chart'
                },
                xAxis: {
                    title: {
                        text: 'Price'
                    }
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: 'Date'
                    }
                },
                legend: {
                    backgroundColor: '#FFFFFF',
                    reversed: true
                },
                tooltip: {
                    formatter: function() {
                        return ''+
                            this.series.name +': '+ this.y +'';
                    }
                },
                plotOptions: {},
                series: result
            });
        }
    });
});
share|improve this question
Why are you using die() for a normal exit? – Barmar 18 hours ago
I got rid of the die() and I no longer get the error and now the chart shows up in the container but its just the white background of the chart, no data. – evann 18 hours ago
1  
Did you replace it with echo? Or maybe return, depending on how the function is being called? – Barmar 17 hours ago
How much data there is? Generally "unresponsive script" is caused because the JS code is taking too long to execute. This would lead me to believe the amount of data is so big it takes forever to render the chart. – Jani Hartikainen 16 hours ago
There is A LOT of data, its bitcoin prices updating live every second – evann 16 hours ago

2 Answers

Sounds like the problem is that there's too much data to present.

You can try using a speedier browser (Chrome usually works pretty fast), limiting the data, or trying another charting library.

Limiting the data is probably the most likely one to work. If you need to show all the data, the best way to go about it would be to only load partial data and then if the user for example scrolls the chart, load the missing data.

Another way to present more data at the same time would be to calculate averages for the data on server. For example, if the ticker data is from every second, you could pre-calculate hourly or even daily averages on the server. This generally allows you to show a relatively accurate chart without causing performance problems, and many libraries also support dynamically loading more accurate data if you zoom the chart.

share|improve this answer
Thanks. I think I would want to go with loading partial data then more loads when the user scrolls the chart. However, I'm not exactly sure on how to do that. – evann 15 hours ago

Highcharts can not handle so huge number of data, but using Highstock with dataGrouping or lazy-loading you should be able to handle a lot of points, see demo.

Also this article should help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.