Problem/Motivation

The three dashboard charts stay blank after installation. Chart.js is loaded through
the Drupal library system, which places it in the page footer, but the chart-building
code runs from an inline <script> tag earlier in the page.
The browser reaches the inline script before Chart.js is available and throws
ReferenceError: Chart is not defined, so no chart is drawn.
The chart data is also printed directly into that inline script with
json_encode|raw, which trips the project's ESLint job.

Steps to reproduce

  1. Install ai_metering, configure a provider, and sync pricing.
  2. Make a few AI calls so usage rows exist.
  3. Open /admin/reports/ai-metering.
  4. The three charts (token split, cost by provider, cost by model) are blank. The browser console shows ReferenceError: Chart is not defined.

Environment

  • Drupal version: 11.3.11
  • ai_metering version: 1.0.0-alpha1
  • AI provider module + version: any (e.g. ai_provider_anthropic 1.x)
  • AI feature module tested: N/A

Proposed resolution

Move chart rendering, table sorting, and the month filter into a dedicated JavaScript
file (js/ai-metering-dashboard.js) attached through the
ai_metering_dashboard library with core/drupalSettings,
core/once, and the Chart.js library as dependencies.
Pass chart data through drupalSettings instead of a raw inline script.
This fixes the load order, eliminates the inline script, and clears the ESLint findings.

Remaining tasks

  • Move the inline script to js/ai-metering-dashboard.js as a Drupal behavior.
  • Pass chart data via drupalSettings from the controller.
  • Declare library dependencies (drupalSettings, once, chartjs).
  • Verify the charts render with no console errors.
  • Review.

User interface changes

Yes — the three dashboard charts now render correctly.

API changes

N/A

Data model changes

N/A

AI assistance

N/A

Comments

codeitwisely created an issue. See original summary.

codeitwisely’s picture

Assigned: Unassigned » codeitwisely

  • 1b6b95e4 committed on 1.0.x-3604226-move-dashboard-js-to-library
    Issue #3604226 by codeitwisely: Move dashboard JS to a library file to...

  • codeitwisely committed c0699596 on 1.0.x
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604227-add-log-base-url-to-hook-theme
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604256-pricing-source-plugin
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604238-test-litellm-rate-path
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604229-skip-wrapper-provider-double-count
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604228-uid-filter-in-usage-log
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604255-token-details-column
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604240-toolbar-icon
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...

  • codeitwisely committed c0699596 on 1.0.x-3604346-by-role-dashboard
    Issue #3604226: Move dashboard JS to a library to fix chart rendering...
codeitwisely’s picture

Status: Active » Fixed

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.