chart = {
const svg = d3.select(DOM.svg(width, height))
.attr("viewBox", `${-width / 2} ${-height/2} ${width} ${height}`)
.style("width", width)
.style("height", height)
.style("font", "10px sans-serif");
const g = svg.append("g")
.attr("class","dts")
g.selectAll("g")
.data(dts)
.join("g")
.call(g=>{
g.append("path")
.attr("fill", (d,i)=>color(i))
.attr("d", arc)
g.append("text")
.attr('text-anchor', 'left')
.attr("fill-opacity","0.8")
.attr("font-size","0.7em")
.attr("transform",d => {
const angle = x(d.name) + x.bandwidth()/2
const radius = (y(d.val) + y(0))/2
const xx = radius * Math.sin(angle)
const yy = -radius * Math.cos(angle)
return `translate(${xx},${yy}) rotate(${180/Math.PI * angle - 90})`
})
.text(d => d.name)
})
svg.append("g")
.attr("class","category-one")
.selectAll("g")
.data(words)
.join("g")
.call(g => {
g.append("path")
.attr("fill", (d,i)=>color(i))
.attr("d", wordArc1)
g.append("clipPath") //限定绘制范围为数据对应的矩形大小,超出矩形的的文字不绘制
.attr("id",(d,i)=>("first_"+ i +"_clip"))//设置id并在数据结构中新建clipUid属性方便下面引用
.append("path")//矩形大小同上
.attr("d", wordArc1)
g.append("g")
.attr("clip-path",(d,i)=>`url(#first_${i}_clip)`)//引用clipPath来限制绘制范围
.append("text")
.attr('text-anchor', 'middle')
.attr("transform",(d,i)=>{
const angle = firstScaleX(i)+firstScaleX.bandwidth()/2
const {short,long} = firstScaleY(i)
const radius = (short+long)/2
const _x = radius * Math.sin(angle)
const _y = -radius * Math.cos(angle)
return `translate(${_x},${_y}) rotate(${180/Math.PI * angle })`
})
.text(d=>d)
})
svg.append("g")
.attr("class","category-two")
.selectAll("g")
.data(Task)
.join("g")
.call(g => {
g.append("path")
.attr("fill", "#6699ff")
.attr("fill-opacity",d=>d[1])
.attr("d", wordArc2)
g.append("clipPath") //限定绘制范围为数据对应的矩形大小,超出矩形的的文字不绘制
.attr("id",(d,i)=>("second_"+ i +"_clip"))//设置id并在数据结构中新建clipUid属性方便下面引用
.append("path")//矩形大小同上
.attr("d", wordArc2)
g.append("g")
.attr("clip-path",(d,i)=>`url(#second_${i}_clip)`)//引用clipPath来限制绘制范围
.append("text")
.attr("font-size","1em")
.attr('text-anchor', 'middle')
.attr("transform",(d,i)=>{
const angle = secondScaleX(i)+secondScaleX.bandwidth()/2
const {short,long} = ScaleY(i)
const radius = (short+long)/2
const _x = radius * Math.sin(angle)
const _y = -radius * Math.cos(angle)
return `translate(${_x},${_y}) rotate(${180/Math.PI * angle })`
})
.text(d=>d[0])
})
svg.append("g")
.append("circle")
.attr("r",outerRadius)
.attr("fill","none")
.attr("stroke","black")
.attr("stroke-dasharray","4")
svg.append("g")
.append("circle")
.attr("r",wordRadius)
.attr("fill","none")
.attr("stroke","black")
.attr("stroke-dasharray","4")
svg.append("g")
.call(g => {
const arc = d3.arc()
g.append("path")
.attr("id","wordRadius")
.attr("d",arc({
innerRadius: wordRadius+10,
outerRadius: wordRadius+10,
startAngle: firstScaleX(0),
endAngle: firstScaleX(5)
}))
.attr("fill","none")
g.append("text")
.attr("fill", "black")
.attr('text-anchor', 'middle')
.attr("font-size","2em")
.attr("x",425)
.append("textPath")
.attr("href","#wordRadius")
.text("category-one")
})
//折线图
const LineGraph = svg.append("g")
.attr("class","lineGraph")
LineGraph.append("path")
.attr("fill","none")
.attr('stroke', '#ff0000')
.attr('stroke-width', 1)
.attr('d', smoothLine(speakTimes) )
LineGraph.append("path")
.attr("fill", "steelblue")
.attr("fill-opacity", 0.2)
.attr("d", area
.outerRadius(scaleBottomLineY(0))
.innerRadius(d => scaleBottomLineY(d))
(speakTimes))
svg.append("g")
.call(yAxisBottom)
return svg.node();
}