human-kit charts

Dot

Chart.Dot draws one dot for each row, at the x value and the y value of the row. A scatter plot is a chart with a dot mark. A dot mark can also go on a line chart or a bar chart, for example to show targets.

Weight and fuel economy of cars
originWeight (lb)Fuel economy (mpg)
Europe2,05036
Europe2,13031
Europe2,23430
Europe2,37226
Europe2,43029
Europe2,67027
Europe2,95023
Europe3,19022
Japan1,83535
Japan1,98538
Japan2,06531
Japan2,13033
Japan2,29032
Japan2,37030
Japan2,56527
Japan2,72025
USA2,72026
USA2,93324
USA3,08622
USA3,43319
USA3,50418
USA3,69315
USA3,85015
USA4,35414
USA4,44013

Anatomy

<script lang="ts">
	import { Chart } from '@human-kit/charts';
</script>

<Chart.Root
	data={cars}
	x="weight"
	y="mpg"
	series="origin"
	xLabel="Weight (lb)"
	yLabel="Fuel economy (mpg)"
	xScale={{ zero: false }}
	yScale={{ zero: false }}
>
	<Chart.Title>Weight and fuel economy</Chart.Title>
	<Chart.Plot>
		<Chart.Axis position="bottom" />
		<Chart.Axis position="left" />
		<Chart.Dot />
	</Chart.Plot>
	<Chart.Legend />
	<Chart.DataTable />
</Chart.Root>

The mark makes a <g> with one group for each series. A series group holds one <path> for each dot.

Labels

On a scatter plot, the two values of a dot are measures. A name such as "Europe, 2,130, 31" does not tell which value is the weight. Give xLabel and yLabel to Chart.Root:

  • The name of a dot puts the label before each value: "Europe, Weight (lb) 2,130, Fuel economy (mpg) 31".
  • An axis without a label shows the label of its channel.
  • The tooltip and the data table show the labels.

A name of a dot starts with the series, because the series tells the group, and the two values are both measures. The labels are also available on the other marks.

Scales

The y scale includes zero by default, because a bar needs it. On a scatter plot, give zero: false to the scales, thus the dots fill the plot.

Shapes

Color must not be the only thing that identifies a series. The dots of two series are mixed on the plot, thus each series also gets its own shape. With symbol="auto" (the default), the series get the shapes in this order: circle, square, triangle, diamond, cross and triangle-down. After six series, the order starts again.

All of the shapes have the same area as a circle with the radius r. The default radius is 4 pixels. Give a shape name, for example symbol="circle", to give that shape to all of the series.

Chart.Legend shows the shape of each series in its swatch.

Order

The dots of a series are in the order of x, then of y. The keyboard, the order of the elements and the data table use this order. Thus a screen reader user can predict the next dot, and the data does not need an order.

Two dots at the same position are two points. Each one has its own focus target and its own row in the data table.

Keyboard

ArrowRight and ArrowLeft move to the next or the previous dot of the series, in the order of x. ArrowUp and ArrowDown move to the dot with the closest x value in the previous or the next series. When two dots have the same distance in x, the dot with the closest y value gets the focus. The full table is on the Accessibility page.

Number of dots

Each dot is an element and a focus target. With 2,000 dots, the chart draws in about 0.5 seconds, and a key press moves the focus in about 7 milliseconds. With 5,000 dots, the chart draws in about 1.3 seconds, and a key press takes about 15 milliseconds. Above 2,000 dots, think about a summary of the data.

Styles

Give each series a color with the CSS color. The dots use it as their fill.

.chart [data-series='Europe'] {
	color: #2563eb;
}

.chart [data-mark='dot'] [data-point] {
	fill-opacity: 0.8;
}

.chart [data-mark='dot'] [data-point][data-focus-visible] {
	stroke: currentColor;
	stroke-width: 2;
}

The focus ring is a stroke on the path, thus it follows the shape.

ElementAttributeWhen
Group of the markdata-markAlways. The value is dot.
Group of a seriesdata-seriesThe series has a name. The value is the name.
Group of a seriesdata-symbolAlways. The value is the name of the shape.
Dotdata-pointAlways.
Dotdata-focusedThe dot has the focus.
Dotdata-focus-visibleThe dot has the focus, and the focus came from the keyboard.
Dotdata-selectedThe dot is selected.

API reference

Dot

A dot mark. It makes one <path> per row, in the shape of its series.

Prop Type Default

* required.

Data attribute Description
data-mark On the group of the mark. The value is dot.
data-series On the group of each series. The value is the name of the series.
data-symbol On the group of each series. The value is the name of the shape.
data-point On each point: a circle, a rect for a bar, or a path for a dot.
data-focused On the point that has the focus.
data-focus-visible On the focused point, when the focus came from the keyboard.
data-selected On the selected point. The point also has aria-current="true".