Oversikt over vintyper
Datagrunnlaget er JSON beskrivelsen av vinene: viner.json.
I dette tilfellet lar vi brukeren kontrollere hvilke kolonner han/hun vi ha med i framstillingen.
Javaskriptet som preparerer data er slik:
_prepareData2.js
// raw data var wines; // data to use in chart var typeTable; // selected dicedistribution var diceTable; // complete, backup var diceTableFull; // dices counted pr type var RED=[0,0,0,0,0,0]; var WHITE=[0,0,0,0,0,0]; var SPARK=[0,0,0,0,0,0]; var ROSE=[0,0,0,0,0,0]; var ALL=[0,0,0,0,0,0]; // ajax, jQuery, to get hold of data function loadWines(){ // while testing locally:wines=allwines; return; $.ajax({ url:'https://borres.hiof.no/wep/data/vin/viner.json', async:false, success:function(data) { wines=data.list; }, error:function(data) { wines=null; } }); } function makeDiceTable(){ for(var ix=0;ix < wines.length;ix++){ var W=wines[ix]; var d=parseInt(W.dice); if((d<1)||(d>6)){console.log("DataError dice=",d);} if(W.type=="sparkling") SPARK[d-1]=SPARK[d-1]+1; else if(W.type=="red") RED[d-1]=RED[d-1]+1; else if(W.type=="white") WHITE[d-1]=WHITE[d-1]+1; else if(W.type=="rose") ROSE[d-1]=ROSE[d-1]+1; else console.log("DataError type=",W.type); ALL[d-1]=ALL[d-1]+1; } // Create the data table. var table = new google.visualization.DataTable(); table.addColumn('string', 'Terningkast'); table.addColumn('number', 'Alle'); table.addColumn('number', 'Røde'); table.addColumn('number', 'Hvite'); table.addColumn('number', 'Musserende'); table.addColumn('number', 'Rose'); table.addRow(["1",ALL[0],RED[0],WHITE[0],SPARK[0],ROSE[0]]) table.addRow(["2",ALL[1],RED[1],WHITE[1],SPARK[1],ROSE[1]]) table.addRow(["3",ALL[2],RED[2],WHITE[2],SPARK[2],ROSE[2]]) table.addRow(["4",ALL[3],RED[3],WHITE[3],SPARK[3],ROSE[3]]) table.addRow(["5",ALL[4],RED[4],WHITE[4],SPARK[4],ROSE[4]]) table.addRow(["6",ALL[5],RED[5],WHITE[5],SPARK[5],ROSE[5]]) return table; } // load raw data and prepare tables function prepareWineTables(){ loadWines(); diceTable=makeDiceTable(); // keep a complete backup diceTableFull=diceTable.clone(); show("2,3,4,5"); } // show dices function show(list){ // restore before we start removing diceTable=diceTableFull.clone(); for(var ix=5; ix > 0;ix--){ var index=list.indexOf(ix); if(index==-1) diceTable.removeColumn(ix); } drawChart(diceTable,'Terningkast'); }
og framvisningskoden er slik:
_showData2.js
// 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(prepareWineTables); // Callback that creates and populates a data table, function drawChart(datatable,T) { // Chart options var options = { 'title':T, 'width':400, 'height':400, is3D: true }; // Instantiate chart and draw it var chartBar = new google.visualization.BarChart(document.getElementById('barchart')); chartBar.draw(datatable, options); }
Interaktivt
LOADING...
Koden som lar oss velge er slik:
_radiobuttons
<fieldset style="width:400px"><form> <input type="radio" name="vis" onchange="show(this.value)" value="[2,3,4,5]" id="alle" />Alle<br/> <input type="radio" name="vis" onchange="show(this.value)" value="[1]"/>Samlet<br/> <input type="radio" name="vis" onchange="show(this.value)" value="[2]"/>Røde<br/> <input type="radio" name="vis" onchange="show(this.value)" value="[3]"/>Hvite<br/> <input type="radio" name="vis" onchange="show(this.value)" value="[4]"/>Musserende<br/> <input type="radio" name="vis" onchange="show(this.value)" value="[5]"/>Rosé<br/> </form></fieldset> <script>document.getElementById("alle").checked = true;</script>