Home >Backend Development >PHP Tutorial >PHP and Vue.js Development Guide: How to Present Statistical Charts in Web Pages
PHP and Vue.js Development Guide: How to Present Statistical Charts in Web Pages
Introduction:
In web development, it is very common to present statistical charts need. PHP and Vue.js are two very popular technologies that can be combined to achieve dynamic and interactive statistical chart display. This article will introduce how to use PHP and Vue.js to develop statistical chart functions and provide relevant code examples.
For example, create a data table named "stats" containing the fields "id", "date" and "value":
CREATE TABLE stats(
id INT AUTO_INCREMENT PRIMARY KEY, date DATE, value INT
);
// Database connection settings
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";
// Create database connection
$conn = new mysqli($servername , $username, $password, $dbname);
// Check database connection
if ($conn->connect_error) {
die("连接失败:" . $conn->connect_error);
}
/ / Query statistics
$sql = "SELECT * FROM stats";
$result = $conn->query($sql);
// Convert query results to JSON format
$data = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) { $data[] = $row; }
}
// Output data in JSON format
echo json_encode($data);
//Close the database connection
$conn->close();
?>
In this code, we first set up the database connection parameters and then create a database connection. Next, we execute a query to get the statistics and convert the query results into JSON format. Finally, we output the data in JSON format and close the database connection.
<title>统计图表</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<div id="app"> <canvas id="chart"></canvas> </div> <script> new Vue({ el: '#app', mounted: function() { // 在页面加载完成后请求后端数据 axios.get('chart.php') .then(function(response) { // 处理返回的数据 var data = response.data; // 处理数据并呈现图表 var labels = []; var values = []; for (var i = 0; i < data.length; i++) { labels.push(data[i].date); values.push(data[i].value); } var ctx = document.getElementById('chart').getContext('2d'); var chart = new Chart(ctx, { type: 'bar', data: { labels: labels, datasets: [{ label: '统计图表', data: values, }] }, options: {} }); }) .catch(function(error) { console.log(error); }); } }); </script>
In this code, we first introduce Vue .js and Axios scripts. Then, in the mounted hook of the Vue instance, we use the axios library to request the backend data. After successfully obtaining the data, we process the data and use the Chart.js library to present statistical charts.
Conclusion:
By combining PHP and Vue.js, we can easily implement the function of displaying statistical charts on web pages. PHP is responsible for processing back-end data requests and database operations, and Vue.js is responsible for front-end data display and interaction. Through the Chart.js library, we can easily create various types of statistical charts. I hope this article will help you understand and use PHP and Vue.js to develop statistical charts.
The above is the detailed content of PHP and Vue.js Development Guide: How to Present Statistical Charts in Web Pages. For more information, please follow other related articles on the PHP Chinese website!