


How to implement dynamically updated horizontal statistical charts in PHP and Vue.js
How to implement dynamically updated horizontal statistical charts in PHP and Vue.js
Foreword:
Statistical charts are one of the important components of data visualization. In web development, PHP is used as the back-end language to handle data storage and calculation, while Vue.js is used as the front-end framework to present data and page interaction. This article will introduce how to combine PHP and Vue.js to implement dynamically updated horizontal statistical charts.
1. Preparation
Before we start, we need to install and configure the following environment:
- Server environment: Build a server that can run PHP code, such as Apache and Nginx wait.
- Database: Use MySQL or other relational database.
2. Back-end development
- Create database table
First, we need to create a database table to store statistical data. For example, we create a table named "statistics ” table contains two fields: id and value.
CREATE TABLE statistics ( id INT AUTO_INCREMENT PRIMARY KEY, value INT );
- Backend interface
In PHP, we can provide it to the front end by writing a backend interface. Create a file named "api.php" and write the following code:
<?php // 设置响应头 header('Content-Type: application/json'); // 连接数据库 $db = new PDO('mysql:host=localhost;dbname=your_database;charset=utf8', 'your_username', 'your_password'); // 查询数据 $stmt = $db->query('SELECT * FROM statistics'); $statistics = $stmt->fetchAll(PDO::FETCH_ASSOC); // 返回数据 echo json_encode($statistics);
The above code connects to the database through PDO, queries the statistical data, and then returns the query results to the front end in JSON format.
3. Front-end development
- Page structure
To use Vue.js on the front-end to render pages and process data, we need to create an HTML file and introduce Vue.js CDN link. The specific code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>动态更新的水平统计图表</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <canvas id="chart"></canvas> </div> <script src="app.js"></script> </body> </html>
- JavaScript code
Create a file named "app.js" in the same directory and write the following code:
new Vue({ el: '#app', data: { chartData: [] }, mounted() { this.getChartData(); }, methods: { getChartData() { fetch('api.php') .then(response => response.json()) .then(data => { this.chartData = data.map(item => item.value); this.renderChart(); }) .catch(error => console.error(error)); }, renderChart() { var ctx = document.getElementById('chart').getContext('2d'); new Chart(ctx, { type: 'horizontalBar', data: { labels: ['1月', '2月', '3月', '4月', '5月', '6月'], datasets: [{ label: '销售统计', data: this.chartData, backgroundColor: 'rgba(0,123,255,0.5)' }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } }); } } });
The above code uses Vue.js to create a Vue instance, and calls the getChartData method in the mounted hook function, sends a GET request through fetch to obtain the data returned by the backend interface, then assigns the data to chartData, and calls the renderChart method to render statistics. chart.
4. Test run
Open the HTML file in the browser, and you can see the dynamically updated horizontal statistical chart. If there is new statistical data that needs to be added, the data can be added by calling the backend interface, and then the frontend will automatically obtain the latest data and update the chart.
Conclusion:
This article introduces how to implement dynamically updated horizontal statistical charts in PHP and Vue.js. Obtain statistical data from the database through the back-end interface, and use Vue.js to present the data on the front-end and implement dynamic updates of charts. This implementation method can be applied to many actual data visualization scenarios to improve user experience and data display effects.
The above is the detailed content of How to implement dynamically updated horizontal statistical charts in PHP and Vue.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
