Skip to content

Commit 02c4f98

Browse files
First commit.
0 parents  commit 02c4f98

File tree

7 files changed

+183
-0
lines changed

7 files changed

+183
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = tab
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.gitignore

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

index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script type="module" src="index.js" defer></script>
5+
</head>
6+
<body>
7+
<h1>Test</h1>
8+
<ternary-plot side="400"></ternary-plot>
9+
</body>
10+
</html>

index.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { html, define } from 'https://unpkg.com/hybrids/src';
2+
import style from './style.js';
3+
4+
const margin = {
5+
get: () => ({top: 10, right: 10, bottom: 10, left: 10}),
6+
}
7+
8+
export const TernaryPlot = {
9+
side: 400,
10+
margin,
11+
width: {
12+
get: ({side, margin}) => side - margin.left - margin.right,
13+
},
14+
height: {
15+
get: ({side, margin}) => side - margin.top - margin.bottom,
16+
},
17+
radius: {
18+
get: ({side}) => {
19+
const scaleFactor = 1 / 2;
20+
return side * scaleFactor;
21+
},
22+
},
23+
shift: {
24+
get: ({radius}) => {
25+
// Eye-adjusted vertical shift
26+
return 0.8 * (radius * (1 - Math.sin(Math.PI / 6))) / 2;
27+
}
28+
},
29+
globalTransform: {
30+
get: ({margin, shift}) => `translate(${margin.left}, ${margin.top + shift})`,
31+
},
32+
render
33+
};
34+
35+
function render ({ globalTransform }) {
36+
return html`
37+
${style}
38+
<svg width=400 height=400>
39+
<g transform="${globalTransform}">
40+
<polygon class="border" points="${getBorderPoints()}"/>
41+
</g>
42+
</svg>
43+
`;
44+
}
45+
46+
define('ternary-plot', TernaryPlot);
47+
48+
const margin1 = {top: 10, right: 10, bottom: 10, left: 10};
49+
const width = 400 - margin1.left - margin1.right;
50+
const height = 400 - margin1.top - margin1.bottom;
51+
const numberOfTicks = 10;
52+
const tickLenght = 10;
53+
54+
function getCenter() {
55+
return [width / 2, height / 2];
56+
}
57+
58+
function getSide() {
59+
return Math.min(width, height);
60+
}
61+
62+
function getRadius() {
63+
const side = getSide();
64+
const scaleFactor = 1 / 2;
65+
return side * scaleFactor;
66+
}
67+
68+
function getVertices() {
69+
const radius = getRadius();
70+
const center = getCenter();
71+
72+
// Vertices are sorted as [right, bottom, left].
73+
return [0, Math.PI * (2 / 3), Math.PI * (4 / 3)]
74+
.map(angle => [
75+
center[0] + (radius * Math.sin(angle)),
76+
center[1] - (radius * Math.cos(angle)),
77+
]);
78+
}
79+
80+
function getBorderPoints() {
81+
const vertices = getVertices();
82+
83+
return `
84+
${vertices[0][0]},${vertices[0][1]}
85+
${vertices[1][0]},${vertices[1][1]}
86+
${vertices[2][0]},${vertices[2][1]}`;
87+
}

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "ternary-plot",
3+
"version": "1.0.0",
4+
"description": "web component of a ternary plot.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"ternary",
11+
"plot",
12+
"web",
13+
"component"
14+
],
15+
"author": "",
16+
"license": "ISC",
17+
"devDependencies": {
18+
"hybrids": "^4.0.2"
19+
}
20+
}

style.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { html } from 'https://unpkg.com/hybrids/src';
2+
3+
export default html`
4+
<style>
5+
6+
svg {
7+
border: 1px solid red;
8+
}
9+
10+
svg .border {
11+
stroke: #353839;
12+
stroke-width: 5px;
13+
fill: none;
14+
}
15+
16+
svg .axes line {
17+
fill: none;
18+
stroke: #eee;
19+
stroke-width: 1px;
20+
}
21+
22+
svg .ticks {
23+
fill: none;
24+
stroke: #353839;
25+
stroke-width: 2px;
26+
}
27+
28+
svg .labels text {
29+
text-anchor: middle;
30+
}
31+
32+
svg .titles text {
33+
font-size: 13px;
34+
font-weight: bold;
35+
text-anchor: middle;
36+
}
37+
38+
</style>
39+
`;

0 commit comments

Comments
 (0)