D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
areologist
Full window
Github gist
stock data (line)
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } svg { border: 1px solid gray; } </style> </head> <body> <script> const margin = { top: 20, right: 30, bottom: 40, left: 40, }; const fullWidth = 560; const fullHeight = 500; const width = fullWidth - margin.right - margin.left; const height = fullHeight - margin.top - margin.bottom; const svg = d3.select("body").append("svg") .attr("width", fullWidth) .attr("height", fullHeight) .append('g') .attr('transform', `translate(${margin.left}, ${margin.top})`); const parseTime = d3.timeParse('%Y/%m/%d'); let yScale, xScale; d3.json('data.json', (err, data) => { data.forEach(ticker => { ticker.values.forEach(d => { d.date = parseTime(d.date); d.close = parseFloat(d.close); }) }); setup(data); }); function setup(data) { // build y scale and axis let min = d3.min(data, ticker => d3.min(ticker.values, d => d.close)); let max = d3.max(data, ticker => d3.max(ticker.values, d => d.close)); yScale = d3.scaleLinear() .domain([min, max]) .range([height, 0]); const yAxis = d3.axisLeft(yScale); svg.append('g') .call(yAxis); // build the x scale and axis min = d3.min(data, ticker => d3.min(ticker.values, d => d.date)); max = d3.max(data, ticker => d3.max(ticker.values, d => d.date)); xScale = d3.scaleTime() .domain([min, max]) .range([0, width]); const xAxis = d3.axisBottom(xScale) .ticks(6); svg.append('g') .attr('transform', `translate(0, ${height})`) .call(xAxis); update(data); } function update(data) { const line = d3.line() .x(d => xScale(d.date)) .y(d => yScale(d.close)) .curve(d3.curveCatmullRom.alpha(0.5)); const colors = ['#ff9900', '#3369e8']; svg.selectAll('.line') .data(data) .enter() .append('path') .attr('class', 'line') .attr('d', d => line(d.values)) .style('stroke-width', 2) .style('stroke', (d, i) => colors[i]) .style('fill', 'none'); } </script> </body>
https://d3js.org/d3.v4.min.js