Created
September 27, 2020 18:22
-
-
Save waseemnawaz/b3ea12a6cea4295140a736ed2480e0cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = [ | |
[ 400, 200 ], | |
[ 210,140 ], | |
[ 722,300 ], | |
[ 70,160 ], | |
[ 250,50 ], | |
[ 110,280 ], | |
[ 699,225 ], | |
[ 90, 220 ] | |
]; | |
var chart_width = 850; | |
var chart_height = 400; | |
var bar_padding = 50; | |
//create the svg | |
var svg = d3.select('#chart') | |
.append('svg') | |
.attr('height', chart_height) | |
.attr('width', chart_width) | |
//create the scales | |
var x_scale = d3.scaleLinear() | |
.domain([d3.min(data, function(d){ | |
return d[0] | |
}),d3.max(data, function(d){ | |
return d[0] | |
})]) | |
.rangeRound([bar_padding, chart_width - bar_padding]) | |
var y_scale = d3.scaleLinear() | |
.domain([d3.min(data, function(d){ | |
return d[1] | |
}),d3.max(data, function(d){ | |
return d[1] | |
})]) | |
//.rangeRound([bar_padding, chart_height - bar_padding]) | |
.rangeRound([chart_height - bar_padding, bar_padding]) | |
var r_scale = d3.scaleSqrt() | |
.domain([d3.min(data, function(d){ | |
return d[1] | |
}),d3.max(data, function(d){ | |
return d[1] | |
})]) | |
.rangeRound([5, 15]) | |
//create an axis | |
var x_axis = d3.axisBottom(x_scale) | |
svg.append('g') | |
.attr('class', 'x-axis') | |
.attr('transform', 'translate(0, ' + (chart_height - bar_padding) + ')') | |
.call(x_axis) | |
var y_axis = d3.axisLeft(y_scale) | |
.tickValues([100, 200, 300]) | |
svg.append('g') | |
.attr('class', 'y-axis') | |
.attr('transform', 'translate(' + bar_padding + ', 0)') | |
.call(y_axis) | |
//.tickValues('100', '200', '300') | |
//create the circles | |
svg.selectAll('circle') | |
.data(data) | |
.enter() | |
.append('circle') | |
.attr('cx', function(d, i){ | |
return x_scale(d[0]) | |
}) | |
.attr('cy', function(d){ | |
return y_scale(d[1]) | |
}) | |
.attr('r', function(d){ | |
return r_scale(d[1]) | |
}) | |
.attr('fill', function(d){ | |
return 'rgb('+ Math.round(d[1] + 0.5) +',0,0)' | |
//return "rgb(0, 0, " + Math.round(d[1] + 1) + ")" | |
}) | |
//create the labels | |
svg.append('g') | |
.selectAll('text') | |
.data(data) | |
.enter() | |
.append('text') | |
.text(function(d){ | |
return d.join('-') | |
}) | |
.attr('x', function(d, i){ | |
return x_scale(d[0]) + 10 | |
}) | |
.attr('y', function(d){ | |
return y_scale(d[1]) | |
}) | |
/*.attr('r', function(d){ | |
return r_scale(d[1]) | |
})*/ | |
.attr('fill', 'black') | |
.attr('font-size', '12px') | |
.attr('font-weight', 'bold') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>D3 Scatter Plot</title> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
</head> | |
<body> | |
<div id="chart"></div> | |
<script src="https://d3js.org/d3.v5.js"></script> | |
<script src="app4.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#chart{ | |
width: 850px; | |
height: 400px; | |
background-color: #f7f7f7; | |
margin: 25px auto; | |
} | |
.x-axis path .x-axis line{ | |
shape-rendering: crispEdges; | |
} | |
.x-axis text{ | |
font-family: sans-serif; | |
font-weight: bold; | |
font-size: 16px; | |
} | |
.y-axis path .y-axis line{ | |
shape-rendering: crispEdges; | |
} | |
.y-axis text{ | |
font-family: sans-serif; | |
font-weight: bold; | |
font-size: 16px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment