Skip to content

Instantly share code, notes, and snippets.

@pablocid
Created February 18, 2019 18:58
Show Gist options
  • Select an option

  • Save pablocid/5068e60236e2408d6aac1af69a505bef to your computer and use it in GitHub Desktop.

Select an option

Save pablocid/5068e60236e2408d6aac1af69a505bef to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v4.min.js"></script>
<style>
body { position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<h2>Scatter Plot</h2>
<script>
// Chart Properties
var margin = {top: 20, right: 55, bottom: 50, left: 70};
var width = 960 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;
// setup x
var xValue = function(d) { return d.Calories;}, // data -> value
xScale = d3.scale.linear().range([0, width]), // value -> display
xMap = function(d) { return xScale(xValue(d));}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
// setup y
var yValue = function(d) { return d["Protein (g)"];}, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d));}, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// x-axis
svg.append("g")
.attr("class", "axis x-axis")
.attr("transform", "translate(0," + height + ")") // move axis to bottom of chart
.call(d3.axisBottom(xScale));
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment