简体   繁体   English

d3颜色渐变线性比例不起作用

[英]d3 color gradient linear scale not working

I have the following creating two circles for each data point, based on the latitude, longitude. 下面,我根据纬度,经度为每个数据点创建两个圆。 The circles differ in size and color. 圆圈的大小和颜色不同。 I'd like for the "fill" in circles1 to be based on a color gradient value returned by colorScale. 我希望circle1中的“填充”基于colorScale返回的颜色渐变值。 However, the circles just appear black. 但是,圆圈​​只是显示为黑色。 What am I getting wrong here? 我这是怎么了?

d3.csv("cities-lived.csv", function(data) {

var colorScale = d3.scale.linear()
    .domain([-15, 7.5, 30])
    .range(["#2c7bb6", "#ffff8c", "#d7191c"])
    .interpolate(d3.interpolateHcl);

var circles1 = svg.selectAll("foo")
    .data(data)
    .enter()
    .append("circle")
    .attr("cx", function(d) {
        return projection([d.lon, d.lat])[0];
    })
    .attr("cy", function(d) {
        return projection([d.lon, d.lat])[1];
    })
    .attr("r", function(d) {
        return Math.sqrt(d.years * 2);
    })
    .style("fill", function(d) {
        return colorScale(d.temp);
    })    
        .style("opacity", 0.85) 

.on("mouseover", function(d) {      
        div.transition()        
           .duration(200)      
           .style("opacity", .9);      
           div.text(d.place)
           .style("left", (d3.event.pageX) + "px")     
           .style("top", (d3.event.pageY - 28) + "px");    
    })   

    // fade out tooltip on mouse out               
    .on("mouseout", function(d) {       
        div.transition()        
           .duration(500)      
           .style("opacity", 0);   
    })

var circles2 = svg.selectAll("foo")
    .data(data)
    .enter()
    .append("circle")
    .attr("cx", function(d) {
        return projection([d.lon, d.lat])[0];
    })
    .attr("cy", function(d) {
        return projection([d.lon, d.lat])[1];
    })
    .attr("r", function(d) {
        return Math.sqrt(d.avg * 2);
    })
        .style("fill", "none")    
        .style("opacity", 0.85) 
        .style("stroke", "black")
        .style("stroke-width", "2");

    // Modification of custom tooltip code provided by Malcolm Maclean, "D3 Tips and Tricks" 
    // http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html

});  

Having this (shown below) in place of the .style("fill", function(d).... works perfectly fine. 用这个(如下所示)代替.style(“ fill”,function(d)....可以很好地工作。

    .style("fill", "rgb(217,91,67)") 

The above code is based on http://blockbuilder.org/anonymous/0e1213b567bcfc74376cc0e9a7238c1b , and the colorscale function is based on http://bl.ocks.org/nbremer/a43dbd5690ccd5ac4c6cc392415140e7 . 上面的代码基于http://blockbuilder.org/anonymous/0e1213b567bcfc74376cc0e9a7238c1b ,并且色阶功能基于http://bl.ocks.org/nbremer/a43dbd5690ccd5ac4c6cc392415140e7

Your code is working fine, your issue must be coming from your data : 您的代码运行正常,您的问题必须出自您的数据:

.style("fill", function(d) {
    return colorScale(d.temp);
})   

d.temp must be undefined d.temp必须undefined

I did a fiddle as an example https://jsfiddle.net/ddspczr1/ 我做了一个小提琴作为示例https://jsfiddle.net/ddspczr1/

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

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