estou a seguir este exemplo
http://www.webcodefree.com.br/blog/?s=Google+Chart+API
problema leio os dados em json campo valor, acontece que o grafico gerado é todo da mesma cor..
aqui esta o codigo php usado
Código :
javascript
Código :
http://www.webcodefree.com.br/blog/?s=Google+Chart+API
problema leio os dados em json campo valor, acontece que o grafico gerado é todo da mesma cor..
aqui esta o codigo php usado
Código :
//Inicializando as variáveis
$table = array();
$rows = array();
//Criando as colunas dentro do array
$table['cols'] = array(
array('id' => '','label' => 'ID', 'type' => 'string'),
array('id' => '','label' => 'valor', 'type' => 'number'),
);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$temp = array();
$temp[] = array('v' => (string) $row['ID']);
$temp[] = array('v' => (float) $row['valor']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
//"json_encode" é uma função do próprio php que irá transformar o array em JSON
$jsonTable = json_encode($table);
echo $jsonTable;
$table = array();
$rows = array();
//Criando as colunas dentro do array
$table['cols'] = array(
array('id' => '','label' => 'ID', 'type' => 'string'),
array('id' => '','label' => 'valor', 'type' => 'number'),
);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$temp = array();
$temp[] = array('v' => (string) $row['ID']);
$temp[] = array('v' => (float) $row['valor']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
//"json_encode" é uma função do próprio php que irá transformar o array em JSON
$jsonTable = json_encode($table);
echo $jsonTable;
javascript
Código :
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script src="../backend/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
// Carregue o API Visualization e o pacote piechart.
google.load('visualization', '1', {'packages': ['corechart']});
// Defina um callback a ser executado quando o API de visualização do Google é carregado.
google.setonloadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getdados.php",
dataType: "json",
async: false
}).responseText;
alert(jsonData);
// Coloque aqui as configurações do gráfico, acesse a documentação para mais opções
var options = {'title': 'valores',
'width': 1000,
'height': 680,
color: 'red',
visibleInLegend: true,
is3D:true,
};
// Create our data table out of JSON data loaded from server.
var wrapper = new google.visualization.ChartWrapper({
chartType: 'ColumnChart',
dataTable: jsonData,
options:options,
containerId: 'chart_div'
});
wrapper.draw();
var formatter = new google.visualization.NumberFormat(
{prefix: '€ ', negativeColor: 'red', negativeParens: false});
formatter.format(wrapper, 1);
//Desenha o gráfico
}
</script>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script src="../backend/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
// Carregue o API Visualization e o pacote piechart.
google.load('visualization', '1', {'packages': ['corechart']});
// Defina um callback a ser executado quando o API de visualização do Google é carregado.
google.setonloadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getdados.php",
dataType: "json",
async: false
}).responseText;
alert(jsonData);
// Coloque aqui as configurações do gráfico, acesse a documentação para mais opções
var options = {'title': 'valores',
'width': 1000,
'height': 680,
color: 'red',
visibleInLegend: true,
is3D:true,
};
// Create our data table out of JSON data loaded from server.
var wrapper = new google.visualization.ChartWrapper({
chartType: 'ColumnChart',
dataTable: jsonData,
options:options,
containerId: 'chart_div'
});
wrapper.draw();
var formatter = new google.visualization.NumberFormat(
{prefix: '€ ', negativeColor: 'red', negativeParens: false});
formatter.format(wrapper, 1);
//Desenha o gráfico
}
</script>
<div id="chart_div" style="width: 900px; height: 500px;"></div>