ホームページ >データベース >mysql チュートリアル >PHP、MySQL、JSON を使用して Google チャートを作成するにはどうすればよいですか?
PHP MySQL Google Chart JSON - 完全な例
テクノロジーの状況が進化するにつれて、データを視覚化する機能がますます重要になってきます。データ視覚化のための強力なツールの 1 つが Google Chart です。これにより、開発者は円グラフ、棒グラフ、折れ線グラフなどのさまざまなグラフを作成できます。ただし、Google チャートを MySQL データ ソースと統合すると、特にプログラミング言語として PHP を使用する場合に課題が生じる可能性があります。
この記事では、PHP と MySQL を使用して Google チャートを生成するための包括的なソリューションを提供します。さまざまな PHP データ アクセス メソッドの使用を示す複数の例を取り上げます:
例 1: PHP-MySQL-JSON-Google Chart (Ajax 以外)
使用法:
コード:
$con = mysql_connect("localhost", "Username", "Password") or die("Failed to connect with database!!!!"); mysql_select_db("Database Name", $con); // The Chart table contains two fields: weekly_task and percentage // This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts $sth = mysql_query("SELECT * FROM chart"); /* --------------------------- example data: Table (Chart) -------------------------- weekly_task percentage Sleep 30 Watching Movie 40 work 44 */ //flag is not needed $flag = true; $table = array(); $table['cols'] = array( // Labels for your chart, these represent the column titles // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title array('label' => 'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); $rows = array(); while($r = mysql_fetch_assoc($sth)) { $temp = array(); // The following line will be used to slice the Pie chart $temp[] = array('v' => (string) $r['Weekly_task']); // Values of each slice $temp[] = array('v' => (int) $r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; $jsonTable = json_encode($table); //echo $jsonTable; ?> <html> <head> <!--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(<?=$jsonTable?>); var options = { title: 'My Weekly Plan', 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.PieChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> </head> <body> <!--this is the div that will hold the pie chart--> <div>
例 2: PHP-PDO-JSON-MySQL-Google Chart
この例では、PHP データ オブジェクト (PDO) を使用してデータベースに接続し、より高い柔軟性とsecurity.
コード:
/* ... (code) ... */ try { /* Establish the database connection */ $conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /* Select all the weekly tasks from the table googlechart */ $result = $conn->query('SELECT * FROM googlechart'); /* --------------------------- example data: Table (googlechart) -------------------------- weekly_task percentage Sleep 30 Watching Movie 10 job 40 Exercise 20 */ $rows = array(); $table = array(); $table['cols'] = array( // Labels for your chart, these represent the column titles. /* Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for Slice title */ array('label' => 'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); /* Extract the information from $result */ foreach($result as $r) { $temp = array(); // The following line will be used to slice the Pie chart $temp[] = array('v' => (string) $r['weekly_task']); // Values of each slice $temp[] = array('v' => (int) $r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; // convert data into JSON format $jsonTable = json_encode($table); //echo $jsonTable; } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } ?>
例 3: PHP-MySQLi-JSON-Google Chart
この例データベースには MySQL 拡張機能の改良版である MySQLi を利用しますインタラクション。
/* ... (code) ... */ /* Establish the database connection */ $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } /* Select all the weekly tasks from the table googlechart */ $result = $mysqli->query('SELECT * FROM googlechart'); /* --------------------------- example data: Table (googlechart) -------------------------- Weekly_Task percentage Sleep 30 Watching Movie 10 job 40 Exercise 20 */ $rows = array(); $table = array(); $table['cols'] = array( // Labels for your chart, these represent the column titles. /* Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for Slice title */ array('label' => 'Weekly Task', 'type' => 'string'), array('label' => 'Percentage', 'type' => 'number') ); /* Extract the information from $result */ foreach($result as $r) { $temp = array(); // The following line will be used to slice the Pie chart $temp[] = array('v' => (string) $r['weekly_task']); // Values of the each slice $temp[] = array('v' => (int) $r['percentage']); $rows[] = array('c' => $temp); } $table['rows'] = $rows; // convert data into JSON format $jsonTable = json_encode($table); //echo $jsonTable;
以上がPHP、MySQL、JSON を使用して Google チャートを作成するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。