Data can be extracted using
CSV files. The CSV stands for
Comma separated values which a simple format used to store the data of a table, the CVS files can be used in Google Spreadsheets, Microsoft Office Excel, Open Office Calc. CVS like do not contain any rows, columns or styling like other table files they only contain single sheet in a file.
Example
In order to extract the data from a CVS file, create a data file in which the data is separated by commas as shown below and then save the file with .csv Extension.
[c]
Categories,Apples,Bananas,Oranges,Mangos
David,3,3,3,2
John,4,4,8,6
[/c]
Now create a JavaScript file for writing function to extract the CVS file data as shown below.
[c]
$.get('data.csv', function(data) {
// Split the lines
var lines = data.split('\n');
// Iterate over the lines and add categories or series
$.each(lines, function(lineNo, line) {
var items = line.split(',');
// header line containes categories
if (lineNo == 0) {
$.each(items, function(itemNo, item) {
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);
}
});
// Create the chart
var chart = new Highcharts.Chart(options);
});
[/c]
Output
By running the above code in the preferred browser user can get the output as shown in the image below.