D3
推薦資料來源:
使用
地圖
加上地圖線
更新 x 與 y 軸
事件綁定
隱藏動畫
Forcing
Last updated
Last updated
<line> elements for the edges
<text> elements for the labels
<circle> elements to represent the nodes
<g> elements as containers that move the nodes and labels togetherd3.select('')
d3.selectAll('')d3.select('').attr('width', 20).style("fill", "#69b3a2")d3.select('').append("svg")const x = d3.scaleLinear()
.domain([0, 4000]) // 數值範圍
.range([ 0, width ]); // 實際顯示範圍const data = [{...}...]
d3.select('#test')
.selectAll('circle')
.data(data)
.enter() // 類似forEach
.append("circle")const data = [{
case: 23,
}....]
let aScale = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d.cases)])
.range([0, 300]);
d3.select("#scatterplot #x-axis")
.call(d3.axisBottom(aScale).ticks(5)); .transition()
.duration(1000)d3.json('data/world.json').then(world => {
let projection = d3.geoWinkel3().scale(140).translate([365, 225]);
let geoPath = d3.geoPath(projection);
const svg = d3
.select("#map-chart")
.append("svg")
.attr("width", 365 * 2)
.attr("height", 225 * 2);
svg
.selectAll("path")
.data(topojson.feature(world, world.objects.countries).features)
.enter()
.append("path")
.attr("d", geoPath)
.attr("class", "country");
}); var graticule = d3.geoGraticule();
svg
.append("path")
.datum(graticule)
.attr("class", "graticule line")
.attr("d", geoPath);
svg
.append("path")
.datum(graticule.outline)
.attr("class", "graticule outline")
.attr("d", geoPath);.graticule {
fill: none;
stroke: #777;
stroke-width: .5px;
stroke-opacity: .2;
pointer-events: none;
}
.graticule.outline {
stroke-width: 2px;
} if(!document.querySelector('.wrapper-group g')) {
// 第一次 init
d3.select(".wrapper-group")
.attr("transform", "translate(180, 10)")
.append("g")
.attr("id", "x-axis")
.attr("transform", `translate(0,${this.height})`)
.call(d3.axisBottom(xScale).ticks(5));
d3.select(".wrapper-group")
.append("g")
.attr("id", "y-axis")
.attr("transform", "translate(0,0)")
.call(d3.axisLeft(yScale).ticks(5));
} else {
// 更新 axis
d3.select(".wrapper-group #x-axis")
.call(d3.axisBottom(xScale).ticks(5));
d3.select(".wrapper-group #y-axis")
.call(d3.axisLeft(yScale).ticks(5));
} // 畫 y 軸 標籤
d3.select(".plot-svg")
.append("g")
.attr(
"transform",
"translate(" + this.width / 6 + ", " + this.height / 2 + ")"
)
.append("text")
.attr("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.text('test'); ......
.on('click', function(e) {
d3.select(this).style("fill", 'black');
}) .on('click', function(e) {
d3.select(this).style("fill", d3.color(d3.select(this).attr("fill")).darker());
}) d3.select(ele)
.select("text")
.transition()
.duration(600)
.attr("opacity", switchOn ? 1 : 0);