block by curran 56c0461b88f6f188557c0e6fa1812fe2

IMDB-Rate vs Facebook-Likes

Full Screen

This scatter plot shows how IMDB rate of a movie relates to Facebook likes.

The data is from Kaggle: IMDB 5000 Movie Dataset.

Built with blockbuilder.org

forked from BruceHenry‘s block: IMDB-Rate vs Facebook-Likes

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <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>
    <title>Basic Scatter Plot</title>
    <style>
      body {
        margin: 0px;
      }
      .domain {
        display: none;
      }
      .tick line {
        stroke: #b4d9b4;
      }
      .tick text{
        fill: #63c96b;
        font-size: 20pt;
        font-family: sans-serif;
      }
      .x-axis-label {
        fill: #ffcb0f;
        font-size: 20pt;
        font-family: sans-serif;
      }

      .y-axis-label {
        fill: #3b5998;
        font-size: 20pt;
        font-family: sans-serif;
      }
      .circle{
        fill: #4357e9;
      }
    </style>
  </head>
  <body>
    <svg width="960" height="530"></svg>
    <script>
      const xValue = d => d.imdb_score;
      const xLabel = 'IMDB Rate';
      const yValue = d => d.movie_facebook_likes;
      const yLabel = 'Facebook Likes';
      const margin = { left: 150, right: 50, top: 20, bottom: 120 };

      const svg = d3.select('svg');
      const width = svg.attr('width');
      const height = svg.attr('height');
      const innerWidth = width - margin.left - margin.right;
      const innerHeight = height - margin.top - margin.bottom;

      const g = svg.append('g')
          .attr('transform', `translate(${margin.left},${margin.top})`);
      const xAxisG = g.append('g')
          .attr('transform', `translate(0, ${innerHeight})`)
      const yAxisG = g.append('g');
      
      xAxisG.append('text')
          .attr('class', 'x-axis-label')
          .attr('x', innerWidth / 2)
          .attr('y', 80)
          .text(xLabel);

      yAxisG.append('text')
          .attr('class', 'y-axis-label')
          .attr('x', -innerHeight / 2)
          .attr('y', -100)
          .attr('transform', `rotate(-90)`)
          .style('text-anchor', 'middle')
          .text(yLabel);

      const xScale = d3.scaleLinear();
      const yScale = d3.scaleLog();

      const xAxis = d3.axisBottom()
        .scale(xScale)
        .tickPadding(14)
        .tickSize(-innerHeight);

      const commaFormat = d3.format(',');
      const yAxis = d3.axisLeft()
        .scale(yScale)
        .ticks(11)
        .tickPadding(18)
        .tickFormat(d => Math.log10(d) % 1 === 0 ? commaFormat(d) : '')
        .tickSize(-innerWidth);

      const row = d => {        
        d.movie_facebook_likes = +d.movie_facebook_likes;
        if(d.movie_facebook_likes>0){
          d.imdb_score = +d.imdb_score;
        	return d;
        }        
      };

      d3.csv('movie_metadata.csv', row, data => {
        xScale
          .domain(d3.extent(data, xValue))
          .range([0, innerWidth])
          .nice();

        yScale
          .domain(d3.extent(data, yValue))
          .range([innerHeight, 0])
          .nice();
        g.selectAll('circle').data(data)
          .enter().append('circle')
            .attr('class', 'circle')
            .attr('cx', d => xScale(xValue(d)))
            .attr('cy', d => yScale(yValue(d)))
            .attr('fill-opacity', 0.08)
            .attr('r', 9)
          .append('title')
            .text(d => d.movie_title + ' (' + commaFormat(yValue(d)) + ' likes)' );

        xAxisG.call(xAxis);
        yAxisG.call(yAxis);
      });
    </script>
  </body>
</html>