0

I use Highcharts and when I update the site it doesn't update the data from which it constructs the graph.

I take the data from mysql, save it in a .csv file and open the .csv in highcharts. It seems to save the old values in the graph even if the .csv file is updated.

 <?php

unlink('column-data.csv');  //This file must exist or else it gives error

include 'database.php';

$result = mysql_query("SELECT 
    value
FROM
    data
INTO OUTFILE 'C:/xampp/htdocs/arduinoproj/test3/column-data.csv'    
FIELDS ENCLOSED BY '\,' 
TERMINATED BY ';' 
ESCAPED BY '\"' 
")
  or die("Could not issue MySQL query");

include 'highcharts.php'

?>

..and the highcharts code is more or less unchanged.

The changed code from the original is in bold. Highcharts code:

<!DOCTYPE html>
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>My first column chart by Highcharts</title>
        <!-- 1. Add JQuery and Highcharts in the head of your page -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script src="http://code.highcharts.com/highcharts.js"></script>

        <!-- 2. You can add print and export feature by adding this line -->
        <script src="http://code.highcharts.com/modules/exporting.js"></script>


        <!-- 3. Add the JavaScript with the Highchart options to initialize the chart -->
        <script type="text/javascript">
        jQuery(document).ready(function() { 

            var options = {
                chart: {
                    renderTo: 'container',
                    **type: 'line'**
                },
                title: {
                    text: 'Tiny Tool Monthly Sales'                 
                },
                subtitle: {
                    text: '2014 Q1-Q2'
                },
                xAxis: {
                    categories: []
                },
                yAxis: {
                    title: {
                        text: 'Sales'
                    }
                },
                series: []
            };
            // JQuery function to process the csv data
            $.get(**'column-data.csv'**, function(data) {
                // Split the lines
                var lines = data.split('\n');
                $.each(lines, function(lineNo, line) {
                    var items = line.split(',');

                    // header line contains names of categories
                    if (lineNo == 0) {
                        $.each(items, function(itemNo, item) {
                            //skip first item of first line
                            if (itemNo > 0) options.xAxis.categories.push(item);
                        });
                    }

                    // the rest of the lines contain data with their name in the first position
                    else {
                        var series = { 
                            data: []
                        };
                        $.each(items, function(itemNo, item) {
                            if (itemNo == 0) {
                                series.name = item;
                            } else {
                                series.data.push(parseFloat(item));
                            }
                        });

                        options.series.push(series);

                    }

                });
                //putting all together and create the chart
                var chart = new Highcharts.Chart(options);
            });         

        });
        </script>

    </head>
    <body>

        <!-- 3. Add the container -->
        <div id="container" style="width: 600px; height: 400px; margin: 0 auto"></div>      

    </body>
</html>
Lennart
  • 217
  • 1
  • 3
  • 11
  • How do you load csv data into the Highcharts? Maybe your data is cached by browser (try `ctrl+R` to refresh). – Paweł Fus Feb 09 '16 at 12:42
  • I haven't changed the highcharts code really, and it does work but the update lags behind. – Lennart Feb 09 '16 at 13:07
  • 1
    I am sorry, but you need to show us your Highcharts code. How do we suppose to know how do you load data? There is many ways to do that :) Anyway, I am pretty sure this is a cache problem, try this: `$.get('column-data.csv', {ts: +new Date()}, function(data) { ... });` - so you will pass on current timestamp to avoid caching. Or read answers [here](http://stackoverflow.com/questions/8841425/how-to-set-cache-false-in-jquery-get-call). – Paweł Fus Feb 09 '16 at 13:10
  • Yes of course I edited the post without telling you, sorry about that. Now it works perfectly, many thanks! – Lennart Feb 09 '16 at 13:20
  • Oh, one mor thing though, last value doesn't seem to be added.. any ideas? – Lennart Feb 09 '16 at 13:28
  • Nope, unless you add sample CSV you are using ;) Anyway, chart is always missing last value? Or just only after the update of the file? And does your file contain that missing value? – Paweł Fus Feb 09 '16 at 13:36
  • Have you applied solution with cache: `$.get('column-data.csv', {ts: +new Date()}, function(data) { ... });` ? – Paweł Fus Feb 17 '16 at 11:49

0 Answers0