Current Location: Home> Latest Articles> Array of frequency data required to generate a bar chart

Array of frequency data required to generate a bar chart

M66 2025-06-07

array_count_values() is a built-in PHP function that takes an array as an argument and returns a new array where the key is the only element that appears in the array and the value is the number of times the element appears in the original array. The syntax of the function is as follows:

 array_count_values(array $array): array

The return value of this function is an associative array, where the key of each element is the value of the original array, and the value is the number of times the value appears in the array.

2. Example: Use array_count_values() to generate frequency data

Suppose we have an array of user IDs that contain a website access log, and we want to count the number of visits per user and generate a histogram for it. Here is an example:

 <?php
// Assume this is the user access data we get from the website log
$userVisits = ['user1', 'user2', 'user3', 'user1', 'user4', 'user1', 'user2'];

// use array_count_values Function to get the number of visits per user
$visitCounts = array_count_values($userVisits);

// Print frequency data array
print_r($visitCounts);
?>

In the above code, the array_count_values() function will count the number of times each user appears in the $userVisits array. The output will look like:

 Array
(
    [user1] => 3
    [user2] => 2
    [user3] => 1
    [user4] => 1
)

3. Frequency data required to generate the bar chart

According to the example above, we have obtained access frequency data for each user. Next, we can format this data into a data format suitable for drawing a bar chart. Typically, the data format of a bar chart is an array where the key is a category (in this case the user) and the value is the frequency of that category (i.e. the number of visits).

For example, we can use the following code to convert the frequency data into the format required to draw the bar chart:

 <?php
// Suppose we have already obtained the frequency data array $visitCounts
$visitCounts = array_count_values($userVisits);

// The data format required to convert to a bar chart
$barChartData = [];
foreach ($visitCounts as $user => $count) {
    $barChartData[] = [
        'label' => $user,
        'value' => $count
    ];
}

// Print bar chart data
print_r($barChartData);
?>

The output will be an array containing key-value pairs for each user and their number of visits, suitable for generating a bar chart:

 Array
(
    [0] => Array
        (
            [label] => user1
            [value] => 3
        )
    [1] => Array
        (
            [label] => user2
            [value] => 2
        )
    [2] => Array
        (
            [label] => user3
            [value] => 1
        )
    [3] => Array
        (
            [label] => user4
            [value] => 1
        )
)

4. Draw a bar chart using the histogram library

Now we have got an array of frequency data suitable for drawing a bar chart. Next, we can use some popular JavaScript libraries such as Chart.js to generate bar charts. You can pass the above data into front-end JavaScript code and use these libraries to draw bar charts.

For example, using Chart.js can be implemented as follows:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bar chart example</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
    <canvas id="barChart"></canvas>
    <script>
        var ctx = document.getElementById('barChart').getContext('2d');
        var barChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ['user1', 'user2', 'user3', 'user4'],
                datasets: [{
                    label: 'Number of visits',
                    data: [3, 2, 1, 1],
                    backgroundColor: ['#FF5733', '#33FF57', '#3357FF', '#F0F33F'],
                    borderColor: ['#FF5733', '#33FF57', '#3357FF', '#F0F33F'],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
    </script>
</body>
</html>

This HTML file displays a bar chart where the tags and data are from the frequency array generated by PHP.