Charts.js rendering issue on page load

Mobile Technologies Mobile Computing 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I have a strange issue occurring with my Charts.js line chart. Upon page load, nothing seems to be visible in the element: However, once you resize the browser window, the chart displays as intended: Has anyone else encountered this issue and know of a way to fix it? The JavaScript for my chart is below. Please let me know if I need to alter it in any way to fix this issue. function drawLineChart() { var dates = []; var clientCosts = []; $.ajax({ url: '../inc/wip-data.php', dataType: 'json' }).done(function(data) { data.forEach(function(item) { dates.push(item.date); clientCosts.push(item.clientCostsTotal); }); }); console.log(dates, clientCosts); var chartData = { labels: dates, datasets: [{ fillColor: "rgba(151,187,205,0.2)", strokeColor: "rgba(151,187,205,1)", pointColor: "rgba(151,187,205,1)", pointHighlightStroke: "rgba(151,187,205,1)", data: clientCosts }] }; // Get the context of the canvas element we want to select var ctx = document.getElementById("myChart").getContext("2d"); // Instantiate a new chart var myChart = new Chart(ctx, { responsive: true, type: 'line', data: chartData, options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } }); } drawLineChart(); This issue is not replicating in JS Bin. The only difference between the JS Bin and my local build is the AJAX call for the data, so that leads me to believe the AJAX call is what is causing the issue. Any guidance on what may be calling this issue would be appreciated.

Posted on 16 Aug 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ The data used by the chart is not available when the chart is created. This is because the ajax call is asynch, and the data becomes available only when the ajax request is complete. In order to have the chart visible right away, you will need to use this call myChart.update(), in the "done" function of the ajax call after you populate the data, in order to update the chart: ... data.forEach(function(item) { dates.push(item.date); clientCosts.push(item.clientCostsTotal); }); myChart.update();

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Mobile Technologies Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community