File size: 5,399 Bytes
5c335da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class CustomChart extends HTMLElement {
    constructor() {
        super();
    }

    connectedCallback() {
        this.attachShadow({ mode: 'open' });
        
        const type = this.getAttribute('type') || 'line';
        const data = (this.getAttribute('data') || '').split(',').map(Number);
        const labels = (this.getAttribute('labels') || '').split(',');
        
        const max = Math.max(...data);
        const min = Math.min(...data);
        const range = max - min || 1;
        
        const width = 600;
        const height = 200;
        const padding = 30;
        
        // Generate points for line chart
        const points = data.map((value, index) => {
            const x = padding + (index / (data.length - 1)) * (width - 2 * padding);
            const y = height - padding - ((value - min) / range) * (height - 2 * padding);
            return `${x},${y}`;
        }).join(' ');
        
        // Generate grid lines
        let gridLines = '';
        for (let i = 0; i <= 4; i++) {
            const y = padding + (i / 4) * (height - 2 * padding);
            gridLines += `<line x1="${padding}" y1="${y}" x2="${width - padding}" y2="${y}" class="chart-grid"/>`;
        }
        
        // Generate data points (circles)
        let pointCircles = '';
        data.forEach((value, index) => {
            const x = padding + (index / (data.length - 1)) * (width - 2 * padding);
            const y = height - padding - ((value - min) / range) * (height - 2 * padding);
            pointCircles += `<circle cx="${x}" cy="${y}" class="chart-point" data-value="${value}"/>`;
        });
        
        // Generate labels
        let xLabels = '';
        labels.forEach((label, index) => {
            const x = padding + (index / (labels.length - 1)) * (width - 2 * padding);
            xLabels += `<text x="${x}" y="${height - 8}" class="chart-label" text-anchor="middle">${label}</text>`;
        });
        
        // Generate y-axis labels
        let yLabels = '';
        for (let i = 0; i <= 4; i++) {
            const y = padding + (i / 4) * (height - 2 * padding);
            const value = Math.round(max - (i / 4) * range);
            yLabels += `<text x="${padding - 8}" y="${y + 4}" class="chart-label" text-anchor="end">${value}</text>`;
        }
        
        this.shadowRoot.innerHTML = `
            <style>
                :host {
                    display: block;
                    width: 100%;
                }
                svg {
                    width: 100%;
                    height: 100%;
                    overflow: visible;
                }
                .chart-line {
                    fill: none;
                    stroke: #22c55e;
                    stroke-width: 2;
                    stroke-linecap: round;
                    stroke-linejoin: round;
                    filter: drop-shadow(0 0 4px rgba(34, 197, 94, 0.5));
                }
                .chart-grid {
                    stroke: #1f2937;
                    stroke-width: 1;
                    stroke-dasharray: 4 4;
                }
                .chart-label {
                    fill: #6b7280;
                    font-size: 10px;
                    font-family: monospace;
                }
                .chart-point {
                    fill: #0a0a0a;
                    stroke: #22c55e;
                    stroke-width: 2;
                    r: 4;
                    transition: all 0.2s;
                    cursor: pointer;
                }
                .chart-point:hover {
                    r: 6;
                    fill: #22c55e;
                }
                .tooltip {
                    position: absolute;
                    background: #1f2937;
                    color: #fff;
                    padding: 4px 8px;
                    border-radius: 4px;
                    font-size: 12px;
                    pointer-events: none;
                    opacity: 0;
                    transition: opacity 0.2s;
                }
            </style>
            <div style="height: 200px; position: relative;">
                <svg viewBox="0 0 ${width} ${height}">
                    ${gridLines}
                    <polyline points="${points}" class="chart-line"/>
                    ${pointCircles}
                    ${xLabels}
                    ${yLabels}
                </svg>
            </div>
        `;
        
        // Add tooltips
        const points = this.shadowRoot.querySelectorAll('.chart-point');
        points.forEach(point => {
            point.addEventListener('mouseenter', (e) => {
                const tooltip = document.createElement('div');
                tooltip.className = 'tooltip';
                tooltip.textContent = `Value: ${e.target.dataset.value}`;
                tooltip.style.left = `${e.target.cx.baseVal.value + 10}px`;
                tooltip.style.top = `${e.target.cy.baseVal.value - 30}px`;
                this.shadowRoot.appendChild(tooltip);
                setTimeout(() => tooltip.style.opacity = '1', 10);
            });
            
            point.addEventListener('mouseleave', () => {
                const tooltip = this.shadowRoot.querySelector('.tooltip');
                if (tooltip) tooltip.remove();
            });
        });
    }
}

customElements.define('custom-chart', CustomChart);