// the wines we want
var selectionXPathF=
'//wine[country="Frankrike" and type="red" and volume = "75"]';
var selectionXPathI=
'//wine[country="Italia" and type="red" and volume = "75"]';
// datatable when loaded and prepared
var dataTable=null;
function drawChart(element) {
if(!dataTable){
document.getElementById(element).innerHTML="no data loaded";
return;
}
var data = new google.visualization.DataTable();
data.addColumn('string', 'Terningkast');
data.addColumn('number', 'snitt');
data.addRows(dataTable);
// Chart options
var options =
{'title':'Gjennomsnittspris pr flaske fordelt på terningkast',
'width':500,'height':300
};
var chartBar =
new google.visualization.BarChart(document.getElementById(element));
chartBar.draw(data, options);
}
function getAndShowWinePrices(path, element){
$.ajax({
url:'viner.xml',
// want load to finnish before drawing
async:false,
success:function(data,status,xhr)
{
var XMLDom=xhr.responseXML;
var nodes=XMLDom.evaluate(path, XMLDom, null,
XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
var sum = [0,0,0,0,0,0];
var count= [0,0,0,0,0,0];
while(result){
var dice=parseInt(
result.getElementsByTagName("dice")[0].
firstChild.nodeValue);
count[dice-1]+=1;
var price=parseFloat(
result.getElementsByTagName("price")[0].
firstChild.nodeValue);
sum[dice-1]+=price;
result=nodes.iterateNext();
}
// prepare the datatable for the chart
dataTable=[];
for(var ix=0; ix < 6; ix++){
var p=sum[ix]/count[ix];
dataTable.push([""+(ix+1),p]);
//dataTable.push([ix+1,p.toFixed(0)]);
}
//draw the chart
drawChart(element);
},
error:function(data)
{
document.getElementById(Element).innerHTML=
"error when loading";
}
});
}
// 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(drawChart);