// raw data
var wines=null;
// datatable to use in chart
var wineData;
// Load the Visualization API and the basic chart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(prepareWineData);
// Creat, populates and show a data table,
function drawChart() {
if(!wines)
return;
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Terningkast');
data.addColumn('number', 'Antall');
data.addRows(wineData);
// Chart options
var options = {
'title':'Vintyper. '+wines.length+' viner',
'width':400,
'height':250,
pieStartAngle: 90,
is3D: true,
slices: {
0: { color: 'red' },
1: { color: 'gray' },
2: { color: 'blue' },
3: { color: 'orange' }
}
};
// Generate chart
var chartPie = new google.visualization.PieChart(document.getElementById('chart'));
// when chart has finnished loading we also want to get and display a fixed png-image
google.visualization.events.addListener(chartPie, 'ready',
function () {
document.getElementById('image').innerHTML = '<img src="' + chartPie.getImageURI() + '">';
}
);
// load and draw it
chartPie.draw(data, options);
}
// ajax, jQuery, to get hold of data
function loadWines(){
// while local testingwines=allwines;return;
// ajax call to load viner.json and assign value to wines
$.ajax({
url:'https://borres.hiof.no/wep/data/vin/viner.json',
async:false,
success:function(data)
{
wines=data.list;
},
error:function(data)
{
wines=null;
document.getElementById("loaderror").innerHTML="Fikk ikke tak i data";
}
});
}
// count types and prepare columns, finally: draw it
function prepareWineData(){
loadWines();
if(wines!=null){
//while testing: console.log(wines.length);
var sparkling=0;
var red=0;
var white=0;
var rose=0;
for(var ix=0;ix < wines.length;ix++){
var W=wines[ix];
if(W.type=="sparkling") sparkling+=1;
else if(W.type=="red") red+=1;
else if(W.type=="white") white+=1;
else if(W.type=="rose") rose+=1;
}
wineData=[["Rød",red],["Hvit",white],["Musserende",sparkling],["Rosé",rose]];
// draw it
drawChart();
}
}