Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/draw.scoop

Selected production source · src/chart/draw.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.style.*
6internal fun chartDraw(canvas: Canvas, plan: ChartPlan, geometry: ChartGeometry) {
7 if (!chartIsCartesian(plan.input.kind)) {
8 chartDrawPolar(canvas, plan, geometry)
9 return
10 }
11 val appearance = plan.appearance
12 val input = plan.input
13 val d = appearance.density
14 for (line in geometry.grid)
15 canvas.drawLine(
16 line.start,
17 line.end,
18 Paint(appearance.theme.tokens.palette.border.withAlpha(0.5), strokeWidth = d)
19 )
20 canvas.withState {
21 canvas ->
22 canvas.clipRect(geometry.plot)
23 for (series in geometry.series) {
24 val color = series.series.color
25 if (series.series.mark == ChartMark.Bar) {
26 for (sample in series.samples)
27 when (sample) {
28 case Some(val value) -> when (value.shape) {
29 case Rectangle(val bounds) -> canvas.drawRoundRect(
30 bounds,
31 input.barRadius * d,
32 input.barRadius * d,
33 Paint(color)
34 );
35 else -> {}
36 }
37 case None -> {}
38 }
39 } else {
40 for (run in chartRuns(series.samples, input.connectMissing)) {
41 val points = Array<Point>(run.size) { at -> run[at].point }
42 if (series.series.mark == ChartMark.Area && geometry.plot.height > 0.0) {
43 path {
44 chartTrace(
45 this,
46 points,
47 input.curve,
48 input.orientation == Orientation.Horizontal
49 )
50 val base = Array<Point>(run.size) { at -> run[run.size - 1L - at].base }
51 chartTrace(
52 this,
53 base,
54 input.curve,
55 input.orientation == Orientation.Horizontal,
56 false
57 )
58 this.closeContour()
59 }.use {
60 path -> canvas.drawPath(
61 path,
62 Paint(
63 color,
64 gradient =
65 Some(
66 Gradient.linear(
67 Point(geometry.plot.x, geometry.plot.y),
68 Point(
69 geometry.plot.x,
70 geometry.plot.y + geometry.plot.height
71 ),
72 [
73 GradientStop(
74 0.0,
75 color.withAlpha(color.alpha * 0.45)
76 ),
77 GradientStop(
78 1.0,
79 color.withAlpha(color.alpha * 0.04)
80 )
81 ]
82 )
83 )
84 )
85 )
86 }
87 }
88 path {
89 chartTrace(
90 this,
91 points,
92 input.curve,
93 input.orientation == Orientation.Horizontal
94 )
95 }.use {
96 path -> canvas.drawPath(
97 path,
98 Paint(
99 color,
100 PaintingStyle.Stroke,
101 2.0 * d,
102 StrokeCap.Round,
103 StrokeJoin.Round
104 )
105 )
106 }
107 }
108 if (input.pointRadius > 0.0)
109 for (sample in series.samples)
110 when (sample) {
111 case Some(val value) -> canvas.drawCircle(
112 value.point,
113 input.pointRadius * d,
114 Paint(color)
115 );
116 case None -> {}
117 }
118 }
119 }
120 }
121 for (label in geometry.labels)
122 appearance.draw(canvas, label.text, label.origin)
123 if (input.labels)
124 for (series in geometry.series)
125 for (sample in series.samples)
126 when (sample) {
127 case Some(val value) -> if (value.visible) {
128 val text = chartNumber(value.value)
129 val extent = appearance.measure(text)
130 val origin =
131 Point(
132 chartClamp(
133 value.point.x - extent.width / 2.0,
134 0.0,
135 lowerBound(geometry.size.width - extent.width, 0.0)
136 ),
137 chartClamp(
138 value.point.y - extent.height - 6.0 * d,
139 0.0,
140 lowerBound(geometry.size.height - extent.height, 0.0)
141 )
142 )
143 appearance.draw(canvas, text, origin, true)
144 }
145 case None -> {}
146 }
149internal fun chartDrawActive(
150 canvas: Canvas,
151 plan: ChartPlan,
152 geometry: ChartGeometry,
153 selection: ChartSelection?
154) {
155 if (!chartIsCartesian(plan.input.kind)) {
156 chartDrawPolarActive(canvas, plan, geometry, selection)
157 return
158 }
159 val active =
160 when (selection) {
161 case Some(val key) -> geometry.find(key);
162 case None -> None
163 }
164 when (active) {
165 case None -> {}
166 case Some(val sample) -> {
167 val d = plan.appearance.density
168 val color = plan.appearance.theme.tokens.palette.ring
169 canvas.withState {
170 canvas ->
171 canvas.clipRect(geometry.plot)
172 when (sample.shape) {
173 case Rectangle(val bounds) -> canvas.drawRoundRect(
174 bounds,
175 plan.input.barRadius * d,
176 plan.input.barRadius * d,
177 Paint(color, PaintingStyle.Stroke, 2.0 * d)
178 )
179 else -> {
180 val line =
181 if (plan.input.orientation == Orientation.Horizontal)
182 ChartGrid(
183 Point(geometry.plot.x, sample.point.y),
184 Point(geometry.plot.x + geometry.plot.width, sample.point.y)
185 )
186 else
187 ChartGrid(
188 Point(sample.point.x, geometry.plot.y),
189 Point(sample.point.x, geometry.plot.y + geometry.plot.height)
190 )
191 canvas.drawLine(
192 line.start,
193 line.end,
194 Paint(plan.appearance.theme.tokens.palette.border, strokeWidth = d)
195 )
196 canvas.drawCircle(
197 sample.point,
198 lowerBound(plan.input.pointRadius, 4.0) * d,
199 Paint(plan.appearance.theme.tokens.palette.background.background)
200 )
201 canvas.drawCircle(
202 sample.point,
203 lowerBound(plan.input.pointRadius, 4.0) * d,
204 Paint(color, PaintingStyle.Stroke, 2.0 * d)
205 )
206 }
207 }
208 }
209 }
210 }