Bar chart
Bars, stacked bars, and horizontal bars of the rows of a list signal.
Result
use hypersig::{Page, Segment, Signals};
use hypersig_ui::{Chart, Tabs, token};
use serde::{Deserialize, Serialize};
#[derive(Segment, Serialize, Deserialize, Clone, Copy, PartialEq)]
pub(super) enum Kind {
Bar,
Stacked,
Horizontal,
}
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Month {
name: String,
desktop: i32,
mobile: i32,
}
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Traffic {
kind: Kind,
months: Vec<Month>,
}
pub(super) fn page() -> Page<Traffic> {
// Each kind of chart draws the same two series of the same months.
let visitors = |chart: Chart<Traffic, Month>| {
chart
.series(
"Desktop",
Month::desktop(),
token::chart_1(),
)
.series(
"Mobile",
Month::mobile(),
token::chart_2(),
)
};
let label = "Visitors";
let names = [
L { en: "Jan", ja: "1 月" },
L { en: "Feb", ja: "2 月" },
L { en: "Mar", ja: "3 月" },
L { en: "Apr", ja: "4 月" },
L { en: "May", ja: "5 月" },
L { en: "Jun", ja: "6 月" },
];
let months = names
.into_iter()
.zip([(186, 80), (305, 200), (237, 120), (73, 190), (209, 130), (214, 140)])
.map(|(name, (desktop, mobile))| Month {
name: site.t(name).to_owned(),
desktop,
mobile,
})
.collect();
let tabs = Tabs::new(label, Traffic::kind(), |kind| match kind {
Kind::Bar => (
"Bar",
visitors(Chart::bar(label, Traffic::months(), Month::name())).render(),
),
Kind::Stacked => (
"Stacked",
visitors(Chart::stacked_bar(label, Traffic::months(), Month::name())).render(),
),
Kind::Horizontal => (
"Horizontal",
visitors(Chart::horizontal_bar(label, Traffic::months(), Month::name())).render(),
),
});
Page::new(
label,
Traffic {
kind: Kind::Bar,
months,
},
)
.child(tabs)
}Accessibility
A bar chart is the same <figure> as a line chart: the bars, the labels, the legend, and the box under the pointer are hidden from assistive technology, and a screen reader reads a visually hidden <table> of the rows, captioned with the chart's label, with a column per series. The chart takes no keys.
Three kinds of bars
Three constructors of Chart draw bars. They take the same arguments as Chart::line, and the chart takes series, format, and max_labels as on the Chart page:
impl<S: 'static, T: 'static> Chart<S, T> {
pub fn bar<R: FromPage>(
label: impl Into<String>,
rows: Sig<S, Vec<T>, R>,
category: Sig<T, String>,
) -> Self;
pub fn stacked_bar<R: FromPage>(/* the same */) -> Self;
pub fn horizontal_bar<R: FromPage>(/* the same */) -> Self;
}| Constructor | What each row draws |
|---|---|
Chart::bar | A group of bars side by side, one per series, from 0 up, or down for a negative value. |
Chart::stacked_bar | One bar in which each series stacks on the ones added before it. Positive values stack up from 0 and negative ones down from it, so the scale reaches the largest sum of a row's positive values and the smallest sum of its negative ones. |
Chart::horizontal_bar | A band down the side, labeled on the left, with a bar per series running from 0 along the scale at the bottom. It suits categories whose names are too long to sit under a vertical bar. |
The scale is fitted to the values as for a line chart, on a step of 1, 2, 2.5, or 5 times a power of ten, and a statement or a patch that changes a value moves its bar and refits the scale with no request.
Colors
Each series is drawn in the Token<Color> it takes, usually one of token::chart_1 to token::chart_5, so the bars follow the theme and dark mode. A color written as a string has no conversion to a token and is E0308:
use hypersig::Signals;
use hypersig_ui::Chart;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Quarter {
name: String,
income: f64,
}
#[derive(Signals, Serialize, Deserialize)]
struct Results {
quarters: Vec<Quarter>,
}
// error[E0308]: mismatched types: expected `Token<Color>`, found `&str`
let chart = Chart::bar("Income", Results::quarters(), Quarter::name())
.series("Income", Quarter::income(), "green");The HTML
A bar chart of two series over two months draws one <rect> per row and series inside the same <svg> as a line chart. Here the scale runs from 0 to 400, and each value is placed in hundredths of the drawing:
<svg viewBox="0 0 100 100" preserveAspectRatio="none" width="100%" height="100%">
<line x1="0" x2="100" vector-effect="non-scaling-stroke" y1="100.000" y2="100.000"></line>
…
<rect x="5.000" width="20.000" y="53.500" height="46.500"></rect>
<rect x="55.000" width="20.000" y="23.750" height="76.250"></rect>
<rect x="25.000" width="20.000" y="80.000" height="20.000"></rect>
<rect x="75.000" width="20.000" y="50.000" height="50.000"></rect>
</svg>The first two <rect> elements are Desktop in January and February, and the last two are Mobile. The browser updates y and height when a value or the scale changes.
What is not checked
The bars of a row share its band, so many series in a bar chart draw thin bars, and nothing limits their number. A stacked bar adds values of different series, which makes sense only where they are parts of one whole; the compiler does not know. Each bar computes the scale from every row, so the work of a change grows with the square of the number of rows, as for a line chart.
Example: income and costs
A stacked bar chart where the costs are negative, so each bar reaches above and below 0:
use hypersig::{NumberFormat, Page, Signals};
use hypersig_ui::{Chart, token};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Quarter {
name: String,
income: f64,
costs: f64,
}
#[derive(Signals, Serialize, Deserialize)]
struct Results {
quarters: Vec<Quarter>,
}
const THOUSANDS: NumberFormat = NumberFormat::new().prefix("$").suffix("k");
// Costs are negative, so their part of each bar reaches down from 0.
let chart = Chart::stacked_bar("Income and costs", Results::quarters(), Quarter::name())
.series("Income", Quarter::income(), token::chart_1())
.series("Costs", Quarter::costs(), token::chart_5())
.format(THOUSANDS);
let quarters = vec![
Quarter { name: "Q1".into(), income: 120.0, costs: -45.0 },
Quarter { name: "Q2".into(), income: 150.0, costs: -60.0 },
Quarter { name: "Q3".into(), income: 90.0, costs: -70.0 },
];
let html = Page::new("Results", Results { quarters }).child(chart).render();Next, Donut chart draws each row's share of a total.