ECharts Stacked Area Traffic Breakdown — Free Legend-Toggle Snippet

ECharts Stacked Area Traffic Breakdown · Charts · Plain HTML, CSS & JS · Live preview

What's included

Features

True legend-driven re-stacking
Hiding a series recomputes the stack total automatically.
Five-source composition view
Organic, Direct, Social, Referral, and Paid Ads at once.
Hover-isolated series
emphasis.focus dims everything but the traced source.
Borderless area fills
Zero line width keeps focus on the filled regions.
Axis tooltip
One hover shows every visible source's value together.
Continuous-axis layout
boundaryGap: false pins points to the plot edges.
Deterministic sample data
Seeded wave functions, same shapes on every load.
Responsive canvas
ResizeObserver keeps the chart sized to its container.

About this UI Snippet

ECharts Stacked Area Traffic Breakdown — a Legend That Actually Re-Stacks

Screenshot of the ECharts Stacked Area Traffic Breakdown snippet rendered live

A stacked area chart's whole value is comparing composition over time — but the moment you want to isolate just "Organic Search" and "Direct" by eye, five overlapping colors become hard to parse. ECharts' legend solves this for free: click any entry, and that series is removed from the stack, with every series above it sliding down to fill the gap.

stack: 'total' is the only thing making it a stack

Each of the five series is an ordinary line series with an areaStyle — what turns five separate area charts into one stacked one is giving every series the identical stack string. ECharts sums series sharing a stack key at each x position, in the order they're defined, and lays each one's area on top of the running sum from the ones before it.

Toggling the legend re-stacks automatically

This is the detail that's easy to assume needs custom code and doesn't: clicking a legend entry to hide a series is a built-in ECharts interaction, and because the stack total is recomputed from whichever series are currently visible, hiding "Paid Ads" doesn't leave a gap — the remaining four series' stack simply sums to a smaller total and every area redraws to match. No event listener, no manual recomputation.

lineStyle: { width: 0 } is intentional

Each series sets its line width to zero, so only the filled area renders and not a stroke along its top edge — with five stacked colors already fairly saturated, a visible border on each would add visual noise without adding information.

emphasis.focus: 'series' isolates on hover

Hovering any area (not just its legend entry) dims the other four to a lower opacity, so you can trace one source's shape through the 14-day window without the surrounding stack competing for attention — the same idea as the Sankey diagram's adjacency focus, applied to areas instead of graph edges.

Reusing it

Swap the five traffic sources for any composition-over-time data — revenue by product line, server load by region, expenses by category — and the legend-driven re-stacking, hover isolation, and area styling keep working unchanged since they're driven by the stack key, not by any source-specific logic.

Build with AI

Build, Understand, Optimize, and Extend It With AI

You don't have to implement re-stacking logic by hand. Paste this snippet's HTML, CSS, and JS into an AI coding assistant like Claude and ask it to explain exactly how the shared stack: 'total' key causes ECharts to recompute the running sum automatically when a legend item is toggled, and why lineStyle width is set to zero on every series. The same assistant can help optimize it — ask whether the seeded wave function used for sample data produces a realistic-enough distribution compared to real analytics data, and whether emphasis.focus: 'series' is the right choice versus focusing on 'self' only. It's also useful for extending the effect: ask it to add a percentage-of-total view toggle (switching from absolute stacked values to 100% stacked), a date-range picker instead of a fixed 14-day window, or a way to reorder which source stacks on top via drag-and-drop legend items. Treat the code less like a finished artifact and more like a starting point for a conversation.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a stacked area chart showing traffic composition by source over a recent date range using Apache ECharts (load echarts from a CDN, no other library), in plain HTML, CSS, and JavaScript.

Requirements:
- Model at least five distinct data sources (for example Organic Search, Direct, Social, Referral, Paid Ads), each with its own color and roughly two weeks of daily numeric values with some natural variation.
- Render all sources as stacked filled areas sharing a single stacking group, so that the visible top edge of the whole chart represents the sum of every visible source at each day, and hide the stroke/border line on each area so only the filled color regions are visible.
- Include a legend below the chart listing every source by name and color, using the charting library's built-in legend-toggle behavior so that clicking a legend entry hides that source from the stack and automatically recomputes the stacked total from the remaining visible sources — do not implement this recalculation manually.
- On hovering any one area, dim every other area to a lower opacity while keeping the hovered one and its legend entry at full opacity, so a single source's shape can be visually traced across the date range.
- Show a tooltip triggered by hovering anywhere along the x-axis that lists every currently visible source's exact value for that specific day.
- Set the x-axis so the first and last data points sit flush against the plot's edges rather than padded into centered categories, appropriate for a continuous date range.
- Keep the chart instance responsive to its container being resized by calling the chart's resize method whenever the container's size changes.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

Requires
<div class="esa-wrap">
  <div class="esa-card">
    <div class="esa-title">Traffic by Source — Last 14 Days</div>
    <div class="esa-sub">Click a legend item to toggle it — the stack re-totals automatically</div>
    <div class="esa-chart" id="esaChart"></div>
  </div>
</div>

Step by step

How to Use

  1. 1
    Add the ECharts CDNLoad echarts.min.js before the snippet's JS runs.
  2. 2
    Paste HTML, CSS, and JSFive traffic sources stack into one 14-day area chart.
  3. 3
    Click a legend itemThat source drops out and the stack re-totals.
  4. 4
    Click it againIt rejoins the stack in its original position.
  5. 5
    Hover an areaThe others dim so you can trace one source's shape.
  6. 6
    Hover the x-axisThe tooltip lists every visible source's value for that day.

Real-world uses

Common Use Cases

Marketing analytics dashboards
Compare acquisition channels over any date range.
Revenue composition reports
Product lines or regions stacked into one total.
Infrastructure load breakdowns
Requests by service or region over time.
Budget tracking
Expense categories stacked against a spending cap.
Executive dashboards
Pair with a revenue line chart for trend plus composition.
Learning ECharts
A clear reference for stacking and legend interaction.
Related: ECharts Radar Skill Comparison
See the ECharts Radar Skill Comparison for a related charts pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

All five series share the same stack: 'total' string, which tells ECharts to sum them at every x position in series order and layer each area on top of the running total. Clicking a legend entry to hide a series is a built-in ECharts interaction, and because the stack sum is recalculated from whichever series are currently visible, the remaining areas automatically redraw to a smaller total with no gap and no manual event handling needed.

Without it, each stacked area would render with a visible stroke along its top edge in addition to the fill — with five saturated colors already stacked closely together, five more colored strokes adds visual clutter without adding information. Setting width: 0 keeps only the filled area visible, which is cleaner for a composition chart like this one.

Hovering over any one series' area (not just clicking its legend swatch) dims every other series to a lower opacity while keeping the hovered one at full opacity, so you can visually trace how one specific source's contribution changes across the 14-day window without the rest of the stack competing for attention.

Add or remove entries in the SOURCES array (each needs a name, a color, a base value, and a wobble amount for the sample wave), and the series-building map, the stacking, the legend, and the color assignment all derive from that array automatically — no other code needs to change for a fourth or sixth source.

Build the series array from your data source (an API response, a store selector) inside your component before passing it to setOption, initialize the chart once against a container ref, and call setOption again whenever the underlying data changes. Dispose the instance on unmount and keep the ResizeObserver pattern since ECharts does not auto-resize with CSS layout changes alone.