1 回答

TA貢獻1942條經驗 獲得超3個贊
你真的非常需要閱讀手冊,尤其是 SVG 手冊。rect
節點沒有cx
and?cy
,它們有x
and?y
、width
、 and?height
。并且circle
需要一個半徑r
才能可見。
并且您給您讀到的所有屬性都指定了小寫開頭字母。他們需要資本。查找有關調試的手冊。
// set the dimensions and margins of the graph
var margin = {
? ? top: 20,
? ? right: 30,
? ? bottom: 30,
? ? left: 40
? },
? width = 960 - margin.left - margin.right,
? height = 500 - 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("https://gist.githubusercontent.com/michhar/2dfd2de0d4f8727f873422c5d959fff5/raw/fa71405126017e6a37bea592440b4bee94bf7b9e/titanic.csv", function(rawData) {
? const data = rawData.map(function(d) {
? ? return {
? ? ? age: Number(d.Age),
? ? ? fare: Number(d.Fare),
? ? ? sex: d.Sex,
? ? ? survived: d.Survived === "1",
? ? ? name: d.Name
? ? };
? });
? // Add X axis
? var x = d3.scaleLinear()
? ? .domain([0, 80])
? ? .range([0, width]);
? svg.append("g")
? ? .attr("transform", "translate(0," + height + ")");
? // Add Y axis
? var y = d3.scaleLog()
? ? .domain([1e+0, 1e+3])
? ? .range([height, 0]);
? svg.append("g");
? // Add dots
? svg.append('g')
? ? .selectAll("dot").select("female")
? ? .data(data)
? ? .enter()
? ? .append("circle")
? ? .attr("cx", function(d) {
? ? ? return x(d.age);
? ? })
? ? .attr("cy", function(d) {
? ? ? return y(d.fare);
? ? })
? ? .attr("r", 2.8)
? ? .style("opacity", function(d) {
? ? ? return d.survived ? "1" : "0.25";
? ? })
? ? .style("stroke", "black")
? ? .style("fill-opacity", 0.1)
? svg.append('g')
? ? .selectAll("dot").select("male")
? ? .data(data)
? ? .enter()
? ? .append("rect")
? ? .attr("x", function(d) {
? ? ? return x(d.age);
? ? })
? ? .attr("y", function(d) {
? ? ? return y(d.fare);
? ? })
? ? .attr("width", 5)
? ? .attr("height", 5)
? ? .style("opacity", function(d) {
? ? ? return d.survived ? "1" : "0.25";
? ? })
? ? .style("stroke", "black")
? ? .style("fill-opacity", 0.1)
? ? .append("svg:title")
? ? .text(function(d) {
? ? ? return d.name
? ? });
})
<script src="https://d3js.org/d3.v4.js"></script>
<div id="my_dataviz"></div>
添加回答
舉報