TypeError: array_unshift(): Argument #1 ($array) must be of type array

Created on 7 August 2024, 8 months ago

Problem/Motivation

I am using Drupal 10 and charts 5.1.0
I created a view, added two fields Title and score for data and set title as label and score as data.
If I set the chart type to pie it works. If I set to bar, line, or column, I get an error:

TypeError: array_unshift(): Argument #1 ($array) must be of type array, null given in array_unshift() (line 564 of /var/www/html/drupal/modules/contrib/charts/modules/charts_google/src/Plugin/chart/Library/Google.php).

With a trace

#0 /var/www/html/drupal/modules/contrib/charts/modules/charts_google/src/Plugin/chart/Library/Google.php(564): array_unshift()
#1 /var/www/html/drupal/modules/contrib/charts/modules/charts_google/src/Plugin/chart/Library/Google.php(158): Drupal\charts_google\Plugin\chart\Library\Google->chartsGooglePopulateChartData()
#2 /var/www/html/drupal/modules/contrib/charts/src/Element/Chart.php(210): Drupal\charts_google\Plugin\chart\Library\Google->preRender()
#3 [internal function]: Drupal\charts\Element\Chart->preRender()

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Active

Version

5.1

Component

Views integration

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Abontu
  • πŸ‡ΊπŸ‡ΈUnited States andileco

    I'm trying to recreate this. Can you change to a table display and share the output? I'm assuming there's missing data, but I want to see what it looks like.

  • OK,

    Your request for table brought me to the problem. When I set to table I saw there was no header row with column names. That is probably the array that was missing since I did not provide labels.

    To get it working,
    For the fields selected for title and data, I needed to edit and check "Create a label" and provide a label. Once I did that the charts rendered. I assume that pie chart does not use the labels.

    Now I just need to figure out how to create a US map with heatmap coloring. I don't see that in the GUI.

    Thanks for providing this module and responding to my issue!

  • πŸ‡ΊπŸ‡ΈUnited States andileco

    Thanks for solving where that's coming from; definitely something that needs fixing still.

    This will definitely need some custom coding if you're using the charts module.

    Do you need to use Google or could you use the Leaflet module or other mapping solutions in Drupal?

  • I will take a look at Leaflet module. Took a quick glance and it looks like it would work. I am not married to any specific charting. In fact I am trying a bunch.

  • Hi,

    I installed leaflet and have a map with points on each state. What I really want is a heat map or as leaflet calls it a Choropleth Map. I don'i see a way to do this with the leaflet module but maybe I am missing something.

    I want to crate something similar to this: https://leafletjs.com/examples/choropleth/example.html

    Thanks

  • After updating my charts to version 5.1.2, I encountered the same error. I have a content type that contains a chart type field, and I noticed that based on the type of chart I use as the default value (also using the Google charting library), I can't even open the page to create the content type.

    After a brief analysis debugging the code, I noticed that in the part of the code where the error points, there is an if, elseif, and else structure. If the type of chart configured as default is one of the types 'pie', 'donut', 'scatter', or 'bubble', the error does not occur because these types are covered in the if and elseif. However, if it is none of these types, the code goes straight to the else where it tries to unshift $data[0], which does not exist.

    Here is the part of the code that I mentioned:

    $data = $chart_definition['data'];
    
        if (in_array($element['#chart_type'], ['pie', 'donut', 'scatter'])) {
          // Populate the 0th row with the same number of values as the 1st row.
          $data[0] = array_fill(0, count($data[1]), $element[$key]['#title'] ?? '');
        }
        elseif ($element['#chart_type'] === 'bubble') {
          $data[0] = [
            $element[$key]['#title'] ?? '',
            $chart_definition['options']['hAxis']['title'] ?? '',
            $chart_definition['options']['vAxes'][0]['title'] ?? '',
            $chart_definition['options']['zAxis']['title'] ?? '',
          ];
        }
        else {
          array_unshift($data[0], '');
        }

    Then I found two possible solutions:

    1- Configure the default value to be one of the types mentioned above (I haven't covered all scenarios, but the error seems to not occur anymore).
    2- Modify the code inside the else by adding an if to check if $data[0] is not set, and if it is not, set it as an empty array.
    I decided to go with option 2; I'm not sure if it's the best one, as I still need to run a few more tests to make sure it won't generate any side effects.

  • πŸ‡ΊπŸ‡ΈUnited States andileco

    @gstivanin - I did some manual testing and didn't find any issues with your patch, but I also didn't experience this issue in the first place. It would help to be able to recreate the issue...can you provide specific instructions for how to do that?

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States bluegeek9

    bluegeek9 β†’ changed the visibility of the branch 5.1.x to hidden.

Production build 0.71.5 2024