繁体   English   中英

在NVD3散点图上绘制标签

[英]Plotting Labels on NVD3 scatterChart

如果我在http://nvd3.org/examples/scatter.html的示例中有以下代码,是否可以在绘制的点上添加标签? 不是工具提示,但是我的数据中的值之一呈现为始终附加到该点的标签吗? 显然,在此特定示例中这样做会太拥挤,但是在人口稀少的图表中,最好标记每个点是否都有名称。 谢谢!

  nv.addGraph(function() {
  var chart = nv.models.scatterChart()
                .showDistX(true)    //showDist, when true, will display those little distribution lines on the axis.
                .showDistY(true)
                .transitionDuration(350)
                .color(d3.scale.category10().range());

  //Configure how the tooltip looks.
  chart.tooltipContent(function(key) {
      return '<h3>' + key + '</h3>';
  });

  //Axis settings
  chart.xAxis.tickFormat(d3.format('.02f'));
  chart.yAxis.tickFormat(d3.format('.02f'));

  //We want to show shapes other than circles.
  chart.scatter.onlyCircles(false);

  var myData = randomData(4,40);
  d3.select('#chart svg')
      .datum(myData)
      .call(chart);

  nv.utils.windowResize(chart.update);

  return chart;
});

/**************************************
 * Simple test data generator
 */
function randomData(groups, points) { //# groups,# points per group
  var data = [],
      shapes = ['circle', 'cross', 'triangle-up', 'triangle-down', 'diamond', 'square'],
      random = d3.random.normal();

  for (i = 0; i < groups; i++) {
    data.push({
      key: 'Group ' + i,
      values: []
    });

    for (j = 0; j < points; j++) {
      data[i].values.push({
        x: random()
      , y: random()
      , size: Math.random()   //Configure the size of each scatter point
      , shape: (Math.random() > 0.95) ? shapes[j % 6] : "circle"  //Configure the shape of each scatter point.
      });
    }
  }

  return data;
}

您可以只添加标签。 这很俗气,但给出了一个主意:

var svg = d3.select('svg');
svg.selectAll('path')
    .each(function(scatterpoint) {
        svg.append('text')
             .x(scatterpoint.x)
             .y(scatterpoint.y)
             .text(scatterpoint.maybe_you_have_text_here);
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM