Libraryless. Click here for Pure Java version (11599L/65K).
1 | // background color is set with setBackground |
2 | sclass JMultiColorBar > JComponentWithChangeListeners {
|
3 | class Entry {
|
4 | // absolute value from the left! |
5 | settable double value; |
6 | |
7 | settable Color color; |
8 | settable S text; |
9 | settable S toolTip; |
10 | |
11 | Entry previous() {
|
12 | ret elementBefore(entries, this); |
13 | } |
14 | |
15 | double relativeValue() {
|
16 | Entry p = previous(); |
17 | ret p == null ? value : value-p.value; |
18 | } |
19 | } |
20 | |
21 | L<Entry> entries = notifyingList(l0 repaint); |
22 | |
23 | settableWithVar double max = 100; |
24 | |
25 | settable L<Color> defaultColors = ll(Color.red, Color.blue); |
26 | |
27 | InstantNeverHideToolTip toolTipMaker; |
28 | |
29 | swappable Color defaultColor(int index) {
|
30 | ret getCyclic(defaultColors, index); |
31 | } |
32 | |
33 | Entry makeEntry(double relativeValue) {
|
34 | Entry prev = last(entries); |
35 | double value = prev == null ? relativeValue : relativeValue+prev.value; |
36 | ret new Entry().+value.color(defaultColor(l(entries))); |
37 | } |
38 | |
39 | Entry addEntry aka add(S text, double relativeValue) {
|
40 | ret addAndReturn(entries, makeEntry(relativeValue).+text); |
41 | } |
42 | |
43 | Entry addEntry aka add(double relativeValue, Color color) {
|
44 | ret addAndReturn(entries, makeEntry(relativeValue).+color); |
45 | } |
46 | |
47 | S toolTipForEntry(Entry e) {
|
48 | ret e == null ?: spaceCombine(or2(e.toolTip, e.text), renderValue(e.relativeValue())); |
49 | } |
50 | |
51 | swappable S renderValue(double value) { ret str(value); }
|
52 | |
53 | scaffolded S toolTipForPosition(Pt p) {
|
54 | int w = getWidth(); |
55 | Entry e = firstThat(entries, _e -> p.x < xForEntry(_e, w)); |
56 | if (scaffolded()) printVars(+e, xs := map(entries, _e -> xForEntry(_e, w))); |
57 | ret toolTipForEntry(e); |
58 | } |
59 | |
60 | *() {
|
61 | varMax().onChange(l0 repaint); |
62 | jMinSize(50, 10, this); |
63 | toolTipMaker = new InstantNeverHideToolTip(l1 toolTipForPosition, this); |
64 | } |
65 | |
66 | int xForEntry(Entry e, int w) {
|
67 | ret iround(w*clampZeroToOne(doubleRatio(e.value, max))); |
68 | } |
69 | |
70 | public void paintComponent(Graphics g) {
|
71 | super.paintComponent(g); // paint background |
72 | int w = getWidth(), h = getHeight(); |
73 | |
74 | int x = 0; |
75 | for (e : entries) {
|
76 | int x2 = main max(x, xForEntry(e, w)); |
77 | if (x2 > x) |
78 | fillRect(g, x+1, 0, x2-x, h, e.color); |
79 | x = x2; |
80 | } |
81 | } |
82 | } |
Began life as a copy of #1035303
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1036081 |
| Snippet name: | JMultiColorBar [OK] |
| Eternal ID of this version: | #1036081/25 |
| Text MD5: | ff407a45632a0efd9ba02486dc0cea3d |
| Transpilation MD5: | 70ef3a5060e5847514c904e550f86226 |
| Author: | stefan |
| Category: | javax / gui |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-09-07 07:45:06 |
| Source code size: | 2304 bytes / 82 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 631 / 861 |
| Version history: | 24 change(s) |
| Referenced in: | [show references] |