SfSkills Domains Docs GitHub

Home / lwc / lwc-chart-and-visualization

lwc-chart-and-visualization

Charts and visualization in LWC: Chart.js, D3, Plotly via Static Resources; Lightning chart components; performance patterns for 10k+ data points; accessibility; SLDS theming. NOT for CRM Analytics embedding — use admin/crm-analytics-app-creation. NOT for Tableau embedding — use lwc/tableau-embedding-in-lightning.

Open the skill folder on GitHub


LWC Chart and Visualization

Activate when building or reviewing a data visualization inside a Lightning Web Component: dashboards, record-page charts, drill-through widgets. LWC has no native chart primitive; the decision between Chart.js, D3, Plotly, and Salesforce's own charting components comes down to volume, interactivity, and theming.

Before Starting

Core Concepts

Static Resource + Chart library

The canonical pattern: bundle Chart.js / D3 / Plotly as a Static Resource, load via loadScript, render into a canvas or SVG element inside the LWC template.

Lightning chart components

Salesforce ships limited charting primitives (e.g., Lightning charts in Experience Cloud, Report charts). Minimal customization; easy wins for standard use.

Canvas vs SVG

Canvas (Chart.js, Plotly) scales to more data points; SVG (D3) is inspectable and accessible but slower past a few thousand elements.

Data flow

Apex returns data → LWC passes to chart library → chart renders. For large datasets: preaggregate in Apex, paginate on demand, or stream via Platform Events for live dashboards.

Accessibility patterns

Provide a hidden data table under the chart for screen readers; aria-describedby on the chart; ensure color choices meet contrast and are not the sole differentiator.

Common Patterns

Pattern: Chart.js with Static Resource + loadScript

loadScript(this, chartJsStatic) in connectedCallback, create Chart instance in renderedCallback once. Destroy on disconnectedCallback. Data binding via chart.data.datasets[0].data = newData; chart.update().

Pattern: D3 for bespoke visualization

Static Resource bundles D3. LWC renders an empty <svg>; in renderedCallback D3 selects it and draws. Use this.template.querySelector to get the SVG inside shadow DOM.

Pattern: Preaggregated Apex + Chart.js for large datasets

Apex returns bucketed data (e.g., daily aggregates instead of row-level). Chart renders 365 points, not 300,000. SOQL aggregate + GROUP BY is your friend.

Pattern: Streaming dashboard via Platform Events

Platform Event subscription in LWC pushes new datapoint → chart updates without reload. Works for live KPI displays.

Decision Guidance

SituationLibraryReason
Standard bar/line/pie, <1k pointsChart.jsEasy, SLDS-friendly
Bespoke custom vizD3Flexibility
Scientific / statistical plotsPlotlyRich defaults
Reports-style chart on Experience CloudLightning chart componentNative
10k+ live pointsPreaggregate + Chart.js canvasPerformance
Full interactivity (pan, zoom, brush)D3 or PlotlyFeature coverage
  1. Quantify data volume, interactivity, accessibility, theming requirements.
  2. Pick the library per decision guidance.
  3. Bundle library as Static Resource; add licensing attribution if required.
  4. Build a prototype LWC with the simplest chart; verify SLDS theme compatibility.
  5. Measure render time with realistic data; optimize data pipeline first, rendering second.
  6. Add accessibility layer: hidden data table, aria attributes, keyboard navigation.
  7. Write jest tests mocking the library and asserting data transformation.

Review Checklist

Salesforce-Specific Gotchas

  1. renderedCallback fires on every render, not once. Chart libraries that are re-instantiated every render produce memory leaks and flicker — guard with a this._chart reference.
  2. Shadow DOM isolates CSS. Chart.js tooltips and legends respect shadow DOM in Chart.js 3+; earlier versions had quirks.
  3. Static Resource cache. Browser caches aggressively; when upgrading the library, version the Static Resource name or bust the cache explicitly.

Output Artifacts

ArtifactDescription
Library decision recordWhy Chart.js / D3 / Plotly
Static Resource bundleLibrary + licensing attribution
Chart LWC templateJS / HTML / CSS with accessibility layer
Performance measurementRender time vs data volume

Package files