The Concept:
My project is centered around the visualization of stock market data, specifically designed to help users visually track the fluctuations in stock prices over time. The idea was to create an interactive graph that could display a moving window of stock prices, allowing users to see trends and changes as they happen.
A Highlight of the Code:
One snippet of code that stands out to me for its simplicity and effectiveness is the drawGraph function. This function renders the graph on the screen, mapping the stock prices to visual elements that can be easily understood.
function drawGraph(startIndex) { const graphWidth = 700; const graphHeight = 300; const startY = height - graphHeight - 50; const endY = startY + graphHeight; const startX = (width - graphWidth) / 2; const endX = startX + graphWidth; //Calculate the subset of prices to display let displayedPrices = prices.slice(startIndex, Math.min(startIndex + windowSize, prices.length)); const minValue = Math.min(...displayedPrices); const maxValue = Math.max(...displayedPrices); const latestPrice = displayedPrices[displayedPrices.length - 1]; // The last price in the displayed subset push(); stroke(0); noFill(); rect(startX - 10, startY - 10, graphWidth + 20, graphHeight + 20); pop(); push(); stroke(25, 118, 210); strokeWeight(2); noFill(); beginShape(); for (let i = 0; i < displayedPrices.length; i++) { let x = map(i, 0, displayedPrices.length - 1, startX, endX); let y = map(displayedPrices[i], minValue, maxValue, endY, startY); vertex(x, y); } endShape();
Embedded Sketch:
Showcasing the dynamic visualization of stock prices over time
Code:
let stockData; let currentIndex = 0; let windowSize = 15; //Data points to display let prices = []; function preload() { stockData = loadTable('stock_prices.csv', 'csv', 'header'); } function setup() { createCanvas(800, 400); frameRate(1); // Framerate loadPrices(); // Load data } function draw() { background(255); drawGraph(currentIndex); currentIndex++; // Move the window forward if (currentIndex > prices.length - windowSize) currentIndex = 0; // Loop back if we reach the end } function loadPrices() { //Extract prices and store it in array for (let r = 0; r < stockData.getRowCount(); r++) { let price = stockData.getNum(r, 'price'); prices.push(price); } } function drawGraph(startIndex) { const graphWidth = 700; const graphHeight = 300; const startY = height - graphHeight - 50; const endY = startY + graphHeight; const startX = (width - graphWidth) / 2; const endX = startX + graphWidth; //Calculate the subset of prices to display let displayedPrices = prices.slice(startIndex, Math.min(startIndex + windowSize, prices.length)); const minValue = Math.min(...displayedPrices); const maxValue = Math.max(...displayedPrices); const latestPrice = displayedPrices[displayedPrices.length - 1]; // The last price in the displayed subset push(); stroke(0); noFill(); rect(startX - 10, startY - 10, graphWidth + 20, graphHeight + 20); pop(); push(); stroke(25, 118, 210); strokeWeight(2); noFill(); beginShape(); for (let i = 0; i < displayedPrices.length; i++) { let x = map(i, 0, displayedPrices.length - 1, startX, endX); let y = map(displayedPrices[i], minValue, maxValue, endY, startY); vertex(x, y); } endShape(); pop(); push(); textSize(17); fill(0); noStroke(); textAlign(CENTER); text(`Current Price: $${latestPrice.toFixed(2)}`, width / 2, startY - 25); pop(); }
Reflection and Future Work:
This project has allowed me to refine my skills in processing and presenting data in an informative and aesthetically pleasing way. The feedback loop provided by visually tracking the effectiveness of the code in real time has been incredibly rewarding.
Moving forward, I see several avenues for improvement and expansion. For instance, integrating real-time data feeds to allow the graph to update with live market data would significantly enhance its utility. Additionally, incorporating user input to select different stocks or adjust the window size and time frame could make the tool more versatile and user-friendly.