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
- Install ai_metering, configure a provider, and sync pricing.
- Make a few AI calls so usage rows exist.
- Open
/admin/reports/ai-metering. - 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.jsas a Drupal behavior. - Pass chart data via
drupalSettingsfrom 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
Comment #2
codeitwisely commentedComment #13
codeitwisely commented