Witam, chciałbym stworzyć za pomocą Google Charts wykres zmian ceny Oleju Napędowego. Niestety coś mi nie wychodzi, JSON output nie widzi wierszy pobieranych z bazy MySql. Moja strona:
http://www.dawny-inowroclaw.info/orlen/wykres3.phpZawartość pliku wykres3.php
<?php
$con=mysql_connect("localhost","admin","password") or
die("Failed to connect with database!!!!");
array('label' => 'data', 'type' => 'date'), array('label' => 'cenaon', 'type' => 'number') ),
);
while($row = mysqli_fetch_assoc($sth))
{
// date assumes "yyyy-MM-dd" format
$dataArr = explode('-', $row['data']); $year = (int) $dataArr[0];
$month = (int) $dataArr[1] - 1; // subtract 1 to make month compatible with javascript months
$day = (int) $dataArr[2];
array('v' => "data($year, $month, $day)"), array('v' => $row['cenaon']) ));
}
$json = json_encode($results, JSON_PRETTY_PRINT);
<!--Load the Ajax API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?php echo $json ?>);
var options = {
title: 'Cena paliw',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
<!--this is the div that will hold the pie chart-->
Polecenie echo $json; daje:
Kod
{ "cols": [ { "label": "data", "type": "date" }, { "label": "cenaon", "type": "number" } ], "rows": [] }
Tabela Bazy danych wygląda tak:
Kod
Host: localhost
Baza danych: latino_orlen
Czas wygenerowania: 27 Lut 2016, 09:58
Wygenerowany przez: phpMyAdmin 4.0.10.7 / MySQL 5.6.23
Zapytanie SQL: SELECT * FROM `ceny` LIMIT 0, 30;
Rekordy: 4
id data cenaon
19 2016-02-24 2.939
20 2016-02-25 2,99
21 2016-02-26 2.95
22 2016-02-27 2.964
Struktura tabeli:
Kod
Kolumna Typ Null
id int(11) Nie
data date Nie
cenaon varchar(100) Nie
Co robię źle? Pomoże ktoś? Z góry dziękuję
Ten post edytował latino 27.02.2016, 13:05:59