i want a line chart optimization for when the user is zoomed out far because when even…
- User
i want a line chart optimization for when the user is zoomed out far because when even if im just rendering the candle wicks it is still very laggy when multiple million are rendered at once. It is still vital to see all of the detail of the price action
import * as THREE from './three.module.js';
// Scene setup const scene = new THREE.Scene(); scene.background = new THREE.Color(0x000000);
// Camera setup const aspectRatio = window.innerWidth / window.innerHeight; const cameraSize = 200; const camera = new THREE.OrthographicCamera( -cameraSize * aspectRatio, cameraSize * aspectRatio, cameraSize, -cameraSize, 1, 1000 ); camera.position.set(0, 0, 500);
// Renderer setup const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
// Function to generate random OHLC data function generateOHLCData(count) { const data = new Array(count); let open = 50; // Starting price for (let i = 0; i < count; i++) { const delta = Math.random() * 20 - 10; // Random movement between -10 and +10 const close = open + delta; const high = Math.max(open, close) + Math.random() * 5; // Random overshoot up to 5 const low = Math.min(open, close) - Math.random() * 5; // Random undershoot up to 5 data[i] = { open, high, low, close }; open = close; // Next open starts where the last close was } return data; }
// Convert RGB to normalized color values (0 to 1) function rgbToNormalized(r, g, b) { return [r / 255, g / 255, b / 255]; }
// Arrays to store the instanced meshes and wicks const instancedMeshes = []; const wickLineSegmentsArray = [];
// Create instanced candlesticks with chunking function createInstancedCandlesticks(data, chunkSize) { const barWidth = 1;
// Materials const material = new THREE.MeshBasicMaterial({ color: 0xffffff, // Set base color to white vertexColors: THREE.InstanceColor // Use per-instance colors });
const wickMaterial = new THREE.LineBasicMaterial({ vertexColors: true }); // Enable vertex colors
// Geometry for the candlestick body const geometryBar = new THREE.BoxGeometry(barWidth, 1, 0); // Width, height, depth
// Define colors in normalized RGB const bullishColor = rgbToNormalized(0, 70, 0); // Custom RGB for bullish candles (dark green) const bearishColor = rgbToNormalized(138, 0, 0); // Custom RGB for bearish candles (dark red)
const dataLength = data.length; const numChunks = Math.ceil(dataLength / chunkSize);
let currentX = 0;
const dummy = new THREE.Object3D();
for (let chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) { const startIndex = chunkIndex * chunkSize; const endIndex = Math.min(startIndex + chunkSize, dataLength); const chunkData = data.slice(startIndex, endIndex);
code - Assistant