我為航空公司事故制作了一張圖表,我想用平面形狀替換圓形。我制作了一個非常簡單的平面 SVG,但不知道如何在我的圖表中使用它。感謝您提供的任何幫助。這是我想使用的 SVG:
<svg xmlns="http://www.w3.org/2000/svg" width="313" height="305">
<path d="m2,106h28l24,30h72l-44,-133h35l80,132h98c21,0 21,34 0,34l-98,0 -80,134h-35l43,-133h-71l-24,30h-28l15,-47"/>
</svg>
這是圖表的代碼:我試圖在 append 方法中為 SVG 提供路徑引數,但沒有奏效。
<!-- Code from d3-graph-gallery.com -->
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.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: 30, bottom: 90, left: 40},
width = 460 - 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 ")");
// Parse the Data
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/7_OneCatOneNum_header.csv", function(data) {
// X axis
var x = d3.scaleBand()
.range([ 0, width ])
.domain(data.map(function(d) { return d.Country; }))
.padding(1);
svg.append("g")
.attr("transform", "translate(0," height ")")
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "translate(-10,0)rotate(-45)")
.style("text-anchor", "end");
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 13000])
.range([ height, 0]);
svg.append("g")
.call(d3.axisLeft(y));
// Lines
svg.selectAll("myline")
.data(data)
.enter()
.append("line")
.attr("x1", function(d) { return x(d.Country); })
.attr("x2", function(d) { return x(d.Country); })
.attr("y1", function(d) { return y(d.Value); })
.attr("y2", y(0))
.attr("stroke", "grey")
// Circles
svg.selectAll("mycircle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) { return x(d.Country); })
.attr("cy", function(d) { return y(d.Value); })
.attr("r", "4")
.style("fill", "#69b3a2")
.attr("stroke", "black")
})
</script>

uj5u.com熱心網友回復:
您可能需要做更多的作業來改造您的飛機。但是,這將讓你開始。
var plane_d = "m2,106h28l24,30h72l-44,-133h35l80,132h98c21,0 21,34 0,34l-98,0 -80,134h-35l43,-133h-71l-24,30h-28l15,-47"
// Append group for each data point
var planes = svg.selectAll(null)
.data(data)
.enter()
.append("g")
.attr("transform", function(d) { return 'translate(' x(d.Country) ',' y(d.Value) ')' })
// draw plane on each group
planes.append('path')
.attr('d', plane_d)
.style("fill", "#69b3a2")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/329036.html
標籤:javascript d3.js
上一篇:Haskell錯誤-無法將型別“[Int]->String”與“[Char]”匹配;預期型別:字串;實際型別:[Int]->字串
下一篇:一些節點折疊的D3強制布局
