Skip to main content
100%

d3.js learning

✓ Published0🌍 Public
TTJPepping
Last edited Apr 3, 2018
Created on Mar 29, 2018

A stacked column chart displays four annual cost categories—Energy, Business Air Travel, Employee Commuting, and Other—from 2012 to 2016. The d3.v3 stack layout computes layer offsets, while ordinal and linear scales map years and cumulative values to SVG rects. Axes, a legend, and a hidden SVG tooltip group appear on mouseover, with tooltip text updated via d3.mouse coordinates.

AI-generated description

A simple stacked coloumn graph built using d3's stack layout.

The SVG tooltip is based on this gist by Mike Bostock. In short, the tooltip's group, comprised of a rect and a text element, are created when the chart is first drawn and set to be hidden. Action is applied to the stacks' rects: on mouseover the group become visible; on mousemove the mouse's XY position is captured and used to transform/translate the group.

If you ajust the tooltip's XY offset, keep in mind that there can be no overlap between the group and the mouse's position or the group's visibility will remain hidden.

Similar vizzes