Current Location: Home> Latest Articles> How to Create Grouped and Categorized Statistical Charts with PHP and Vue.js

How to Create Grouped and Categorized Statistical Charts with PHP and Vue.js

M66 2025-06-21

How to Create Grouped and Categorized Statistical Charts with PHP and Vue.js

Introduction:
Statistical charts are an important tool for data visualization, helping us understand data more intuitively. In this article, we will explain how to use PHP and Vue.js to implement grouped and categorized statistical charts. We will guide you through the process with code examples for better understanding.

Step 1: Prepare Data

First, we need to prepare some data for the chart. In this example, we will use sales data, and the structure of the data is as follows:

$data = [
    ['group' => 'A', 'category' => 'cat1', 'amount' => 100],
    ['group' => 'A', 'category' => 'cat1', 'amount' => 150],
    ['group' => 'A', 'category' => 'cat2', 'amount' => 200],
    ['group' => 'B', 'category' => 'cat2', 'amount' => 300],
    ['group' => 'B', 'category' => 'cat1', 'amount' => 180],
    ['group' => 'C', 'category' => 'cat1', 'amount' => 120],
    ['group' => 'C', 'category' => 'cat2', 'amount' => 250]
];

Step 2: Back-End Data Processing

Next, we need to process the data in PHP for easier display on the front-end. Here is an example of PHP code to process the data:

$groupedData = [];

foreach ($data as $row) {
    $group = $row['group'];
    $category = $row['category'];
    $amount = $row['amount'];

    if (!isset($groupedData[$group])) {
        $groupedData[$group] = [];
    }
    if (!isset($groupedData[$group][$category])) {
        $groupedData[$group][$category] = 0;
    }

    $groupedData[$group][$category] += $amount;
}

echo json_encode($groupedData);

Step 3: Front-End Display

Now that we have the processed data, we will use Vue.js to display it on the front-end. Here are examples of HTML and Vue.js code:

HTML Code Example:

<div id="app">
    <div v-for="(groupData, group) in groupedData" :key="group">
        <h3>{{ group }}</h3>
        <table>
            <tr v-for="(amount, category) in groupData" :key="category">
                <td>{{ category }}</td>
                <td>{{ amount }}</td>
            </tr>
        </table>
    </div>
</div>

Vue.js Code Example:

new Vue({
    el: '#app',
    data: {
        groupedData: []
    },
    mounted() {
        axios.get('/api/data.php')
            .then(response => {
                this.groupedData = response.data;
            })
            .catch(error => {
                console.log(error);
            });
    }
});

With the code above, we have implemented the functionality of grouping and categorizing data based on "group" and "category". Using Vue.js's v-for directive, we loop through the groups and categories, displaying the data dynamically in the front-end.

Conclusion

In this article, we have explained how to use PHP and Vue.js to create grouped and categorized statistical charts. We started with data processing in PHP and then displayed it on the front-end using Vue.js. We hope this article helps you understand how to create effective statistical charts based on grouped and categorized data.