Spaces:
Running
Running
File size: 5,207 Bytes
fab03ee | 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 144 145 146 147 148 149 150 151 152 153 154 155 156 | class CustomTradingChart extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.candles = [];
this.currentPrice = 7500;
}
connectedCallback() {
this.render();
this.canvas = this.shadowRoot.getElementById('chart-canvas');
this.ctx = this.canvas.getContext('2d');
this.resizeCanvas();
window.addEventListener('resize', () => this.resizeCanvas());
this.draw();
}
resizeCanvas() {
if (!this.canvas) return;
const rect = this.getBoundingClientRect();
this.canvas.width = rect.width;
this.canvas.height = 400;
this.draw();
}
updateChart(candles, currentPrice) {
this.candles = candles;
this.currentPrice = currentPrice;
this.draw();
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
width: 100%;
}
.chart-container {
position: relative;
width: 100%;
height: 400px;
background: linear-gradient(180deg, rgba(15, 23, 42, 0.3) 0%, transparent 100%);
}
canvas {
width: 100%;
height: 100%;
}
.price-label {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
background: rgba(99, 102, 241, 0.9);
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
font-family: monospace;
pointer-events: none;
z-index: 10;
}
.grid-lines {
position: absolute;
inset: 0;
pointer-events: none;
}
</style>
<div class="chart-container">
<canvas id="chart-canvas"></canvas>
<div class="price-label" id="price-label">7500.00</div>
</div>
`;
}
draw() {
if (!this.ctx || !this.canvas) return;
const ctx = this.ctx;
const width = this.canvas.width;
const height = this.canvas.height;
// Clear canvas
ctx.clearRect(0, 0, width, height);
if (this.candles.length === 0) return;
// Calculate scales
const prices = this.candles.flatMap(c => [c.high, c.low]);
const minPrice = Math.min(...prices) * 0.999;
const maxPrice = Math.max(...prices) * 1.001;
const priceRange = maxPrice - minPrice;
const candleWidth = (width - 60) / this.candles.length * 0.8;
const spacing = (width - 60) / this.candles.length;
// Draw grid
ctx.strokeStyle = 'rgba(99, 102, 241, 0.1)';
ctx.lineWidth = 1;
for (let i = 0; i < 5; i++) {
const y = height - (height * (i / 4));
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(width, y);
ctx.stroke();
}
// Draw candles
this.candles.forEach((candle, i) => {
const x = i * spacing + 30;
const yOpen = height - ((candle.open - minPrice) / priceRange) * height;
const yClose = height - ((candle.close - minPrice) / priceRange) * height;
const yHigh = height - ((candle.high - minPrice) / priceRange) * height;
const yLow = height - ((candle.low - minPrice) / priceRange) * height;
const isRise = candle.close >= candle.open;
const color = isRise ? '#10b981' : '#f43f5e';
// Wick
ctx.strokeStyle = color;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x + candleWidth / 2, yHigh);
ctx.lineTo(x + candleWidth / 2, yLow);
ctx.stroke();
// Body
ctx.fillStyle = color;
const bodyHeight = Math.max(Math.abs(yClose - yOpen), 1);
const bodyY = Math.min(yOpen, yClose);
ctx.fillRect(x, bodyY, candleWidth, bodyHeight);
});
// Draw current price line
const currentY = height - ((this.currentPrice - minPrice) / priceRange) * height;
ctx.strokeStyle = '#6366f1';
ctx.lineWidth = 2;
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(0, currentY);
ctx.lineTo(width, currentY);
ctx.stroke();
ctx.setLineDash([]);
// Update price label
const label = this.shadowRoot.getElementById('price-label');
if (label) {
label.textContent = this.currentPrice.toFixed(2);
label.style.top = `${currentY}px`;
}
}
}
customElements.define('custom-trading-chart', CustomTradingChart); |