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
| const margin = { top: 50, right: 50, bottom: 50, left: 50, } const width = window.innerWidth - margin.left - margin.right const height = window.innerHeight - margin.top - margin.bottom
const n = 21
const xScale = d3.scaleLinear() .domain([0, n-1]) .range([0, width])
const yScale = d3.scaleLinear() .domain([0, 1]) .range([height, 0])
const line = d3.line() .x(function(d, i) { return xScale(i) }) .y(function(d, i) { return d.y }) .curve(d3.curveMonotoneX)
const dataset = d3.range(n) .map(function(d) { return { y: d3.randomUniform(1)() } })
const svg = d3.select('body') .append('svg') .attr('width', width + margin.left + margin.right) .attr('height', height + margin.top + margin.bottom) .append('g') .attr('transform', `translate(${margin.left}, ${margin.top})`)
svg.append('g') .attr('class', 'x axis') .attr('transform', `translate(0, ${height})`) .call(d3.axisBottom(xScale))
svg.append('g') .attr('class', 'y axis') .call(d3.axisLeft(yScale))
svg.append('path') .datum(dataset) .attr('class', 'line') .attr('d', line)
svg.selectAll('.dot') .data(dataset) .enter().append('circle') .attr('cx', function (d, i) { return xScale(i) }) .attr('cy', function (d, i) { return yScale(dy) }) .attr('r', 5)
|