Published
Edited
Jul 3, 2020
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])

const chords = chord(data1);

const group = svg.append("g")
.selectAll("g")
.data(chords.groups)
.enter().append("g");


group.filter((d,i)=>i>=n)
.append("path")
.attr("fill", d => color1(d.index))
.attr("stroke", d => d3.rgb(color1(d.index)).darker())
.attr("d", arc);

const groupTick = group.append("g")
.selectAll("g")
.data(d => groupTicks(d, 1))
.join("g")
.attr("transform", d => `rotate(${d.angle * 180 / Math.PI - 90}) translate(${outerRadius},0)`);//D3角度从12点钟开始,文字一开始是水平的对应3点钟,所以文字的初始角度转化为D3角度正好是pi/2

groupTick.append("line")
.attr("stroke", "#000")
.attr("x2", 6);

groupTick
.filter(d => d.value % 2 === 0)
.append("text")
.attr("x", 8)
.attr("dy", ".35em")
.attr("transform", d => d.angle > Math.PI ? "rotate(180) translate(-16)" : null)
.attr("text-anchor", d => d.angle > Math.PI ? "end" : null)
.text(d => d.value);

svg.append("g")
.attr("fill-opacity", 0.67)
.selectAll("path")
.data(chords)
.join("path")
.attr("d", ribbon)
.attr("fill", d => color1(d.source.index))
.attr("stroke", d => d3.rgb(color1(d.source.index)).darker());

return svg.node();
}
Insert cell
chord(data)
Insert cell
data = [
[11975, 5871, 8916, 2868],
[ 1951, 10048, 2060, 6171],
[ 8010, 16145, 8090, 8045],
[ 1013, 990, 940, 6907],
]
Insert cell
n = 2
Insert cell
chord(data1)
Insert cell
data2 = [[1,3],[2,2]]
Insert cell
data1 =[[0,0,1,3],
[0,0,2,2],
[1,2,0,0],
[3,2,0,0]
]
Insert cell
function groupTicks(d, step) {
const k = (d.endAngle - d.startAngle) / d.value;
return d3.range(0, d.value, step).map(value => {
return {value: value, angle: value * k + d.startAngle};
});
}
Insert cell
formatValue = d3.formatPrefix(",.0", 1e3)
Insert cell
chord = d3.chord()
.padAngle(0.05) // 不同点之间间隔角度
.sortSubgroups(d3.descending)
Insert cell
arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius)
Insert cell
ribbon = d3.ribbon()
.radius(innerRadius )
Insert cell
color1 = d3.scaleOrdinal()
.domain(d3.range(2))
.range(["#F26223", "#FFDD89"])
Insert cell
color = d3.scaleOrdinal()
.domain(d3.range(4))
.range(["#000000", "#FFDD89", "#957244", "#F26223"])
Insert cell
outerRadius = Math.min(width, height) * 0.5 - 30
Insert cell
innerRadius = outerRadius - 20
Insert cell
height = Math.min(640, width)
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more