Skip to content

Commit 7ee16a8

Browse files
committed
Added example pie chart
1 parent f67f532 commit 7ee16a8

File tree

7 files changed

+186
-10
lines changed

7 files changed

+186
-10
lines changed

npm/helloworld/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"bootstrap-icons": "^1.13.1",
13+
"echarts": "^5.6.0",
1314
"lit": "^3.3.0",
1415
"tslib": "^2.5.2"
1516
},

npm/helloworld/src/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@
6060
<wc-button color="error">Error</wc-button>
6161
<wc-button disabled>Disabled</wc-button>
6262
</wc-content>
63-
<wc-content>
64-
BOTTOM CONTENT
63+
<wc-content style="border: 1px solid red">
64+
<wc-piechart></wc-piechart>
6565
</wc-content>
6666
</wc-content>
6767

6868
</wc-content>
6969

7070
</wc-canvas>
7171

72-
<wc-toast visible>HELLO</wc-toast>
72+
<!-- Place a toast at the bottom of the page -->
73+
<wc-toast></wc-toast>
7374
</body>
7475

7576
</html>

npm/helloworld/src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import './wc/core/GoogleAuth'
88

99
// Import classes
1010
import { GoogleAuth } from './wc/core/GoogleAuth';
11+
import { Toast } from './wc/layout/Toast';
12+
import { Button } from './wc/layout/Button'
1113

1214
// Initialize the app
1315
document.addEventListener("DOMContentLoaded", () => {
@@ -17,4 +19,17 @@ document.addEventListener("DOMContentLoaded", () => {
1719
Theme: 'outline',
1820
Size: 'small'
1921
};
22+
23+
24+
const toast = document.querySelector('wc-toast') as Toast;
25+
const buttons = document.querySelectorAll('wc-button');
26+
buttons.forEach((button) => {
27+
button.addEventListener('click', (evt) => {
28+
const button = evt.target as Button;
29+
toast.show(`Button clicked ${ button.textContent }`,{
30+
duration: 3000,
31+
color: button.color
32+
});
33+
});
34+
});
2035
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as echarts from 'echarts/core';
2+
import { PieChart as ePieChart } from 'echarts/charts';
3+
import { CanvasRenderer } from 'echarts/renderers';
4+
import { LitElement, html, css, nothing, PropertyValues } from 'lit';
5+
import { customElement, property } from 'lit/decorators.js';
6+
7+
// Register the required components
8+
echarts.use([
9+
ePieChart,
10+
CanvasRenderer
11+
]);
12+
13+
/**
14+
* @class PieChart
15+
*
16+
* This class is a renders a pie chart, within a canvas or content element.
17+
*
18+
* @example
19+
* <wc-canvas vertical>
20+
* <wc-nav>....</wc-nav>
21+
* <wc-piechart>....</wc-piechart>
22+
* </wc-canvas>
23+
*/
24+
@customElement('wc-piechart')
25+
export class PieChart extends LitElement {
26+
#chart: echarts.ECharts | null = null;
27+
#node: HTMLDivElement | null = null;
28+
29+
protected firstUpdated(): void {
30+
this.#chart = echarts.init(this.shadowRoot.querySelector('div'));
31+
32+
// Set options for the chart
33+
this.#chart.setOption({
34+
series: [
35+
{
36+
type: 'pie',
37+
label: {
38+
"fontFamily": "IBM Plex Sans",
39+
},
40+
data: [
41+
{
42+
value: 335,
43+
name: 'Direct Visit'
44+
},
45+
{
46+
value: 234,
47+
name: 'Union Ad'
48+
},
49+
{
50+
value: 1548,
51+
name: 'Search Engine'
52+
}
53+
]
54+
}
55+
]
56+
});
57+
58+
// Set to resize the chart when the window is resized
59+
window.addEventListener('resize', function () {
60+
this.#chart.resize();
61+
}.bind(this));
62+
}
63+
64+
65+
static get styles() {
66+
return css`
67+
:host {
68+
flex: 1 0;
69+
}
70+
71+
div {
72+
display: flex;
73+
flex: 1 0;
74+
height: 100%;
75+
}
76+
`;
77+
}
78+
79+
render() {
80+
return html`
81+
<div class=${this.className || nothing}>${this.#node}</div>
82+
`;
83+
}
84+
85+
get className() {
86+
const classes = [];
87+
return classes.join(' ');
88+
}
89+
}

npm/helloworld/src/wc/layout/Toast.ts

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { LitElement, html, css, nothing } from 'lit';
22
import { customElement, property } from 'lit/decorators.js';
33

4+
interface ShowOptions {
5+
/** The duration the toast is displayed for, in milliseconds. */
6+
duration?: number;
7+
8+
/** The color of the button. */
9+
color?: string;
10+
}
11+
412
/**
513
* @class Toast
614
*
@@ -13,7 +21,9 @@ import { customElement, property } from 'lit/decorators.js';
1321
@customElement('wc-toast')
1422
export class Toast extends LitElement {
1523
@property({ type: Boolean }) visible: boolean = false;
24+
@property({ type: Number }) duration: number = 3000;
1625
@property({ type: String }) color?: string;
26+
#timer: number | null = null;
1727

1828
render() {
1929
return html`
@@ -23,34 +33,88 @@ export class Toast extends LitElement {
2333
`;
2434
}
2535

36+
show(message: string, opts = {} as ShowOptions) {
37+
this.visible = true;
38+
this.textContent = message;
39+
this.color = opts.color || 'primary';
40+
41+
// Cancel any existing timer
42+
if (this.#timer) {
43+
clearTimeout(this.#timer);
44+
}
45+
// Set a new timer
46+
this.#timer = setTimeout(() => {
47+
this.visible = false;
48+
this.#timer = null;
49+
}, opts.duration || this.duration);
50+
}
51+
2652
static get styles() {
2753
return css`
2854
:host {
2955
position: fixed;
3056
z-index: 1000;
3157
right: 0;
3258
bottom: 0;
33-
34-
margin: var(--toast-margin);
35-
background-color: var(--toast-background-color);
36-
text-color: var(--toast-text-color);
3759
}
3860
3961
:host div {
62+
display: inline-flex;
63+
margin: var(--toast-margin);
64+
padding: var(--toast-padding);
65+
border: var(--toast-border);
66+
border-radius: var(--toast-border-radius);
4067
transition: visibility 0.2s, opacity 0.2s ease-in-out;
68+
cursor: pointer;
69+
user-select: none;
4170
4271
&.visible {
43-
display: block;
4472
visibility: visible;
4573
opacity: 1;
4674
}
4775
4876
&:not(.visible) {
49-
display: none;
5077
visibility: hidden;
5178
opacity: 0;
5279
}
5380
}
81+
82+
div.color-primary {
83+
background-color: var(--primary-color);
84+
color: var(--primary-opp-color);
85+
}
86+
div.color-secondary {
87+
background-color: var(--secondary-color);
88+
color: var(--secondary-opp-color);
89+
}
90+
div.color-light {
91+
background-color: var(--light-color);
92+
color: var(--light-opp-color);
93+
}
94+
div.color-dark {
95+
background-color: var(--dark-color);
96+
color: var(--dark-opp-color);
97+
}
98+
div.color-white {
99+
background-color: var(--white-color);
100+
color: var(--white-opp-color);
101+
}
102+
div.color-black {
103+
background-color: var(--black-color);
104+
color: var(--black-opp-color);
105+
}
106+
div.color-success {
107+
background-color: var(--success-color);
108+
color: var(--success-opp-color);
109+
}
110+
div.color-warning {
111+
background-color: var(--warning-color);
112+
color: var(--warning-opp-color);
113+
}
114+
div.color-error {
115+
background-color: var(--error-color);
116+
color: var(--error-opp-color);
117+
}
54118
`;
55119
}
56120

npm/helloworld/src/wc/root.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ import './layout/Nav'
1111
import './layout/NavItem'
1212
import './layout/NavSpace'
1313
import './layout/Toast'
14+
15+
// Charts
16+
import './charts/PieChart'

npm/helloworld/src/wc/tokens.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ body {
109109

110110
/* toast */
111111
:root {
112+
--toast-margin: calc(var(--space-small) * 2) var(--space-small);
113+
--toast-padding: var(--space-default) var(--space-default);
114+
--toast-border: calc(var(--space-small) / 4) solid light-dark(var(--grey-50-color),var(--grey-50-color));
115+
--toast-border-radius: calc(var(--space-small) / 2);
112116
--toast-background-color: light-dark(var(--grey-20-color),var(--grey-80-color));
113117
--toast-text-color: light-dark(var(--light-color),var(--dark-color));
114-
--toast-margin: var(--space-default);
115118
}

0 commit comments

Comments
 (0)