HAHS.
Back to Catalog

Toggle View

interaction

Also known as: view switch, chart type switch, mode toggle, layout switch

Enable explorationShow distributionShow relationship NumericalCategoricalTemporal

Description

Toggle view is an interaction that lets users switch the entire chart type or layout while keeping the underlying data the same. A button bar might offer “Bar,” “Line,” and “Table” options; clicking one transforms the visualization into that representation. The same dataset that appeared as a grouped bar chart now appears as a stacked bar, a line chart, or a heatmap, each emphasizing different aspects of the data.

This pattern acknowledges that no single chart type is optimal for all analytical tasks. A bar chart excels at comparing magnitudes across categories, a line chart reveals temporal trends, a stacked area chart shows part-to-whole composition over time, and a table provides exact values. By offering multiple representations behind a simple toggle, the designer lets the user choose the lens best suited to their current question without requiring separate dashboards or pages for each view.

The toggle can range from switching between closely related chart types (grouped bar ↔ stacked bar ↔ normalized stacked bar) to fundamentally different representations (map ↔ table ↔ chart). Animated transitions between views are critical: when bars morph into lines or circles rearrange into a grid, the animation helps the user understand that they are seeing the same data in a new form. Without animation, the toggle feels like navigating to a different page rather than re-viewing the same information.

Toggle View — try it yourself

When to Use

  • When the data naturally supports multiple chart types that each reveal different insights (e.g., temporal data can be bars, lines, or areas).
  • When different audiences prefer different representations — analysts might want a scatterplot, executives might want a bar chart.
  • When the analytical task involves comparing different structural properties of the data (magnitude comparison vs. trend vs. distribution).
  • In exploratory dashboards where the user is not sure which chart type will best answer their question.
  • When screen space is limited and showing multiple charts side-by-side is not feasible.

When NOT to Use

  • When the alternative chart types would be misleading or nonsensical for the data (e.g., toggling a categorical bar chart to a line chart implies false continuity).
  • When the data clearly has one optimal representation and alternatives add no analytical value.
  • When the transition between chart types would be so disorienting that users lose track of the data.
  • When the user needs to compare the two views simultaneously — linked side-by-side views are better than toggle.
  • In narrative/explanatory contexts where the author should control the representation.

How It Works

  1. A control element (button group, segmented control, tabs, or radio buttons) displays the available chart types.
  2. The user clicks a chart type option (e.g., “Line” instead of the current “Bar”).
  3. The system computes the new layout — new scales, axes, mark types, and positions for each data item.
  4. An animated transition morphs the current marks into the new form: bars may shrink to points and connect into lines, or rectangles may rearrange into a treemap grid.
  5. Axes, legends, and labels update to match the new chart type’s conventions.
  6. The user can toggle back at any time, with the reverse animation playing.

Variations

  • Chart-type toggle: Switch between fundamentally different chart types (bar ↔ line ↔ area ↔ table).
  • Layout toggle: Same chart type but different arrangements (grouped bar ↔ stacked bar ↔ normalized stacked bar).
  • Map/chart toggle: Switch between geographic (choropleth) and statistical (bar chart) representations of the same regional data.
  • Chart/table toggle: Switch between a visual chart and a data table view, useful for accessibility and data export.
  • Orientation toggle: Switch between horizontal and vertical bar charts, or between row and column layouts.
  • 3D/2D toggle: Switch between a flat 2D representation and a 3D perspective view.

Code Reference

// Toggle between bar chart and line chart
const views = ["bar", "line"];
let currentView = "bar";

d3.selectAll(".view-btn").on("click", function () {
  currentView = this.dataset.view;
  d3.selectAll(".view-btn").classed("active", false);
  d3.select(this).classed("active", true);

  if (currentView === "bar") {
    svg.selectAll(".line-path").transition().duration(500).style("opacity", 0);
    svg.selectAll(".bar")
      .transition().duration(500)
      .attr("y", d => y(d.value))
      .attr("height", d => height - y(d.value))
      .style("opacity", 1);
  } else {
    svg.selectAll(".bar").transition().duration(500).style("opacity", 0);
    svg.selectAll(".line-path")
      .transition().duration(500)
      .style("opacity", 1)
      .attr("d", line);
  }
});