Skip to content

Commit f1dfb4b

Browse files
committed
feat: project init
0 parents  commit f1dfb4b

File tree

15 files changed

+11290
-0
lines changed

15 files changed

+11290
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
npm-debug.log
2+
.DS_Store
3+
.cache
4+
node_modules/
5+
dist/

.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.gitignore
2+
.prettierrc
3+
.prettierignore
4+
.npmignore
5+
.prettierrc
6+
package-lock.json
7+
tsconfig.json
8+
lib/
9+
node_modules/

.prettierignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.cache
2+
package.json
3+
package-lock.json
4+
dist/
5+
node_modules/

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"endOfLine": "lf",
3+
"semi": false,
4+
"singleQuote": false,
5+
"tabWidth": 2,
6+
"trailingComma": "es5"
7+
}

LICENSE.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#### License
2+
3+
[The MIT License (MIT)](https://betomuniz.mit-license.org/)
4+
5+
Copyright (c) 2023 Beto Muniz (http://betomuniz.com/)
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# chrts
2+
3+
A simple chart web components library using SVG, CSS, and TypeScript.
4+
5+
## Installation
6+
7+
```bash
8+
npm install chrts
9+
```
10+
11+
## Usage
12+
13+
First, import the library in your JavaScript or TypeScript file:
14+
15+
```typescript
16+
import { ChrtsPie, ChrtsBars, ChrtsGauge, ChrtsSegment } from "chrts"
17+
```
18+
19+
Then, use the components in your HTML:
20+
21+
### Pie Chart
22+
23+
```html
24+
<chrts-pie>
25+
<chrts-segment color="red" label="Red" value="120"></chrts-segment>
26+
<chrts-segment color="blue" label="Blue" value="80"></chrts-segment>
27+
</chrts-pie>
28+
```
29+
30+
### Bar Chart
31+
32+
```html
33+
<chrts-bars>
34+
<chrts-segment color="green" label="Green" value="50"></chrts-segment>
35+
<chrts-segment color="purple" label="Purple" value="100"></chrts-segment>
36+
</chrts-bars>
37+
```
38+
39+
Each chart component uses the `<chrts-segment>` child component to define its data segments. The `<chrts-segment>` component has three attributes:
40+
41+
- `color`: The color of the segment.
42+
- `label`: The label of the segment.
43+
- `value`: The numerical value of the segment.

docs/examples/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>chrts Example</title>
7+
<style>
8+
chrts-pie,
9+
chrts-bars,
10+
chrts-gauge {
11+
display: inline-block;
12+
width: 200px;
13+
height: 200px;
14+
}
15+
</style>
16+
</head>
17+
<body>
18+
<chrts-pie>
19+
<chrts-segment color="red" label="Red" value="120"></chrts-segment>
20+
<chrts-segment color="blue" label="Blue" value="80"></chrts-segment>
21+
</chrts-pie>
22+
<chrts-bars>
23+
<chrts-segment color="green" label="Green" value="50"></chrts-segment>
24+
<chrts-segment color="purple" label="Purple" value="100"></chrts-segment>
25+
<chrts-segment color="#ff0" label="Yellow" value="250"></chrts-segment>
26+
</chrts-bars>
27+
28+
<script type="module">
29+
import "../../dist/charts/pie.js"
30+
import "../../dist/charts/bars.js"
31+
// import {
32+
// ChrtsPie,
33+
// ChrtsBars,
34+
// ChrtsGauge,
35+
// ChrtsSegment,
36+
// } from "../../dist/index.js"
37+
</script>
38+
</body>
39+
</html>

lib/charts/bars.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class ChrtsBars extends HTMLElement {
2+
constructor() {
3+
super()
4+
}
5+
6+
connectedCallback() {
7+
this.render()
8+
}
9+
10+
render() {
11+
const segments = Array.from(this.querySelectorAll("chrts-segment"))
12+
const totalValue = segments.reduce(
13+
(sum, segment) => sum + parseFloat(segment.getAttribute("value") || "0"),
14+
0
15+
)
16+
const barWidth = 100 / segments.length
17+
18+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
19+
svg.setAttribute("viewBox", "0 0 100 100")
20+
this.appendChild(svg)
21+
22+
segments.forEach((segment, index) => {
23+
const value = parseFloat(segment.getAttribute("value") || "0")
24+
const barHeight = (value / totalValue) * 100
25+
26+
const rect = document.createElementNS(
27+
"http://www.w3.org/2000/svg",
28+
"rect"
29+
)
30+
rect.setAttribute("x", `${index * barWidth}`)
31+
rect.setAttribute("y", `${100 - barHeight}`)
32+
rect.setAttribute("width", `${barWidth}`)
33+
rect.setAttribute("height", `${barHeight}`)
34+
rect.setAttribute("fill", segment.getAttribute("color") || "black")
35+
36+
svg.appendChild(rect)
37+
})
38+
}
39+
}
40+
41+
customElements.define("chrts-bars", ChrtsBars)
42+
export default ChrtsBars

lib/charts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as ChrtsBars } from "./bars"
2+
export { default as ChrtsPie } from "./pie"

lib/charts/pie.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class ChrtsPie extends HTMLElement {
2+
constructor() {
3+
super()
4+
}
5+
6+
connectedCallback() {
7+
const segments = Array.from(this.querySelectorAll("chrts-segment"))
8+
9+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
10+
svg.setAttribute("viewBox", "0 0 100 100")
11+
this.appendChild(svg)
12+
13+
const totalValue = segments.reduce(
14+
(sum, segment) => sum + parseFloat(segment.getAttribute("value") || "0"),
15+
0
16+
)
17+
let accumulatedAngle = 0
18+
19+
segments.forEach((segment) => {
20+
const value = parseFloat(segment.getAttribute("value") || "0")
21+
const segmentAngle = (value / totalValue) * 360
22+
23+
const startAngle = accumulatedAngle - 90
24+
const endAngle = accumulatedAngle + segmentAngle - 90
25+
26+
const largeArcFlag = segmentAngle > 180 ? 1 : 0
27+
28+
const x1 = 50 + 50 * Math.cos(startAngle * (Math.PI / 180))
29+
const y1 = 50 + 50 * Math.sin(startAngle * (Math.PI / 180))
30+
const x2 = 50 + 50 * Math.cos(endAngle * (Math.PI / 180))
31+
const y2 = 50 + 50 * Math.sin(endAngle * (Math.PI / 180))
32+
33+
const pathData = `
34+
M50,50
35+
L${x1},${y1}
36+
A50,50 0 ${largeArcFlag},1 ${x2},${y2}
37+
Z
38+
`
39+
40+
const path = document.createElementNS(
41+
"http://www.w3.org/2000/svg",
42+
"path"
43+
)
44+
path.setAttribute("d", pathData)
45+
path.setAttribute("fill", segment.getAttribute("color") || "black")
46+
47+
svg.appendChild(path)
48+
49+
accumulatedAngle += segmentAngle
50+
})
51+
}
52+
}
53+
54+
customElements.define("chrts-pie", ChrtsPie)
55+
export default ChrtsPie

0 commit comments

Comments
 (0)