d3-嵌套饼图的标签放置

时间:2018-10-15 16:41:38

标签: javascript d3.js charts

我想放置类似于下图所示的标签。抱歉,这可能是2个问题。

enter image description here

尝试了2种不同的方法。

一个不合适的例子,从一个圆开始,独立地绘制每个段,并依赖于数据的排序方式和标记为parent的属性来标识块内的一个段(主段/更大段)。这样,我就无法轻松地根据主要部分在圆中的位置放置标签,并且在数据上感觉不自然。

https://jsfiddle.net/raven0us/c2jtsv4m/

更合适的是,将大块(主要段)和内部大块作为子级,这样,我可以使用centroid并相应地放置标签。而且,事情看起来很自然,但是我无法弄清楚如何在主线段内绘制多个内部线段,因此看起来就像我上一次尝试中的图表一样。

https://jsfiddle.net/raven0us/1v9mtdjL/

在每个脚本的开头console.log(data)之前colors模拟数据,以查看我要说明的数据的确切结构。

2 个答案:

答案 0 :(得分:2)

您已经拥有的布局取决于您的数据是否统一,这在现实世界中不会发生,因此我找到了一个数据集,并用它来创建不需要完美数据的饼图。< / p>

它是第一张和第二张图表的混合。我在代码中添加了很多注释,因此请仔细检查并确认您了解发生了什么。我在https://bl.ocks.org/ialarmedalien/1e453ed9b148be442f50e06ad7eb3759放置了一个演示,因此您可以看到那里的数据输入。

function chart(id) {
  // this reads in the CSV file
  d3.csv('morley3.csv').then( data => {

    // this massages the data I'm using into a more suitable form for your chart
    // we have 12 runs with 6 experiments in each.
    // each datum is of the form 
    // { Run: <number>, Expt: <number>, Speed: <number> }
    const filteredData = data
        .filter( d => d.Run < 13 )
        .map( d => { return { Run: +d.Run, Expt: +d.Expt, Speed: +d.Speed } } )

    // set up the chart
    const width = 800,
    height = 800,
    radius = Math.min(height, width) * 0.5 - 100,
    // how far away from the chart the labels should be
    labelOffset = 10,

    svg = d3.select(id).append("svg")
        .attr("width", width)
        .attr("height", height),

    g = svg.append("g")
        .attr("transform", `translate(${width/2}, ${height/2})`),

    // this will be used to generate the pie segments
    arc = d3.arc()
      .outerRadius(radius)
      .innerRadius(0),

    // group the data by the run number
    // this results in 12 groups of six experiments
    // the nested data has the form
    // [ { key: <run #>, values: [{ Run: 1, Expt: 1, Speed: 958 }, { Run: 1, Expt: 2, Speed: 869 } ... ],
    //   { key: 2, values: [{ Run: 2, Expt: 1, Speed: 987 },{ Run: 2, Expt: 2, Speed: 809 } ... ],
    // etc.
    nested = d3.nest()
      .key( d => +d.Run )
      .entries(filteredData),

    chunkSize = nested[0].values.length,

    // d3.pie() is the pie chart generator
    pie = d3.pie()
      // the size of each slice will be the sum of all the Speed values for each run
      .value( d => d3.sum( d.values, function (e) { return e.Speed } ) )
      // sort by run #
      .sort( (a,b) => a.key - b.key )
      (nested)


    // bind the data to the DOM. Add a `g` for each run
    const runs = g.selectAll(".run")
      .data(pie, d => d.key )
      .enter()
      .append("g")
      .classed('run', true)
      .each( d => {
        // run the pie generator on the children
        // d.data.values is all the experiments in the run, or in pie terms,
        // all the experiments in this piece of the pie. We're going to use 
        // `startAngle` and `endAngle` to specify that we're only generating
        // part of the pie. The values for `startAngle` and `endAngle` come
        // from using the pie chart generator on the run data.

        d.children = d3.pie()
        .value( e => e.Speed )
        .sort( (a,b) => a.Expt - b.Expt )
        .startAngle( d.startAngle )
        .endAngle( d.endAngle )
        ( d.data.values )
      })

    // we want to label each run (rather than every single segment), so
    // the labels get added next.
    runs.append('text')
      .classed('label', true)
      // if the midpoint of the segment is on the right of the pie, set the
      // text anchor to be at the start. If it is on the left, set the text anchor
      // to the end.
      .attr('text-anchor', d => {
        d.midPt = (0.5 * (d.startAngle + d.endAngle))
        return d.midPt < Math.PI ? 'start' : 'end'
      } )
      // to calculate the position of the label, I've taken the mid point of the
      // start and end angles for the segment. I've then used d3.pointRadial to
      // convert the angle (in radians) and the distance from the centre of 
      // the circle/pie (pie radius + labelOffset) into cartesian coordinates.
      // d3.pointRadial returns [x, y] coordinates
      .attr('x', d => d3.pointRadial( d.midPt, radius + labelOffset )[0] )
      .attr('y', d => d3.pointRadial( d.midPt, radius + labelOffset )[1] )
      // If the segment is in the upper half of the pie, move the text up a bit
      // so that the label doesn't encroach on the pie itself
      .attr('dy', d => {
        let dy = 0.35;
        if ( d.midPt < 0.5 * Math.PI || d.midPt > 1.5 * Math.PI ) {
          dy -= 3.0;
        }
        return dy + 'em'
      })
      .text( d => {
        return 'Run ' + d.data.key + ', experiments 1 - 6'
      })
      .call(wrap, 50)

    // now we can get on to generating the sub segments within each main segment.
    // add another g for each experiment     
    const expts = runs.selectAll('.expt')
      // we already have the data bound to the DOM, but we want the d.children,
      // which has the layout information from the pie chart generator
      .data( d => d.children )
      .enter()
      .append('g')
      .classed('expt', true)

    // add the paths for each sub-segment
    expts.append('path')
      .classed('speed-segment', true)
      .attr('d', arc)
    // I simplified this slightly to use one of the built-in d3 colour schemes
    // my data was already numeric so it was easy to use the run # as the colour
      .attr('fill', (d,i) => {
        const c = i / chunkSize,
        color = d3.rgb( d3.schemeSet3[ d.data.Run - 1 ] );

        return c < 1 ? color.brighter(c*0.5) : color;
      })
      // add a title element that appears when mousing over the segment
      .append('title')
      .text(d => 'Run ' + d.data.Run + ', experiment ' + d.data.Expt + ', speed: ' + d.data.Speed )

    // add the lines
    expts.append('line')
      .attr('y2', radius)
      // assign a class to each line so we can control the stroke, etc., using css
      .attr('class', d => {
        return 'run-' + d.data.Run + ' expt-' + d.data.Expt
      })
      // convert the angle from radians to degrees
      .attr("transform", d => {
        return "rotate(" + (180 + d.endAngle * 180 / Math.PI) + ")";
      });

    function wrap(text, width) {
        text.each(function () {
            let text = d3.select(this),
                words = text.text().split(/\s+/).reverse(),
                word,
                line = [],
                lineNumber = 0,
                lineHeight = 1.2, // ems
                tfrm = text.attr('transform')
                y = text.attr("y"),
                x = text.attr("x"),
                dy = parseFloat(text.attr("dy")),
                tspan = text.text(null).append("tspan")
                .attr("x", x)
                .attr("y", y)
                .attr("dy", dy + "em");

            while (word = words.pop()) {
                line.push(word);
                tspan.text(line.join(" "));
                if (tspan.node().getComputedTextLength() > width) {
                    line.pop();
                    tspan.text(line.join(" "));
                    line = [word];
                    tspan = text.append("tspan")
                    .attr("x", x)
                    .attr("y", y)
                    .attr("dy", ++lineNumber * lineHeight + dy + "em")
                        .text(word);
                }
            }
        });
    }

    return svg;
  })
}

chart('#chart');

答案 1 :(得分:1)

我不确定我是否正确理解了这个问题。但这太长了,无法塞入评论,所以我写了一个答案,也许可以解决问题。

第一种方法存在的问题是:

  

这样,我就无法轻松地根据主要细分受众群的位置放置标签   放在圆圈中

标签放置代码为:

labels.selectAll("text")
        .data(keys)
        .enter()
        .append("text")
        .style("text-anchor", "middle")
        .style("font-weight", "bold")
//LABEL PLACEMENT CODE
            .attr("x", (d, i) => {
                return barScale(config.max * 1.2) * Math.cos(segmentSlice * i - Math.PI / 2);
            })
            .attr("y", (d, i) => {
                return barScale(config.max * 1.2) * Math.sin(segmentSlice * i - Math.PI / 2);
            })

这将标签沿大段分隔线放置。我们共有12个航段,每个航段跨越30度。每个大段都有6个子段,每个子段跨度5度。因此,看来您只需要将标签旋转15度(3个子段跨度) 将它们像问题中的图片一样放置。

首先将15度转换为弧度:

15 * PI / 180 = 0.261799

然后将以上值添加到标签放置代码中:

.attr("x", (d, i) => {
    return barScale(config.max * 1.2) * 
        Math.cos(segmentSlice * i - Math.PI / 2 + 0.261799); //HERE
    }).attr("y", (d, i) => {
        return barScale(config.max * 1.2) * 
           Math.sin(segmentSlice * i - Math.PI / 2 + 0.261799); //AND HERE
                })

这是更新的小提琴:https://jsfiddle.net/fha19jtm/

所有标签都像给定图片一样放置。 data属性还可以用于基于大/小段的组合来更改旋转角度。这样,可以将每个标签的放置位置调整到所需的数量。