Problem/Motivation

The Webform Statistics module reports submission counts correctly in numeric form (e.g. 7,000 or 2,000), but the D3.js chart renders truncated values instead (e.g. 7 or 2).
This is caused by the comma (,) being used as a thousands separator in the formatted number string.
When this string is passed to D3, it is parsed as a floating-point number, and the comma is interpreted as
a decimal separator (or causes early parsing termination), resulting in only the integer part before the
comma being rendered on the chart.

Webform statistics error

Steps to reproduce

  1. Install and enable the Webform Statistics module.
  2. Create a webform and collect more than 999 submissions (or seed the database with dummy submissions).
  3. Navigate to the webform statistics page that renders the D3 chart.
  4. Observe that the Y-axis or data values in the chart show 2 or 7 instead of 2,000 or 7,000.
  5. Compare with the numeric summary displayed outside the chart, which shows the correct value.

Proposed resolution

Before passing submission count values to D3, ensure they are cast to a plain integer or float
without locale-specific formatting (i.e. strip thousands separators or avoid formatting the number
before it reaches the JavaScript layer). The server-side code or Twig template should pass raw
numeric values to the chart data, and formatting for display should be handled separately by D3
or a JavaScript number formatter after rendering.

Remaining tasks

  • Identify the exact template or PHP function that formats the submission count before passing it to D3.
  • Apply the fix and verify the chart renders correct values for counts above 999.
  • Add a test case covering submission counts with values that would include a thousands separator.

User interface changes

The D3 chart will now display correct submission counts (e.g. 7,000 instead of 7).
No visual layout changes are expected.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

eduardo morales alberti’s picture

The problem comes from webform_statistics/src/Plugin/views/style/D3Chart.php, method getRenderedFieldValue, it returns the number already rendered.

  protected function getRenderedFieldValue(int $row_index, string $field_id, $row): string {
    if (!isset($this->view->field[$field_id])) {
      return '';
    }

    // Use the pre-rendered field value from renderFields().
    if (isset($this->rendered_fields[$row_index][$field_id])) {
      $value = $this->rendered_fields[$row_index][$field_id];
      // Strip HTML tags and trim.
      $value = strip_tags((string) $value);
      return trim($value);
    }

    return '';
  }

The right approach is to call $this->view->field[$field_id]->getValue($row) to bypass Drupal's number formatting, rather than reading from $this->rendered_fields

eduardo morales alberti’s picture

Status: Active » Needs review
StatusFileSize
new75.04 KB

Ready to review, tested on our tests environments

Webform statistics fix

juanjol’s picture

Assigned: Unassigned » juanjol
Status: Needs review » Fixed

Looks good to me, thank you Eduardo, Merging for next release

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.