Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/semantics.scoop

Selected production source · src/chart/semantics.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.window.accessibility.*
6/** The accessible data table reads the same immutable series and datum keys as the plot. */
7internal class ChartSemanticSource(
8 private val snapshot: ChartSnapshot,
9 private val enabled: Boolean
10): SemanticCollectionSource {
11 public override val count: Long get() = snapshot.data.datums.size
12 public override val search: SemanticItemSearch get() = SemanticItemSearch(name = true)
13 public override fun keyAt(index: Long): SemanticItemKey =
14 SemanticItemKey.Text(snapshot.data.datums[index].key.value)
15 public override fun indexOfKey(key: SemanticItemKey): Long? =
16 when (key) {
17 case Text(val value) -> snapshot.data.indexOfDatum(ChartDatumKey(value))
18 case Number(_) -> None
19 }
20 public override fun describe(index: Long): SemanticItemProperties? =
21 Some(
22 SemanticItemProperties(
23 name = Some(snapshot.data.datums[index].label),
24 enabled = enabled
25 )
26 )
27 fun grid(): SemanticGrid =
28 SemanticGrid(
29 Array<SemanticGridColumn>(snapshot.visible.size) {
30 column ->
31 val series = snapshot.data.series[snapshot.visible[column]]
32 SemanticGridColumn(SemanticItemKey.Text(series.key.value), Some(series.label))
33 },
34 { key, column -> cell(key, column) }
35 )
36 private fun cell(key: SemanticItemKey, column: Long): SemanticItemProperties? {
37 val row = indexOfKey(key) ?: return None
38 val series = snapshot.data.series[snapshot.visible[column]]
39 return Some(
40 SemanticItemProperties(
41 name = Some(snapshot.data.datums[row].label + ", " + series.label),
42 value =
43 when (series.values[row]) {
44 case Some(val value) -> Some(f"${value}");
45 case None -> None
46 },
47 description =
48 if (series.values[row] == None)
49 Some("No value")
50 else
51 None,
52 enabled = enabled
53 )
54 )
55 }