javascript - Chartjs dynamic chart return both x & y axis undefined
Get the solution ↓↓↓I am new to chartjs. After spend some time checking tutorials I came up codes as following and it's giving meundefined
result bothx
andy
axis. I can manage to get static data working i.e. if put down month ony-axis
and charts display fine. Any help is greatly appreciated. By the way I am using chartjsversion 2.1.4.
data.php:
<?php
//setting header to json
header('Content-Type: application/json');
include_once 'includes/db_connect.php';
$query = sprintf("SELECT FiscalPeriod, SUM(DollarsSold) AS Sold
FROM ar_customersalespersonhistory
WHERE FiscalYear = '2017'
GROUP BY FiscalPeriod
ORDER BY FiscalPeriod
");
//execute query
$result = $connection->query($query);
//loop through the returned data
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
//free memory associated with result
$result->close();
//close connection
$connection->close();
//now print the data
print json_encode($data);
?>
Output from data.php:
[{
"FiscalPeriod": "01",
"Sold": "357508.03"
}, {
"FiscalPeriod": "02",
"Sold": "393790.14"
}, {
"FiscalPeriod": "03",
"Sold": "407346.07"
}, {
"FiscalPeriod": "04",
"Sold": "557704.12"
}, {
"FiscalPeriod": "05",
"Sold": "555916.68"
}, {
"FiscalPeriod": "06",
"Sold": "422659.26"
}, {
"FiscalPeriod": "07",
"Sold": "297766.49"
}, {
"FiscalPeriod": "08",
"Sold": "448256.07"
}, {
"FiscalPeriod": "09",
"Sold": "510020.86"
}, {
"FiscalPeriod": "10",
"Sold": "325525.30"
}, {
"FiscalPeriod": "11",
"Sold": "89214.27"
}]
And javascript:
$(document).ready(function() {
$.ajax({
url: '/registration/sales_general.php',
method: 'GET',
// dataType: 'json',
success: function(data) {
// console.log(data);
var month = [];
var sold = [];
for (var i in data) {
month.push(data[i].FiscalPeriod);
sold.push(data[i].Sold);
}
console.log(month);
console.log(sold);
var chartdata = {
// labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
labels: month,
datasets: [{
label: '2017',
backgroundColor: '#26B99A',
// data: [33,24,25,19,46,99,21]
data: sold
}]
};
var ctx = $('#mybarChart1');
ctx.height = 70;
var barGraph = new Chart(ctx, {
type: 'bar',
data: chartdata
});
},
error: function(data) {
console.log(data);
}
});
});
Answer
Solution:
Yourchartdata
object doesn't contain valid ChartJS options
This should work:
var data = [{"FiscalPeriod":"01","Sold":"357508.03"},{"FiscalPeriod":"02","Sold":"393790.14"},{"FiscalPeriod":"03","Sold":"407346.07"},{"FiscalPeriod":"04","Sold":"557704.12"},{"FiscalPeriod":"05","Sold":"555916.68"},{"FiscalPeriod":"06","Sold":"422659.26"},{"FiscalPeriod":"07","Sold":"297766.49"},{"FiscalPeriod":"08","Sold":"448256.07"},{"FiscalPeriod":"09","Sold":"510020.86"},{"FiscalPeriod":"10","Sold":"325525.30"},{"FiscalPeriod":"11","Sold":"89214.27"}];
var month = [];
var sold = [];
for (var i in data) {
month.push(data[i].FiscalPeriod);
sold.push(data[i].Sold);
}
var chartdata = {
type: 'line',
data: {
labels: month,
datasets: [
{
label: '2017',
data: sold,
backgroundColor: '#26B99A',
}
]
},
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, chartdata);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
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/
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
HTML
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 programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
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.