javascript - 使用 d3.js 创建热图,存在颜色问题

标签 javascript d3.js

嗨,我这里有一个热图,我正在尝试为其着色。现在它全部都是红色的,但我想使用 d3.interpolateRdYlBu,我希望我的值较低的是蓝色,较高的是红色,所以我希望它能很好地胶凝。我知道它的读数正确,因为我在控制台中看到红色并且没有其他错误,但我没有做正确的事情,它没有考虑我的值(value)并相应地做。任何帮助将不胜感激!

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>

<!-- Load color palettes -->
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>

<script>

// set the dimensions and margins of the graph
var margin = {top: 80, right: 25, bottom: 30, left: 40},
  width = 1000 - margin.left - margin.right,
  height = 1000 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
.append("g")
  .attr("transform",
        "translate(" + margin.left + "," + margin.top + ")");

//Read the data
d3.csv("https://raw.githubusercontent.com/Nataliemcg18/Data/master/NASA_Surface_Temperature.csv", function(data) {

  // Labels of row and columns -> unique identifier of the column called 'group' and 'variable'
  var myGroups = d3.map(data, function(d){return d.group;}).keys()
  var myVars = d3.map(data, function(d){return d.variable;}).keys()

  // Build X scales and axis:
  var x = d3.scaleBand()
    .range([ 0, width ])
    .domain(myGroups)
    .padding(0.05);
  svg.append("g")
    .style("font-size", 15)
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x).tickSize(0))
    .select(".domain").remove()

  // Build Y scales and axis:
  var y = d3.scaleBand()
    .range([ height, 0 ])
    .domain(myVars)
    .padding(0.05);
  svg.append("g")
    .style("font-size", 15)
    .call(d3.axisLeft(y).tickSize(0))
    .select(".domain").remove()

  // Build color scale
  var myColor = d3.scaleSequential()
    .interpolator( d3.interpolateRdYlBu)
    .domain([1,100])



  // create a tooltip
  var tooltip = d3.select("#my_dataviz")
    .append("div")
    .style("opacity", 0)
    .attr("class", "tooltip")
    .style("background-color", "white")
    .style("border", "solid")
    .style("border-width", "2px")
    .style("border-radius", "5px")
    .style("padding", "5px")

  // Three function that change the tooltip when user hover / move / leave a cell
  var mouseover = function(d) {
    tooltip
      .style("opacity", 1)
    d3.select(this)
      .style("stroke", "green")
      .style("opacity", 1)
  }
  var mousemove = function(d) {
    tooltip
      .html("The exact value of this cell is: " + d.value,   )
      .style("left", (d3.mouse(this)[0]+70) + "px")
      .style("top", (d3.mouse(this)[1]) + "px")
  }
  var mouseleave = function(d) {
    tooltip
      .style("opacity", 0)
    d3.select(this)
      .style("stroke", "none")
      .style("opacity", 0.8)
  }

  // add the squares
  svg.selectAll()
    .data(data, function(d) {return d.group+':'+d.variable;})
    .enter()
    .append("rect")
      .attr("x", function(d) { return x(d.group) })
      .attr("y", function(d) { return y(d.variable) })
      .attr("rx", 4)
      .attr("ry", 4)
      .attr("width", x.bandwidth() )
      .attr("height", y.bandwidth() )
      .style("fill", function(d) { return myColor(d.value)} )
      .style("stroke-width", 4)
      .style("stroke", "none")
      .style("opacity", 0.8)
    .on("mouseover", mouseover)
    .on("mousemove", mousemove)
    .on("mouseleave", mouseleave)
})

// Add title to graph
svg.append("text")
        .attr("x", 0)
        .attr("y", -50)
        .attr("text-anchor", "left")
        .style("font-size", "22px")
        .text("A d3.js heatmap");

// Add subtitle to graph
svg.append("text")
        .attr("x", 0)
        .attr("y", -20)
        .attr("text-anchor", "left")
        .style("font-size", "14px")
        .style("fill", "grey")
        .style("max-width", 400)
        .text("A short description of the take-away message of this chart.");


</script>

最佳答案

在您的色标中,您已将域设置为 [0,100]。然而,您的值在 0 到 1.5 之间,并且您希望将它们颠倒过来

所以,这应该可以解决这个问题:

  // Build color scale
  var myColor = d3.scaleSequential()
     .interpolator( d3.interpolateRdYlBu)
     .domain([1.3,0])

为了更彻底,您可以使用 d3.max 和 d3.min 函数来计算出最小值中的最大值:

  var myColor = d3.scaleSequential()
    .interpolator( d3.interpolateRdYlBu)
    .domain([d3.max(data, d=>d.value),d3.min(data, d=>d.value)])

这是一个 jsFiddle 可以完成此操作:https://jsfiddle.net/x8zyud5t/

关于javascript - 使用 d3.js 创建热图,存在颜色问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61512078/

相关文章:

javascript - Tinymce 编辑器在初始化后只加载了 alert 语句

javascript - 上下文相关的导航栏

javascript - Angularjs slider 选择日期而不是数字

javascript - 如何连接到 HighCharts 中两个链接系列的端点

javascript - 未捕获类型错误 : Cannot read property 'curve' of undefined in D3. js

JavaScript - 如何为浏览器 GET 设置请求 header

javascript - D3.js 跳跃缩放行为

animation - 使用 D3.js 沿连续路径进行插值

javascript - 将数据格式化为新的 JavaScript 结构

javascript - 为什么在不应该存在的矩形元素之间存在空白/"overlap"?