I have table biggest_user in mysql like this:
and i want to create grouped-bar using chart.js.
I already create code like this:
<html>
<head>
<script src="Chart.min.js"></script>
</head>
<body>
<center>
<?php
include 'conn.php';
$sql = "Select date, user, sum(total) as total from user_biggest group by date,user";
# $sql = "SELECT difference, date FROM `biggest_user`";
$date = $conn->query($sql);
$user = $conn->query($sql);
$total = $conn->query($sql);
?>
<canvas id="bar-chart-grouped" width="1000" height="250"></canvas>
<script>
new Chart(document.getElementById("bar-chart-grouped"), {
type: 'bar',
data: {
labels: [<?php while($a = mysqli_fetch_array($date)) { echo '"' . $a['date'] . '",'; } ?>],
datasets: [
{
label: "Total",
backgroundColor: "#3e95cd",
data: [<?php while($b = mysqli_fetch_array($total)) { echo $b['total'] . ', '; } ?>]
},
{
label: "User",
backgroundColor: "#8e5ea2",
data: [<?php while($c = mysqli_fetch_array($user)) { echo $b['user'] . ', '; } ?>]
}
]
},
options: {
title: {
display: true,
text: 'Biggest User (Megabytes)'
}
}
});
</script>
but the result chart is like this:
I think it's not grouped-bar chart. but i don't know what's wrong in my code.
Unfortunately I don't know much aboutphp
. Therefore I can only partially answer your question.
data.labels
should contain an entry for each day. What you further need is onedataset
for each user and that's it.
Please take a look at below code snippet and see how it could work.
new Chart("chart", {
type: 'bar',
data: {
labels: ["01-Sep-20", "02-Sep-20", "03-Sep-20"],
datasets: [{
label: "cs",
backgroundColor: "red",
data: [229, 200, 198]
},
{
label: "finance",
backgroundColor: "green",
data: [162, 150, 178]
},
{
label: "credit",
backgroundColor: "blue",
data: [89, 156, 90]
},
{
label: "it",
backgroundColor: "orange",
data: [89, 55, 112]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chart" height="90"></canvas>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.