d3.js - 将平均值和 n 数添加到 nvd3 箱线图中

标签 d3.js nvd3.js boxplot

我在图表中使用 nvd3 boxplot。是否有任何选项可以将 mean 作为箱线图上的星号 (*) ?我们是否也可以在顶部 mustache 上方设置类似于下图的 n 值。

enter image description here

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

编辑 我想添加一个根据数据点计算的平均值,而不仅仅是箱线图的中心。由于异常值,计算出的平均值可能不在箱线图的中心。

最佳答案

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

  1. 获取所有矩形
  2. 找到中间点
  3. 创建一个文本并将其放在上面计算的中心

代码片段:

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 .

关于d3.js - 将平均值和 n 数添加到 nvd3 箱线图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34406371/

相关文章:

javascript - IE 上看起来很奇怪的 NVD3 MultiBarChart

javascript - 将 Json 数据加载到 nvd3 图中

r - 来自数据帧的箱线图,其中包含多个组的分位数

javascript - 如何使用 d3.js 制作正确的倒置堆叠图?

php - 将 JSON 解析数组转换为 CSV

javascript - D3 条形图 X-ticks 未对齐

javascript - D3 v4 获取元素的翻译值

javascript - 如何更新 NVD3 多图的 y 轴?

python - "ValueError: Dimensions of labels and X must be compatible",使用 Jupyter Notebook

python - 如何将绘图在 xticks 上居中对齐