CSV import utility

Introduction

Since October 2013 there's been a CSV reader object that is part of RGraph. This object helps you fetch and read the contents of CSV files that are held on your server (though by using CORS, cross-origin HTTP requests can be made[icon]). It can also make it easier for you to get information from your database. You can have a server-based script that fetches data from your database and outputs it as CSV data. Then use this CSV reader to read it and get the data from it.

Alternatively, you might have a CSV file that's generated by a server-based application so you can then read that file using this CSV reader.

Note that the CSV importer can be used standalone - so you don't need to include the RGraph core library and it's easier for you to use it with your own applications or code.

In October 2021 this CSV objects API was expanded slightly to bring it into line with the HTMLTable connector.

An example

<script>
    //
    // This fetches the CSV file and shows the Bar chart
    //
    var csv = new RGraph.CSV('/sample.csv', function (csv)
    {
        // Get the first column which becomes the labels
        var labels = csv.column(0),
        
            // Get the second column which becomes the data
            data = csv.column(1),
            
            // Get the number of rows in the CSV
            numrows = csv.numrows,
            
            // Get the number of columns in the CSV
            numcols = csv.numcols;
        

        // Create the chart
        var bar = new RGraph.Bar({
            id:'cvs',
            data: data,
            options: {
                backgroundGridVlines: false,
                backgroundGridBorder: false,
                title: 'A chart using the CSV reader',
                colors: ['#000'],
                xaxis: false,
                yaxis: false,
                marginTop: 35,
                shadow: false,
                colorsStroke: 'rgba(0,0,0,0)',
                textSize: 14,
                labelsAbove: true,
                labelsAboveSpecific: labels,
                labelsAboveSize: 14,
                labelsAboveOffset: -35,
                labelsAboveColor: 'white'
            }
        }).wave();
    });
</script>

API documentation

Constructor(URL, callback[, separator[, end-of-line]])
You use the constructor to create and fetch the CSV file. It takes two arguments:

row = csv.row(index[, start[, length]])
This function fetches a row of data from the CSV and returns it (as an array). Optionally you can give the column index to start at (the column numbering starts from 0). eg row = csv.row(3, 1) You can also optionally give a length which stipulates how many elements are returned to you.

Arguments

column = csv.column(index[, start[, length]])
This function fetches a column of data from the CSV and returns it (as an array). Optionally you can give the row index to start at (the row numbering starts from 0). eg col = csv.column(3, 1)You can also optionally give a length that stipulates how many elements are returned to you.

Arguments

Trailing end-of-lines

Note that trailing EOLs (eg \r\n) are stripped automatically.

Database integration

By using the CSV file reader you can make integrating with a database easier. You can have a server-side script that retrieves data from the database and outputs it like this (or like this).

You can then request that CSV file using the CSV file reader and that allows you to get the data into RGraph.

Reading a <div> tag instead of a file

Instead of reading a CSV file from your server, you can also embed the CSV data into a hidden div in your page.

If your server has high response times you may prefer this in place of an extra AJAX round-trip to your server to fetch the data.

The code is very similar, though instead of a filename you would use the div tags ID - like this:


<div id="myDiv" style="display: none">
    Richard,8,4,7,6,5,3,4
    Dave,7,4,6,9,5,8,7
    Luis,4,3,5,2,6,5,4
    Kevin,4,2,8,9,6,7,3
    Joel,4,5,1,3,5,8,6
    Pete,4,5,6,3,5,8,6
</div>

<script>
    //
    // This fetches the CSV data from the <div> tag above and shows the Bar chart
    //
    var csv = new RGraph.CSV('id:myDiv', function (csv)
    {
        // Create the chart here
    }
</script>

If you use the window.onload event to create your chart(s) then that means that you can put the div tag that contains the CSV data anywhere you like on the page - including above your CSV data. The DOMContentLoaded event would also work equally well in this case.


Demo pages

There are lots of demos of the CSV reader in the download archive They have the words csv-reader or at least csv in the filename.