Pantrix UI

Scatter Chart

A scatter chart: points set out by two values, one along the chart and one up it, to show how the two go together. With a third value for the size of each point it is a bubble chart.

import com.pantrixui.components.chart.scatter.PantrixScatterChart

Examples

Two values, two groups

Fifty days of a café: the day's temperature and the cold drinks sold that day. Weekdays and weekends are separate series; the points go up with the temperature, and the weekends higher still.

Move the pointer near a point, or touch one: the tooltip writes the day's two values under the titles of the axes. Pressing an entry of the legend takes that series out of the chart.

PantrixScatterChart(
    series = listOf(
        ScatterSeries("weekdays", "Weekdays", cafeDays(weekend = false), x = { it.heat }, y = { it.sold }),
        ScatterSeries("weekend", "Weekend", cafeDays(weekend = true), x = { it.heat }, y = { it.sold })
    ),
    xAxis = ChartAxis(title = "Temperature (°C)"),
    yAxis = ChartAxis(title = "Cold drinks"),
    description = "Cold drinks sold by temperature",
    tagKey = "plain"
)

internal fun cafeDays(weekend: Boolean): List<CafeDay> = List(50) { it }
    .filter { (it % 7 >= 5) == weekend }
    .map { day ->
        val heat = 12.0 + (day * 7 % 19) + (day % 4) * 0.5
        val wobble = (day * 37 % 29) - 14
        val busy = if (weekend) 45 else 0
        CafeDay(
            heat = heat,
            sold = (heat * 6.5 - 40 + wobble + busy).toInt().coerceAtLeast(4),
            hot = (190 - heat * 4.5 + wobble).toInt().coerceAtLeast(4),
            food = 85 + wobble + busy / 3
        )
    }

internal class CafeDay(
    val heat: Double,
    val sold: Int,
    val hot: Int,
    val food: Int
)

Bubbles

The products of a shop: price, rating and, as the bubble's size, how many were sold in the month. Each point goes by the product's name (label); sizeAxis says what the size is.

The area grows with the value: the bubble of a product that sold twice as many takes twice the area, not twice the width. Bubbles are drawn half transparent, so that one under another shows through.

val strings = PantrixTheme.strings
PantrixScatterChart(
    series = listOf(
        ScatterSeries(
            key = "products",
            name = "Products",
            data = shopProducts(),
            x = { it.price },
            y = { it.rating },
            size = { it.sold },
            label = { it.name }
        )
    ),
    xAxis = ChartAxis(title = "Price", format = { "${strings.decimal(it)} ₺" }),
    yAxis = ChartAxis(title = "Rating", min = 3.0, max = 5.0),
    sizeAxis = ChartAxis(title = "Sales", format = { "${strings.decimal(it)} sold" }),
    description = "Price, rating and sales of the products",
    tagKey = "bubbles"
)

internal fun shopProducts(): List<ShopProduct> = listOf(
    ShopProduct("Headset", 1450.0, 4.6, 820),
    ShopProduct("Keyboard", 980.0, 4.3, 540),
    ShopProduct("Mouse", 420.0, 4.4, 1260),
    ShopProduct("Monitor", 5200.0, 4.7, 310),
    ShopProduct("Webcam", 760.0, 3.9, 280),
    ShopProduct("Speaker", 1890.0, 4.1, 190),
    ShopProduct("Microphone", 2350.0, 4.5, 150),
    ShopProduct("Charger", 310.0, 4.0, 1640),
    ShopProduct("Cable", 120.0, 3.6, 2100),
    ShopProduct("Tablet pen", 1150.0, 4.2, 95),
    ShopProduct("Laptop stand", 640.0, 4.8, 470),
    ShopProduct("External disk", 2780.0, 4.4, 360)
)

internal class ShopProduct(
    val name: String,
    val price: Double,
    val rating: Double,
    val sold: Int
)

Lines and bands

Marks that help read the points: a line at the average temperature, a line at the day's target, and a band over the hottest days. A mark's x is here not the name of a place but a value of the axis along the chart.

PantrixScatterChart(
    series = listOf(
        ScatterSeries("days", "Days", cafeDays(weekend = false), x = { it.heat }, y = { it.sold })
    ),
    xAxis = ChartAxis(title = "Temperature (°C)"),
    yAxis = ChartAxis(title = "Cold drinks"),
    marks = listOf(
        ChartReferenceBand(x1 = 28, label = "Hot days"),
        ChartReferenceLine(y = 150, label = "Target"),
        ChartReferenceLine(x = 21, label = "Average")
    ),
    description = "Cold drinks sold by temperature, with the target",
    tagKey = "marks"
)

internal fun cafeDays(weekend: Boolean): List<CafeDay> = List(50) { it }
    .filter { (it % 7 >= 5) == weekend }
    .map { day ->
        val heat = 12.0 + (day * 7 % 19) + (day % 4) * 0.5
        val wobble = (day * 37 % 29) - 14
        val busy = if (weekend) 45 else 0
        CafeDay(
            heat = heat,
            sold = (heat * 6.5 - 40 + wobble + busy).toInt().coerceAtLeast(4),
            hot = (190 - heat * 4.5 + wobble).toInt().coerceAtLeast(4),
            food = 85 + wobble + busy / 3
        )
    }

internal class CafeDay(
    val heat: Double,
    val sold: Int,
    val hot: Int,
    val food: Int
)

Telling series apart by shape

Series are told apart by shape as well as by colour: each is drawn with its own mark (marker). It reads for someone who cannot tell the colours apart, and on a black and white print.

val days = cafeDays(weekend = false)
PantrixScatterChart(
    series = listOf(
        ScatterSeries("cold", "Cold drinks", days, x = { it.heat }, y = { it.sold }),
        ScatterSeries("hot", "Hot drinks", days, x = { it.heat }, y = { it.hot }, marker = ChartMarker.Square),
        ScatterSeries("food", "Food", days, x = { it.heat }, y = { it.food }, marker = ChartMarker.Triangle)
    ),
    xAxis = ChartAxis(title = "Temperature (°C)"),
    yAxis = ChartAxis(title = "Sales"),
    description = "Sales of three groups of products by temperature",
    tagKey = "markers"
)

internal fun cafeDays(weekend: Boolean): List<CafeDay> = List(50) { it }
    .filter { (it % 7 >= 5) == weekend }
    .map { day ->
        val heat = 12.0 + (day * 7 % 19) + (day % 4) * 0.5
        val wobble = (day * 37 % 29) - 14
        val busy = if (weekend) 45 else 0
        CafeDay(
            heat = heat,
            sold = (heat * 6.5 - 40 + wobble + busy).toInt().coerceAtLeast(4),
            hot = (190 - heat * 4.5 + wobble).toInt().coerceAtLeast(4),
            food = 85 + wobble + busy / 3
        )
    }

internal class CafeDay(
    val heat: Double,
    val sold: Int,
    val hot: Int,
    val food: Int
)

Pressing a point

onSelect gives the point that is pressed: its x is the row's place in its series, its label the point's name. Press a bubble, or go to one with the keyboard and press Enter, and the line under the card writes the product that was pressed.

val report = LocalExampleEvents.current
val products = shopProducts()
PantrixScatterChart(
    series = listOf(
        ScatterSeries(
            key = "products",
            name = "Products",
            data = products,
            x = { it.price },
            y = { it.rating },
            size = { it.sold },
            label = { it.name }
        )
    ),
    xAxis = ChartAxis(title = "Price"),
    yAxis = ChartAxis(title = "Rating", min = 3.0, max = 5.0),
    sizeAxis = ChartAxis(title = "Sales"),
    onSelect = { point -> report("${point.label}: ${products[point.x as Int].sold} sold") },
    description = "Price, rating and sales of the products",
    tagKey = "pressed"
)

internal fun shopProducts(): List<ShopProduct> = listOf(
    ShopProduct("Headset", 1450.0, 4.6, 820),
    ShopProduct("Keyboard", 980.0, 4.3, 540),
    ShopProduct("Mouse", 420.0, 4.4, 1260),
    ShopProduct("Monitor", 5200.0, 4.7, 310),
    ShopProduct("Webcam", 760.0, 3.9, 280),
    ShopProduct("Speaker", 1890.0, 4.1, 190),
    ShopProduct("Microphone", 2350.0, 4.5, 150),
    ShopProduct("Charger", 310.0, 4.0, 1640),
    ShopProduct("Cable", 120.0, 3.6, 2100),
    ShopProduct("Tablet pen", 1150.0, 4.2, 95),
    ShopProduct("Laptop stand", 640.0, 4.8, 470),
    ShopProduct("External disk", 2780.0, 4.4, 360)
)

internal class ShopProduct(
    val name: String,
    val price: Double,
    val rating: Double,
    val sold: Int
)

Export

export = ChartExport(...) puts an export button over the chart: press it and choose the kind (PNG, SVG or PDF). The chart makes the file and hands it to onExport; below, its name and size show, and a PNG's picture, and "Save the file" hands it over in the platform's way.

The file has the axes, the points and the legend under them; the point that is pointed at and its tooltip are not in it.

var file by remember { mutableStateOf<ChartExportFile?>(null) }
PantrixScatterChart(
    series = listOf(
        ScatterSeries("weekdays", "Weekdays", cafeDays(weekend = false), x = { it.heat }, y = { it.sold }),
        ScatterSeries("weekend", "Weekend", cafeDays(weekend = true), x = { it.heat }, y = { it.sold })
    ),
    xAxis = ChartAxis(title = "Temperature (°C)"),
    yAxis = ChartAxis(title = "Cold drinks"),
    description = "Cold drinks sold by temperature",
    tagKey = "exported",
    export = ChartExport(fileName = "cold-drinks") { file = it }
)
ExportedFile(file, tagKey = "exported")

internal fun cafeDays(weekend: Boolean): List<CafeDay> = List(50) { it }
    .filter { (it % 7 >= 5) == weekend }
    .map { day ->
        val heat = 12.0 + (day * 7 % 19) + (day % 4) * 0.5
        val wobble = (day * 37 % 29) - 14
        val busy = if (weekend) 45 else 0
        CafeDay(
            heat = heat,
            sold = (heat * 6.5 - 40 + wobble + busy).toInt().coerceAtLeast(4),
            hot = (190 - heat * 4.5 + wobble).toInt().coerceAtLeast(4),
            food = 85 + wobble + busy / 3
        )
    }

internal class CafeDay(
    val heat: Double,
    val sold: Int,
    val hot: Int,
    val food: Int
)

Looking closer

zoom = ChartZoom() lets the reader look closer at both values at once: a wheel with Ctrl or Command and two fingers look closer about the point between them, plus and minus on the chart about the point that is pointed at, and the buttons about the middle. A drag of the mouse moves what is in view every way; the point the arrows go to comes into view.

Both axes are marked anew for the values in view, and what lies past the plot's edges is cut off. The part across is state.window, the part up state.valueWindow.

PantrixScatterChart(
    series = listOf(
        ScatterSeries("weekdays", "Weekdays", cafeDays(weekend = false), x = { it.heat }, y = { it.sold }),
        ScatterSeries("weekend", "Weekend", cafeDays(weekend = true), x = { it.heat }, y = { it.sold })
    ),
    xAxis = ChartAxis(title = "Temperature (°C)"),
    yAxis = ChartAxis(title = "Cold drinks"),
    description = "Cold drinks sold by temperature",
    tagKey = "zoomed",
    zoom = ChartZoom()
)

internal fun cafeDays(weekend: Boolean): List<CafeDay> = List(50) { it }
    .filter { (it % 7 >= 5) == weekend }
    .map { day ->
        val heat = 12.0 + (day * 7 % 19) + (day % 4) * 0.5
        val wobble = (day * 37 % 29) - 14
        val busy = if (weekend) 45 else 0
        CafeDay(
            heat = heat,
            sold = (heat * 6.5 - 40 + wobble + busy).toInt().coerceAtLeast(4),
            hot = (190 - heat * 4.5 + wobble).toInt().coerceAtLeast(4),
            food = 85 + wobble + busy / 3
        )
    }

internal class CafeDay(
    val heat: Double,
    val sold: Int,
    val hot: Int,
    val food: Int
)

API

series

A scatter chart: points set out by two values, one along the chart and one up it, to show how the two go together. With a third value for the size of each point it is a bubble chart.

ParameterTypeDefaultWhat it is
seriesList<ScatterSeries<*>>the series, in the order they are listed. Their keys tell them apart, and two with one key are refused.
modifierModifierModifierthe chart's modifier. A height here is the chart's, the legend's with it.
xAxisChartAxis?ChartAxis()the axis of the values along the chart, under it. Its title is what those values are called in the tooltip and to a screen reader. null draws nothing of it.
yAxisChartAxis?ChartAxis()the axis of the values up the chart, beside it. null draws nothing of it.
sizeAxisChartAxis?nullwhat the bubbles' sizes measure: its title is what a size is called in the tooltip and to a screen reader, its format how one is written, and its max the size the largest bubble stands for, where that is not the largest in the chart. Nothing of it is drawn, and nothing else of it is read.
marksList<ChartMark>emptyList()the lines and the bands drawn in the plot besides the points.
legendChartLegend?ChartLegend()where the names of the series stand. null shows none.
tooltipChartTooltip?ChartTooltip()what is shown for the point that is pointed at: its heading is what the point is called, and under it a row for each of its values. Its crosshair is a line across the plot and one up it through the point, and its format writes the value up the chart; a point has no place to share, and shared is not read. A tooltip's own content is given the point's values, the one along the chart first. null shows none.
empty(@Composable () -> Unit)?nullwhat the chart shows while it has no value to draw, in the place of the theme's words for it ("No data"), which a screen reader is then not told either. null writes the words.
stateChartStaterememberChartState()what the reader has hidden and what they point at: the chart's own where none is given.
descriptionString?nullwhat the chart shows, in a few words, for a screen reader: the first thing it says of the chart. Left out, the chart is announced by the names of its series.
onSelect((ChartPoint) -> Unit)?nullcalled with the point that is pressed: its x is its place among its series' rows, counted from nought, its label what it is called, and its value its value up the chart.
styleChartStyle?nullthis chart's own geometry: the fields set here, with the theme's for the rest.
colorsChartColors?nullthis chart's own colours, where they are not the theme's.
tagKeyString?nullthe chart's automation key: the chart is ScatterChart_<tagKey>, its plot ScatterChart_<tagKey>.Plot, a point ScatterChart_<tagKey>.Point_<series' key>_<number>, the number being its row's counted from one, the legend's entry of a series ScatterChart_<tagKey>.Legend_<series' key> and the tooltip ScatterChart_<tagKey>.Tooltip. A press on a point is reported as a ScatterChart select event with the series' key, and a press on a legend's entry as a toggle event with the key and whether the series is shown. Nothing of the values is reported.
exportChartExport?nullwhat lets the reader take the chart out of the app as a PNG, an SVG or a PDF: a button over the chart makes the file of it as it is and hands it over (see ChartExport). null, the chart has no such button.
zoomChartZoom?nullwhat lets the reader look closer at a part of the chart's values, across it and up it alike (see ChartZoom): a wheel with Ctrl or Command and two fingers look closer about the point between them, plus and minus on the chart about the point that is pointed at, and the buttons about the middle; a drag of the mouse moves what is in view every way, the buttons along it, and the arrows bring the point they go to into view. Both axes are marked anew for the values in view, and what lies past the plot's edges is cut off. The parts are kept in state, as its window and its valueWindow. A scatter chart has no navigator: its navigator is not read, nor is its fitValues. null, the chart shows all its values.

ChartStyle

A chart's geometry: how tall it is where nothing says, how thick its lines and how large its marks are, and the room between its parts; and whether its fills have textures besides their colours.

ParameterTypeDefaultWhat it is
heightDpDp.UnspecifiedHow tall a chart's plot is, with its axes, where the chart's modifier gives it no height. A legend above or under the plot adds its own.
lineWidthDpDp.UnspecifiedThe width of a series' line.
markerSizeDpDp.UnspecifiedThe width and height of the mark on a point.
selectedMarkerSizeDpDp.UnspecifiedThe same of the mark on the point that is pointed at.
gridWidthDpDp.UnspecifiedThe width of the grid's lines, of the lines along the axes and of the line that marks the place pointed at. Zero draws none.
labelGapDpDp.UnspecifiedBetween the plot and what is written beside it and under it, and between that and an axis' title.
tickSpacingDpDp.UnspecifiedThe least room between two values marked along the axis of values: what decides how many are marked where the axis does not say.
legendGapDpDp.UnspecifiedBetween the legend and the chart it stands beside.
legendItemGapDpDp.UnspecifiedBetween one entry of the legend and the next.
swatchSizeDpDp.UnspecifiedHow wide the sample of a series is in the legend and in the tooltip: a short run of its line, or a patch of its fill.
swatchGapDpDp.UnspecifiedBetween that sample and the name after it.
tooltipShapeShapeSpecShapeSpec.UnspecifiedThe tooltip's corners: a step of the brand's shape scale, or a fixed shape.
tooltipHorizontalPaddingDpDp.UnspecifiedBetween the tooltip's sides and what it holds.
tooltipVerticalPaddingDpDp.UnspecifiedBetween its top and bottom and what it holds, and between its heading and rows.
tooltipGapDpDp.UnspecifiedBetween the place pointed at and the tooltip beside it.
tooltipElevationDpDp.UnspecifiedThe tooltip's shadow.
barGapDpDp.UnspecifiedBetween two bars that stand side by side at a place. Bars too thin for it stand closer.
barMaxWidthDpDp.UnspecifiedThe widest a bar is, however much room its place has: a chart of three places is not three slabs.
barRadiusDpDp.UnspecifiedHow round the corners of a bar's end are, the end away from nought.
sliceGapDpDp.UnspecifiedBetween two slices of a pie that lie side by side, the same from the pie's middle to its rim.
selectedSliceOffsetDpDp.UnspecifiedHow far the slice of a pie that is pointed at stands out past the pie's rim. The pie keeps this much room about itself, so that the slice has where to stand.
gaugeWidthDpDp.UnspecifiedHow thick the arc of a gauge is. An arc is no thicker than half its radius.
meterHeightDpDp.UnspecifiedHow tall the bar of a meter group is.
trackerHeightDpDp.UnspecifiedHow tall the blocks of a status tracker are.
bubbleMinSizeDpDp.UnspecifiedHow far across the smallest bubble of a scatter chart is: a point whose size is nothing, or too little to be seen as an area, is still a point.
bubbleMaxSizeDpDp.UnspecifiedHow far across its largest bubble is: the one whose size is the most of the chart's.
navigatorHeightDpDp.UnspecifiedHow tall the strip under a chart's plot is, on which the part of the chart that is in view is marked and moved, in a chart whose zoom has one: how wide, beside a plot whose places go down its side.
barBandShareFloatFloat.NaNHow much of a place's band its bars take, more than nought and one at the most: the rest is the room between one place's bars and the next's. Bars are no wider than barMaxWidth all the same.
markLineWidthDpDp.UnspecifiedThe width of a mark's line: a target, a limit, a day. Zero draws none, and the mark's words are written all the same.
markBandAlphaFloatFloat.NaNHow much of its colour a mark's band is drawn in, from nought to one: a band lies behind the series, and is a tint.
patternsBoolean?nullWhether the fills of a chart's series, slices and tiles are drawn with textures besides their colours, and its lines broken in turn (ChartPattern): what tells them apart for a reader who cannot tell the colours apart. Left out, as the system is set: on where it is set to tell things apart without colour, iOS's "Differentiate Without Color" and Android's colour correction, and off otherwise.
nodeWidthDpDp.UnspecifiedHow wide a node of a sankey is.
nodeGapDpDp.UnspecifiedBetween two nodes of a column of a sankey.

ChartColors

What a chart is painted with, for a chart that does not take the theme's colours: a colour that is set is used, and one that is left out stays the theme's.

ParameterTypeDefaultWhat it is
seriesList<Color>emptyList()The colours the series are drawn in, in the order they are handed out, for the series that name none of their own. Empty, they are the theme's chart colours.
gridColorColor.UnspecifiedThe lines drawn across the chart at the marks of an axis. The theme's divider.
axisColorColor.UnspecifiedThe line along an axis. The theme's border.
labelColorColor.UnspecifiedWhat is written beside the axes: the values, the places and the axes' titles, and the names in the legend. The theme's textSecondary.
crosshairColorColor.UnspecifiedThe line that marks the place pointed at. The theme's borderHover: stronger than the grid it crosses, and quieter than the series it stands behind.
tooltipContainerColorColor.UnspecifiedThe tooltip's fill. The theme's surface.
tooltipBorderColorColor.UnspecifiedThe tooltip's edge. The theme's border.
tooltipTextColorColor.UnspecifiedThe tooltip's heading and its values. The theme's textPrimary.
tooltipLabelColorColor.UnspecifiedThe names of the series in the tooltip. The theme's textSecondary.
risingColorColor.UnspecifiedWhat a value that goes up is drawn in: a step of a waterfall that adds. The theme's success.
fallingColorColor.UnspecifiedWhat a value that goes down is drawn in: a step of a waterfall that takes away. The theme's danger.

Accessibility