我使用了root = d3.hierarchy(root); root.sum(d => d.size);
在我的json中我没有子节点的大小值,如何生成适当的d3 Zoomable旭日形象可视化?
这是我的代码
const width = window.innerWidth,height = window.innerHeight, maxRadius =(Math.min(width,height)/ 2) - 5;
const formatNumber = d3.format(',d');
const x = d3.scaleLinear()。range([0,2 * Math.PI])。clamp(true);
const y = d3.scaleSqrt()。range([maxRadius * .1,maxRadius]);
const color = d3.scaleOrdinal(d3.schemeCategory20);
const partition = d3.partition();
const arc = d3.arc()。startAngle(d => x(d.x0))。endAngle(d => x(d.x1)) .innerRadius(d => Math.max(0,y(d.y0))).outerRadius(d => Math.max(0, Y(d.y1)));
const middleArcLine = d => {const halfPi = Math.PI / 2; const角度= [x(d.x0) - halfPi,x(d.x1) - halfPi]; const r = Math.max(0,(y(d.y0)+ y(d.y1))/ 2);
const middleAngle =(angles [1] + angles [0])/ 2; const invertDirection = middleAngle> 0&& middleAngle< Math.PI; //在下象限上写入文本ccw if(invertDirection){angles.reverse(); }
const path = d3.path(); path.arc(0,0,r,角度[0],角度[1], invertDirection); return path.toString(); };
const textFits = d => {const CHAR_SPACE = 6;
const deltaAngle = x(d.x1) - x(d.x0); const r = Math.max(0,(y(d.y0)+ y(d.y1))/ 2); const周长= r * deltaAngle;
返回d.data.name.length * CHAR_SPACE<周长; };
const svg = d3.select(' body')。追加(' svg')。style(' width',' 100vw' ;) .style(' height',' 100vh')。attr(' viewBox',
${-width / 2} ${-height / 2} ${width} ${height}
)。on('点击' ,()=> focusOn()); //重置缩放 在画布上点击d3.json(' dummy3Copy.json',(error,root)=> {if(error)throw error; //启动自定义代码
//结束自定义代码root = d3.hierarchy(root); root.sum(d => d.size);
const slice = svg.selectAll(' g.slice') 。数据(分区(根).descendants());
slice.exit()除去();
const newSlice = slice.enter()。append(' g')。attr(' class',' slice') .on(" mouseover",mouseover).on(" mouseout",mouseOutArc).on('点击', d => {d3.event.stopPropagation(); focusOn(d); });
newSlice.append(' title')。text(d => d.data.name +' \ n' + d.data.id); //.on("mouseover" ;, mouseover);
newSlice.append('路径')。attr(' class',' main-arc')。style(' fill' ,d => color((d.children?d:d.parent).data.name))。attr(' d',arc);
newSlice.append('路径')。attr(' class',' hidden-arc')。attr(' id' ,(_, i)=>
hiddenArc${i}
)。attr(' d',middleArcLine);const text = newSlice.append(' text')。attr(' display',d => textFits(d) ? null:' none');
//添加白色轮廓text.append(' textPath') .attr(' startOffset',' 50%')。attr(' xlink:href',(_,i)=>
#hiddenArc${i}
)。text(d => d.data.name).style(' fill',' none') .style(' stroke',' #ffff')。style(' stroke-width',5) .style(' stroke-linejoin',' round');text.append(' textPath')。attr(' startOffset',' 50%')。attr(' xlink:href&# 39 ;, (_,i)=>
#hiddenArc${i}
)。text(d => d.data.name); });var tooltip = d3.select(" body") .append(" DIV&#34) .attr(" id"," tooltip") .style(" position"," absolute") .style(" z-index"," 10") .style(" opacity",0);
function format_description(d){
return '<b>' + d.name + '</b></br>'; } function mouseover(d) { d3.select(this).attr("stroke","black") tooltip.html(format_description(d)); return tooltip.transition() .duration(50) .style("opacity", 0.9);
} function mouseOutArc(){ d3.select(本).attr(&#34;中风&#34;&#34;&#34) return tooltip.style(&#34; opacity&#34;,0); function focusOn(d = {x0:0,x1:1,y0:0,y1:1}){//如果没有数据点,则重置为顶级 指定的
const transition = svg.transition()。duration(750).tween(&#39; scale&#39;,() =&GT; {const xd = d3.interpolate(x.domain(),[d.x0,d.x1]),yd = d3.interpolate(y.domain(),[d.y0,1]); return t =&gt; {x.domain(xd(t)); y.domain(码(T)); }; });
transition.selectAll(&#39; path.main-arc&#39;)。attrTween(&#39; d&#39;,d =&gt;()=&gt; 弧(d));
transition.selectAll(&#39; path.hidden-arc&#39;)。attrTween(&#39; d&#39;,d =&gt;()=&gt; middleArcLine(d));
transition.selectAll(&#39; text&#39;)。atTTween(&#39; display&#39;,d =&gt;()=&gt; textFits(d)? null:&#39; none&#39;);
moveStackToFront(d);
//
function moveStackToFront(elD){svg.selectAll(&#39; .slice&#39;)。filter(d =&gt; d === elD).each(function(d){this.parentNode.appendChild(this); if(d.parent){moveStackToFront(d.parent);}})}}
提前致谢!
答案 0 :(得分:0)
替换此“root = d3.hierarchy(root); root.sum(d =&gt; d.size);” 以下代码
var root = d3.hierarchy(root,function(d){return d.children}) .sum(函数(d){
if(d.children){ 返回0 } else { 返回1 }});