question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Line Chart Not Accepting Dynamically Generated Arrays

See original GitHub issue

I am writing a WordPress plugin that will be using Chart.js on an admin page. I am using jQuery to pull data from a database via AJAX and then turning this data into an array. When I JSON decode it and turn it into an array in javascript Chart.js does not accept the values(the chart doesn’t display, or only shows one of the labels).

Here is my code:

           if($("#transactions_per_month_chart").length){
                var ctx = $("#transactions_per_month_chart").get(0).getContext("2d");
                //This will get the first returned node in the jQuery collection.
                var myNewChart = new Chart(ctx);

                // AJAX request to get the number of transactions for this month
                var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
                var lineChartDates = [];
                var lineChartTransactions = [];
                var data = {
                        action: 'populate_admin_line_chart_dates'
                };
                $.post(ajaxurl, data, function(response) {
                    $.each(JSON.parse(response), function(i, obj){
                        lineChartDates[i] = String(obj);
                    });
                });
                //alert(lineChartDates);

                var dataTwo = {
                        action: 'populate_admin_line_chart_transactions'
                };
                $.post(ajaxurl, dataTwo, function(response) {
                    $.each(JSON.parse(response), function(i, obj){
                        lineChartTransactions[i] = parseInt(obj);
                    });
                });

                // Create the line chart object
                var dataThree = {
                    labels :lineChartDates,
                    datasets : [
                        {
                            fillColor : "rgba(220,220,220,0.5)",
                            strokeColor : "rgba(220,220,220,1)",
                            pointColor : "rgba(220,220,220,1)",
                            pointStrokeColor : "#fff",
                            data : lineChartTransactions
                        }
                    ]
                }
                var adminLineChart = new Chart(ctx).Line(dataThree);
}

Am I missing something or does Chart.js just not accept dynamically generated arrays?

If you are familiar with WordPress this code exists in the callback function in the following hook:

add_action( 'admin_footer', 'er_qm_admin_chart_js' )

Any help will be greatly appreciated!

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
SamClarke2012commented, Aug 28, 2013

It sounds like your getting caught out by async loading

try

$.ajaxSetup({
async: false
});

0reactions
ssureshs21commented, Oct 16, 2014

I got a solution by removing the “[]” from the datasetValue. Below is my new code

for (var j = 0; j < count; j++) { datasetValue[j] = { fillColor: ‘rgba(220,220,220,0.5)’, strokeColor :‘rgba(220,220,220,1)’ , title :‘2013’, data : [Math.round(Math.random() * 100),Math.round(Math.random() * 100)-10] } }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chartjs: dynamic data (array) not working - Stack Overflow
In my recent work, I'm implementing ChartJS charts. But I'm having trouble popularizing the graph with the data I get from the database....
Read more >
Chart with Dynamic Array - Microsoft Office Forums
I've been tasked with creating a template for a series of line charts that will be based on dynamic arrays. I watched some...
Read more >
Dynamic Chart in Excel - How to Create? (Step by Step)
1. u003cbr/u003eu003cimg alt=u0022Dynamic-Chart-Example-1-4u0022 src=u0022https://www.wallstreetmojo.com/wp-content/uploads/2021/04/Dynamic-Chart-Example-1-4 ... 2. u003cbr/u003eu003cstrongu003eNote:u003c/strongu003e While creating a name range, there should not be any blank values. This is because, in...
Read more >
How to Create an Excel Interactive Chart with Dynamic Arrays
Quickly create an automatically sorted Excel bar chart that ALSO lets you hide and show categories on the chart based on a flag...
Read more >
Dynamically Changing Graph Data Based on Matrix Selections
When Delta is selected in the matrix, the pie chart shows rows with the Delta category. My work so far is available in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found