This data is of 1,000 most popular movies on IMDB in the last 10 years. .
This data set contains 1,000 most popular movies on IMDB in the last 10 years. Inspired by Stylized Scatter Plot with Color Legend. forked from curran's block: Stylized Scatter Plot with Color Legend
To experience the resize behavior, run this example full-screen and resize the browser.
forked from curran's block: Responding to Resize
xxxxxxxxxx
<html>
<head>
<meta charset="utf-8">
<title>Dynamic Size Example</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.24.0/d3-legend.min.js"></script>
<style>
/* Make the chart container fill the page using CSS. */
#chart {
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
}
</style>
</head>
<body>
<div id="chart"></div>
<script>
var chartDiv = document.getElementById("chart");
var svg = d3.select(chartDiv).append("svg");
const xValue = d => +d.Rating;
const xLabel = 'Rating';
const yValue = d => +d.Votes;
const zValue=d=>+d.Runtime;
const yLabel = 'Votes';
const colorValue = d => d.Year;
const colorLabel = 'Year';
const margin = { left: 160, right: 200, top: 60, bottom: 80 };
const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
const xAxisG = g.append('g');
const yAxisG = g.append('g');
const colorLegendG = g.append('g');
const xScale = d3.scaleLinear();
const yScale = d3.scaleLinear();
const colorScale = d3.scaleOrdinal()
.range(d3.schemeCategory10);
var x_label = g.append('text')
.attr('class', 'axis-label');
var y_label = g.append('text')
.attr('class', 'axis-label');
var color_legend = colorLegendG.append('text')
.attr('class', 'legend-label')
function redraw(){
// Extract the width and height that was computed by CSS.
var width = chartDiv.clientWidth;
var height = chartDiv.clientHeight;
// Use the extracted size to set the size of an SVG element.
svg
.attr("width", width)
.attr("height", height);
// Draw an X to show that the size is correct.
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
xAxisG
.attr('transform', `translate(0, ${innerHeight})`);
colorLegendG
.attr('transform', `translate(${innerWidth + 60}, 32)`);
x_label
.attr('class', 'axis-label')
.attr('x', innerWidth / 2)
.attr('y', innerHeight+40)
.text(xLabel);
y_label
.attr('class', 'axis-label')
.attr('x', -innerHeight / 2)
.attr('y', -innerWidth/8)
.attr('transform', `rotate(-90)`)
.style('text-anchor', 'middle')
.text(yLabel);
color_legend
.attr('x', 3)
.attr('y', -20)
.text(colorLabel);
const xAxis = d3.axisBottom()
.scale(xScale)
.tickPadding(15)
.tickSize(-innerHeight);
const yAxis = d3.axisLeft()
.scale(yScale)
.ticks(15)
.tickPadding(15)
.tickSize(-innerWidth);
const colorLegend = d3.legendColor()
.scale(colorScale)
.shape('square');
d3.csv('data.csv', data => {
xScale
.domain(d3.extent(data, xValue))
.range([0, innerWidth])
.nice();
yScale
.domain(d3.extent(data, yValue))
.range([innerHeight, 0])
.nice();
var circles = d3.select('g').selectAll('circle').data(data)
circles.exit().remove();
circles
.enter().append('circle')
.attr('fill', d => colorScale(colorValue(d)))
.merge(circles)
.attr('cx', d => xScale(xValue(d)))
.attr('cy', d => yScale(yValue(d)))
.attr('fill-opacity', 0.5)
.attr('r', d => zValue(d)/15);
xAxisG.call(xAxis);
yAxisG.call(yAxis);
colorLegendG.call(colorLegend)
.selectAll('.cell text');
})};
// Draw for the first time to initialize.
redraw();
// Redraw based on the new size whenever the browser window is resized.
window.addEventListener("resize", redraw);
</script>
</body>
</html>
https://d3js.org/d3.v4.min.js
https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.24.0/d3-legend.min.js