NVD3JavaScript:向散点图中的点添加颜色

NVD3JavaScript:向散点图中的点添加颜色,javascript,plot,scatter-plot,scatter,nvd3.js,Javascript,Plot,Scatter Plot,Scatter,Nvd3.js,我试图从一些数据中用x、y和z值绘制散点图 我的代码与NVD3网站上的示例相同, ,除了我还计算z值。 假设z=x+y。我希望通过在两种颜色之间插值来设置颜色,而不是更改半径的值 所有的点都显示在图表上,但我不知道如何为单个点设置颜色,只为系列设置颜色。为简单起见,我首先尝试将点设置为静态颜色,如下所示: [{"key":"X Plus Y","values":[{"x":0,"y":0,&quo

我试图从一些数据中用x、y和z值绘制散点图

我的代码与NVD3网站上的示例相同, ,除了我还计算z值。 假设z=x+y。我希望通过在两种颜色之间插值来设置颜色,而不是更改半径的值

所有的点都显示在图表上,但我不知道如何为单个点设置颜色,只为系列设置颜色。为简单起见,我首先尝试将点设置为静态颜色,如下所示:

[{"key":"X Plus Y","values":[{"x":0,"y":0,"z":0,"color":"#ff0000"}, ...]
但这不起作用,所以我想我需要用javascript来实现。我查看了scatterChart.js,但没有找到简单的方法。但我绝不是javascript专家,所以我可能很容易错过了一些东西

有什么建议吗?我需要学习如何创建新的模型文件吗

我还想在工具提示中显示z值,但这将是步骤2


谢谢

我刚刚遇到了同样的问题,并找到了一个适合我的用例的解决方法-YMMV

基本上,我的理解(来自NVD3的源代码)是,您实际上只能为系列设置颜色,而不能为单个点设置颜色:因此,我确实向values对象添加了
颜色:'#rrggbb'
项,就像您在示例中所做的那样(因为这样构建数据结构对我来说比较容易),然后根据此设置每个系列的颜色值(我使用下划线.js,但您应该能够在纯JavaScript中完成此操作):

如果您需要使用颜色连续体,而不是逐点设置颜色或使用离散比例,我过去常让D3为我生成颜色值:

function compute_color_on_linear_scale(data, value, colorProperty, colorRange) {
    // get a list of all the groups (this works if the colorProperty,
    // aka z in your case, is numeric, otherwise the sort function
    // needs to be adjusted accordingly)
    // In your case, you could just write item.z rather than item[colorProperty]
    // if your function doesn't need to be generic, and in that case you can
    // omit the colorProperty argument from the argument list altogether
    categories = _.uniq(_.map(data, function(item) {
        return item[colorProperty];
    }).sort(function(a,b){return a - b}), true);

    // if no color range was provided, set a default one
    if(typeof colorRange === 'undefined') { colorRange = ['red', 'green']; }

    // this is mbostock's magic enchantment from his reply linked above
    var color = d3.scale.ordinal()
      .domain(categories)
      .range(d3.range(categories.length).map(d3.scale.linear()
      .domain([0, categories.length - 1])
      .range(colorRange)
      .interpolate(d3.interpolateLab)));

    return color(value);
}
这应该能奏效。我最后一次接触是隐藏这个传说,因为这在这种情况下没有多大意义

至于在工具提示中显示z值,您需要覆盖scatterChart()的默认ToolIPContent()函数,例如

var chart = nv.models.scatterChart()
  .showDistX(true)
  .showDistY(true)
  .tooltipContent(function(key, x, y, obj) {
      return '<h3>' + obj.point.label + ' (' + key + ')</h3>';
  });

要在散点图中为气泡添加颜色属性,我们可以在nv.models.scatter中添加getColor访问器,如getSize

 , getSize      = function(d) { return d.size || 1} // accessor to get the point size
 , getColor     = function(d) { 
                var colors = ["red", "orange", "yellow", "green", "blue"];
                return colors[d.color] || 'red'} // accessor to get the point color
可以在绘制圆的位置使用“填充”属性添加颜色

//Info:May be on Mousehover??
points.enter().append('circle')
    .attr('cx', function(d,i) { return x0(getX(d,i)) })
    .attr('cy', function(d,i) { return y0(getY(d,i)) })
    .attr('r', function(d,i) { return Math.sqrt(z(getSize(d,i))/Math.PI) })
    .attr('fill', function(d,i) { return getColor(d,i)});
 , getSize      = function(d) { return d.size || 1} // accessor to get the point size
 , getColor     = function(d) { 
                var colors = ["red", "orange", "yellow", "green", "blue"];
                return colors[d.color] || 'red'} // accessor to get the point color
//Info:May be on Mousehover??
points.enter().append('circle')
    .attr('cx', function(d,i) { return x0(getX(d,i)) })
    .attr('cy', function(d,i) { return y0(getY(d,i)) })
    .attr('r', function(d,i) { return Math.sqrt(z(getSize(d,i))/Math.PI) })
    .attr('fill', function(d,i) { return getColor(d,i)});