我是 D3 的新手,并试图绘制下面的气泡图。
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<!-- Load color scale -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>
// set the dimensions and margins of the graph
var margin = {top: 10, right: 20, bottom: 30, left: 50},
width = 500 - margin.left - margin.right,
height = 420 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
//Read the data
d3.csv("dis.csv", function(data) {
// Add X axis
var x = d3.scaleLinear()
.domain([0, 30])
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 30])
.range([ height, 0]);
svg.append("g")
.call(d3.axisLeft(y));
// Add a scale for bubble size
var z = d3.scaleLinear()
.domain([0, 300])
.range([ 4, 40]);
// Add a scale for bubble color
var myColor = d3.scaleOrdinal()
.domain(["Asia", "Europe", "Americas", "Africa", "Oceania"])
.range(d3.schemeSet2);
// -1- Create a tooltip div that is hidden by default:
var tooltip = d3.select("#my_dataviz")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "black")
.style("border-radius", "5px")
.style("padding", "10px")
.style("color", "white")
我正在关注本教程https://www.d3-graph-gallery.com/graph/bubble_basic.html ,当我渲染页面时,我看不到任何错误,也没有绘制图表。任何帮助,将不胜感激。
慕娘9325324
相关分类