// raw jsondata as fetched by AJAX
var wines;
// datatables to use in chart
var typeTable;
var diceTable;
// AJAX, jQuery, to get hold of data
function loadWines(){
// when testing with local jsonfile:wines=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";
}
});
}
// prepare a table with distribution of dices [1..6]
function makeDiceTable(){
// count wines of each type
// assume raw data wines are available
var dices=[0,0,0,0,0,0];
for(var ix=0;ix < wines.length;ix++){
var d=parseInt(wines[ix].dice);
dices[d-1]=dices[d-1]+1;
}
var wineData=[];
for(var ix=0;ix <6; ix++)
wineData=wineData.concat([[""+(ix+1),dices[ix]]]);
// Create the data table.
var table = new google.visualization.DataTable();
table.addColumn('string', 'Terningkast');
table.addColumn('number', 'Antall');
table.addRows(wineData);
return table;
}
// prepare a table with distribution of winetype
function makeTypeTable(){
// count wines of each type
// assume raw data wines are available
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;
}
var wineData=[["Rød",red],["Hvit",white],["Musserende",sparkling],["Rosé",rose]];
// Create the data table.
table = new google.visualization.DataTable();
table.addColumn('string', 'Type');
table.addColumn('number', 'Antall');
table.addRows(wineData,"Vintyper");
return table;
}
// load raw data and prepare tables
function prepareWineTables(){
loadWines();
typeTable=makeTypeTable();
diceTable=makeDiceTable();
// initial show
showTypes();
}
// according to user actions
// show vintyper
function showTypes(){drawChart(typeTable,'Vintyper, '+wines.length+' viner');}
// show dices
function showDices(){drawChart(diceTable,'Terningkast, '+wines.length+' viner');}