A monthly sales bar chart built with pure CSS and a few lines of vanilla JavaScript, no chart library needed. This column chart example has a labelled value axis with gridlines, a tooltip on every bar and a this year / last year toggle for sales dashboards, revenue reports and admin panels.
How it works
The root section.bc-chart holds a header with a segmented control (.bc-toggle, two native radio buttons), a row of summary figures (.bc-stats) and the chart itself. .bc-yaxis carries the value labels, .bc-grid the gridlines and ul.bc-bars the twelve bars in a CSS grid.
All numbers live in the DATA object at the top of script.js: labels, full month names and two series, current and previous. On DOMContentLoaded the script picks a "nice" axis maximum with niceStep() (both series share one scale, so switching years stays honest), draws the axis labels and gridlines, and builds one button.bc-bar per month. Each bar height is set through the --bc-h custom property as a percentage of the plot.
Hovering with a mouse, focusing with the keyboard or tapping a bar adds .is-active, which shows its .bc-tip tooltip with the exact value and the change against the other year. The highest month is active on load. Changing the radio button re-renders the heights, totals and best month, and .bc-fill animates to the new height. The class is-ready is added only after the first paint, so the chart is drawn at once and animates only on later changes.
Customization
DATA.seriesinscript.js: replacevalueswith your own figures; labels, totals, tooltips and the table update automatically.DATA.currency: any ISO code such asEURorGBP; all amounts are formatted withIntl.NumberFormat.DATA.ticks: roughly how many gridlines the value axis gets (default5).--bc-bar,--bc-bar-topand--bc-bar-activeon.bc-chart: the indigo bar gradient and the highlighted bar color.--bc-plot-h: plot height,15remon phones and17.5remfrom 640px.width: min(62%, 2.5rem)in.bc-fill: make bars wider or slimmer.--bc-tip-bg: the dark tooltip background.
Use cases
- E-commerce dashboards: compare monthly sales with the previous year at a glance.
- SaaS revenue reports: show monthly recurring revenue with exact values on hover.
- Admin panels: a compact, library-free chart for any twelve-month metric.
- Annual reports and landing pages: present growth figures in a clean, readable card.
Accessibility
Each bar is a real button with an aria-label such as "March, this year: $47,900, +19.2% compared with last year", so screen readers and keyboard users get the value without the tooltip. Arrow keys, Home and End move between bars, and focus is clearly outlined. The radio buttons are grouped in a fieldset with a hidden legend. A visually hidden table.bc-sr lists every month for both years, and a polite live region announces the new total when the year changes. Reduced motion disables the animation.
Usage notes
Several charts can share a page; the script gives each radio group a unique name.