PHP and Vue.js Development Guide: How to Present Statistical Charts in Web Pages

王林
Release: 2023-08-19 06:28:02
Original
939 people have browsed it

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.

  1. Preparation
    Before starting development, make sure you have installed the development environment for PHP and Vue.js. You can download and install the latest versions of PHP and Vue.js through the official website. In addition, you also need a server that supports a database, such as MySQL.
  2. Database Settings
    First, we need to set up a database to store statistical data. Use phpMyAdmin or another MySQL client to create a new database, and then create a data table with fields for statistical data.

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
Copy after login

);

  1. Back-end development
    We use PHP to handle back-end data requests and database operations. Create a file named "chart.php" and add the following code:

// 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);
Copy after login

}

/ / 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;
}
Copy after login

}

// 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.

  1. Front-end development
    Use Vue.js to handle front-end data display and interaction. Create a file called "chart.html" and add the following code:



<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>
Copy after login


<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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!