将平均值和n值添加到nvd3 boxplot

时间:2015-12-21 23:55:32

标签: d3.js nvd3.js boxplot

我在我的图表中使用nvd3 boxplot。在箱线图上有mean作为星号(*)的选项吗?我们是否也可以在顶部晶须上方具有n值,类似于下图。

enter image description here

此问题已发布here。 提前谢谢。

修改 我想添加一个我从数据点计算的平均值,而不仅仅是盒子图的中心。由于异常值,计算出的平均值可能不在箱形图的中心。

1 个答案:

答案 0 :(得分:2)

您可以通过执行以下算法来实现此目的:

  1. 获取所有矩形
  2. 找到中间点
  3. 创建文本并将其放入上面计算的中心
  4. 代码段:

    function makeMarkOnMean(){
        d3.selectAll(".mean").remove();//remove all * mean markers
        //get all the rectangles
        d3.selectAll(".nv-boxplot-box")[0].forEach(function(r){
          window.setTimeout(function(){
            var x = parseFloat(d3.select(r).attr("x")) + d3.select(r).attr("width")/2 - 3; //x position of the star
            var y = parseFloat(d3.select(r).attr("y")) + parseFloat(d3.select(r).attr("height"))/2+12;//y position of the star
            //now make the star on the above x and y        
            d3.select(r.parentNode).append("text").attr("class", "mean").style("font-size", "x-large").text("*").style("fill", "red").attr("x",x).attr("y", y);
          },500)
    });
    

    工作代码here