|
| 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 | +} |
0 commit comments