OtroScatterPlot

In this example below you will see how to do a OtroScatterPlot with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by zdflower, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright zdflower ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>OtroScatterPlot</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <!-- 

Hello Camper!

For now, the test suite only works in Chrome! Please read the README below in the JS Editor before beginning. Feel free to delete this message once you have read it. Good luck and Happy Coding! 

- The freeCodeCamp Team 

-->
	<h1>Scatterplot visualization</h1>
	<svg id="plot"></svg>
  <div></div>
  <script src='https://gitcdn.link/repo/freeCodeCamp/testable-projects-fcc/master/build/bundle.js'></script>
<script src='https://d3js.org/d3.v5.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/zdflower/otroscatterplot-zWVNPB */
html {
		margin: 0;
		padding:0;
	}
body {
	background-image: url("https://raw.githubusercontent.com/zdflower/scatterplotD3/master/landscape-1024.jpg");
	background-size: cover;
	margin:0
	padding:0
}

svg {
	display:block;
	position: relative;
	margin: auto;
}

.ciclista {
	font-size: 8pt;
	text-anchor: initial;
}

circle.noDoping {
      fill: purple;
}

circle.doping {
      fill: red;
}
    
#plot {
  border: solid 1px black;
  box-shadow: 1px 1px 1px 1px;
}

#title{
  font-size: 18pt;
}

.axis text {
  font-family: cursive;
  font-size: 10pt;
}

.label {
  font-size: 16pt;
}

#tooltip {
  position: absolute;
  background-color: red;
  color: orange;
  width: 150px;
  height:110px;
  padding: 5px;
  font-size: 10pt;
  pointer-events: none; 
}

/*Downloaded from https://www.codeseek.co/zdflower/otroscatterplot-zWVNPB */
// !! IMPORTANT README:

// You may add additional external JS and CSS as needed to complete the project, however the current external resource MUST remain in place for the tests to work. BABEL must also be left in place. 

/***********
INSTRUCTIONS:
  - Select the project you would 
    like to complete from the dropdown 
    menu.
  - Click the "RUN TESTS" button to
    run the tests against the blank 
    pen.
  - Click the "TESTS" button to see 
    the individual test cases. 
    (should all be failing at first)
  - Start coding! As you fulfill each
    test case, you will see them go   
    from red to green.
  - As you start to build out your 
    project, when tests are failing, 
    you should get helpful errors 
    along the way!
    ************/

// PLEASE NOTE: Adding global style rules using the * selector, or by adding rules to body {..} or html {..}, or to all elements within body or html, i.e. h1 {..}, has the potential to pollute the test suite's CSS. Try adding: * { color: red }, for a quick example!

// Once you have read the above messages, you can delete all comments. 
var plot = d3.select("svg");

var tooltip = d3.select("div").attr("id", "tooltip").style("opacity", 0);

var margin = { top: 100, right: 80, bottom: 50, left: 100 };
var height = 500 - margin.top - margin.bottom;
var width = 900 - margin.left - margin.right;

plot.style("background-color", "white").style("opacity", "0.8").attr("width", width + margin.left + margin.right).attr("height", height + margin.top + margin.bottom);

plot.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Escalas
var xScale = d3.scaleTime().range([margin.left, width]);

var yScale = d3.scaleTime().range([margin.top, height]);

var parseMinutes = d3.timeParse("%M:%S");
var parseYear = d3.timeParse("%Y");

d3.json("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/cyclist-data.json").then(function (data) {
  data.forEach(function (d) {
    d.Time = parseMinutes(d.Time);
    d.Year = parseYear(d.Year);
  });

  render(data);
}).catch(function (error) {
  return console.error(error);
});

// Idea de usar una función render tomada de Curran Kelleher - Introduction to D3 - Youtube - 2015
function render(data) {

  var minX = d3.min(data, function (d) {
    return d.Year;
  });

  var unoMenosQueMinX = new Date(minX.getFullYear() - 1, minX.getMonth(), minX.getDay());

  var maxX = d3.max(data, function (d) {
    return d.Year;
  });

  var minY = d3.min(data, function (d) {
    return d.Time;
  });

  var medioMenosQueMinY = new Date(minY.getTime() - 30000);
  var maxY = d3.max(data, function (d) {
    return d.Time;
  });
  var medioMasQueMaxY = new Date(maxY.getTime() + 30000); // 60000 milisegundos = 1 minuto


  xScale.domain([unoMenosQueMinX, maxX]);
  yScale.domain([medioMenosQueMinY, medioMasQueMaxY]);

  plot.selectAll("circle").data(data).enter().append("circle").attr("class", "dot").attr("data-xvalue", function (d) {
    return d.Year;
  }).attr("data-yvalue", function (d) {
    return d.Time;
  }).attr("r", "5").attr("cx", function (d) {
    return xScale(d.Year);
  }).attr("cy", function (d) {
    return yScale(d.Time);
  }).style("fill", function (d) {
    return d.Doping ? "red" : "purple";
  }).on("mouseover", function (d) {
    tooltip.attr("data-year", d.Year);
    tooltip.transition().duration(200).style("opacity", .9);
    tooltip.html(d.Time.getMinutes() + ":" + d.Time.getSeconds() + "min. in " + d.Year.getFullYear() + "<br/>" + d.Name + " from " + d.Nationality + "<br/>" + d.Doping).style("left", d3.event.pageX + "px").style("top", d3.event.pageY - 28 + "px");
  }).on("mouseout", function (d) {
    tooltip.transition().duration(500).style("opacity", 0);
  });

  // ejes
  // x
  plot.append("g").attr("id", "x-axis").attr("transform", "translate(0," + height + ")").call(d3.axisBottom(xScale));

  // y
  plot.append("g").attr("id", "y-axis").attr("transform", "translate(" + margin.left + "," + 0 + ")").call(d3.axisLeft(yScale).tickFormat(d3.timeFormat("%M:%S")));

  // y label
  plot.append("text").attr("transform", "rotate(-90)").attr("x", "-250").attr("y", "50").attr("class", "label").text("Minutes");

  // x label
  plot.append("text").attr("x", "250").attr("y", height + margin.bottom).attr("class", "label").text("Year");

  // Título
  plot.append("g").attr("transform", "translate(" + margin.left / 2 + "," + margin.top / 2 + ")").append("text").attr("id", "title").text("Doping en ciclismo profesional. 35 mejores tiempos en Alpe d'Huez");

  leyenda(plot);
}

function leyenda(plot) {
  var leyenda = plot.append("g").attr("transform", "translate(" + (width - margin.right) + "," + (height / 2 + 50) + ")").attr("id", "legend");

  leyenda.append("circle").attr("transform", "translate(0,0)").attr("r", "5").attr("class", "doping");

  leyenda.append("text").attr("transform", "translate(10,5)").text("Con acusación de dopaje");

  leyenda.append("circle").attr("transform", "translate(0,20)").attr("r", "5").attr("class", "noDoping");

  leyenda.append("text").attr("transform", "translate(10,25)").text("Sin acusación de dopaje");
}

Comments