I am trying to create a pie chart using an external csv file and have been unsuccessful so far. I copied a sample that creates a bar chart via an external csv, and that works fine. So i'm wondering if i'm formating the csv incorrectly. Here is the contents of piedata.csv file:
Code: Select all
Bridge Construction,24
Bridge Other,42
Highway Paving,67
And here is the code i'm using on my page:
Code: Select all
var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'pie'
},
title: {
text: 'Projects'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<strong>'+ this.point.name +'</strong>: '+ this.y +' %';
}
}
}
},
colors: [
'#4572A7',
'#AA4643',
'#DB843D'
],
series: [{
type: 'pie',
name: 'Projects',
data: []
}]
};
$.get('piedata.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);
});
Trish