Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/js/legend_customization.ts
2313 views
1
import {
2
css,
3
html,
4
HTMLTemplateResult,
5
LitElement,
6
nothing,
7
TemplateResult,
8
} from "lit";
9
import { property } from "lit/decorators.js";
10
11
import { legacyStyles } from "./ipywidgets_styles";
12
import { SelectOption } from "./utils";
13
import { flexStyles } from "./styles";
14
15
enum LegendType {
16
Linear = "linear",
17
Step = "step",
18
}
19
20
export class LegendCustomization extends LitElement {
21
static get componentName() {
22
return `legend-customization`;
23
}
24
25
static override styles = [
26
flexStyles,
27
legacyStyles,
28
css`
29
.hidden {
30
display: none;
31
}
32
33
.legend-checkbox {
34
vertical-align: middle;
35
}
36
`,
37
];
38
39
@property({ type: Array }) legendTypes: Array<SelectOption> = [
40
{ label: "Linear", value: LegendType.Linear },
41
{ label: "Step", value: LegendType.Step },
42
];
43
44
@property({ type: Boolean }) showLegend: boolean = false;
45
@property({ type: String }) legendType: string = "linear";
46
@property({ type: String }) override title: string = "Legend";
47
@property({ type: String }) labels: Array<string> = [];
48
49
override render() {
50
return html`
51
<div class="vertical-flex">
52
<div class="horizontal-flex">
53
<span>
54
<input
55
type="checkbox"
56
class="legend-checkbox"
57
.checked="${this.showLegend}"
58
@change="${this.showLegendToggleChanged}"
59
/>
60
<span class="legacy-text">Legend</span>
61
</span>
62
</div>
63
${this.renderLegendContents()}
64
</div>
65
`;
66
}
67
68
getLegendData(): void | undefined {
69
if (this.showLegend) {
70
const data: any = {type: this.legendType};
71
if (this.legendType === LegendType.Step) {
72
data.title = this.title;
73
data.labels = this.labels;
74
}
75
return data;
76
}
77
return undefined;
78
}
79
80
private renderLegendTypeRadio(option: SelectOption): TemplateResult {
81
return html`
82
<span>
83
<input
84
type="radio"
85
class="legacy-radio"
86
id="${option.value}"
87
name="legend-type"
88
value="${option.value}"
89
@click="${this.onLegendTypeChanged}"
90
?checked="${this.legendType === option.value}"
91
/>
92
<label class="legacy-text">${option.label}</label>
93
</span>
94
`;
95
}
96
97
private renderLegendContents(): TemplateResult | typeof nothing {
98
if (this.showLegend) {
99
return html`
100
<div class="horizontal-flex">
101
${this.legendTypes.map((model) =>
102
this.renderLegendTypeRadio(model)
103
)}
104
</div>
105
${this.renderLegendTitleAndLabels()}
106
`;
107
}
108
return nothing;
109
}
110
111
private renderLegendTitleAndLabels(): HTMLTemplateResult | typeof nothing {
112
if (this.legendType === LegendType.Step) {
113
return html`
114
<div class="horizontal-flex">
115
<span class="legacy-text">Legend title:</span>
116
<input
117
type="text"
118
class="legacy-text-input"
119
id="labels"
120
name="labels"
121
.value="${this.title}"
122
@change="${this.onTitleChanged}"
123
/>
124
</div>
125
<div class="horizontal-flex">
126
<span class="legacy-text">Legend labels:</span>
127
<input
128
type="text"
129
class="legacy-text-input"
130
id="labels"
131
name="labels"
132
.value="${this.labels.join(", ")}"
133
@change="${this.onLabelsChanged}"
134
/>
135
</div>
136
`;
137
}
138
return nothing;
139
}
140
141
private showLegendToggleChanged(event: Event): void {
142
this.showLegend = (event.target as HTMLInputElement).checked;
143
}
144
145
private onLegendTypeChanged(event: Event): void {
146
this.legendType = (event.target as HTMLInputElement).value;
147
}
148
149
private onTitleChanged(event: Event): void {
150
this.title = (event.target as HTMLInputElement).value;
151
}
152
153
private onLabelsChanged(event: Event): void {
154
const labels = (event.target as HTMLInputElement).value;
155
this.labels = labels.split(",").map(token => token.trim());
156
}
157
}
158
159
// Without this check, there's a component registry issue when developing locally.
160
if (!customElements.get(LegendCustomization.componentName)) {
161
customElements.define(
162
LegendCustomization.componentName,
163
LegendCustomization
164
);
165
}
166
167