为了账号安全,请及时绑定邮箱和手机立即绑定

D3.js 带颜色的热图

D3.js 带颜色的热图

慕桂英3389331 2022-07-21 22:13:37
我正在尝试为我的热图添加色阶。我特别想使用 d3.schemeRdYlBu 这个配色方案,但我很难实现它。目前它只是变黑。我也有一个悬停功能,所以我希望它仍然可以工作,但我更关心的是让颜色正常工作。显然,较低的数字是蓝色的,较高的数字是红色的,表示温度<!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 src="https://d3js.org/d3-color.v1.min.js"></script><script src="https://d3js.org/d3-interpolate.v1.min.js"></script><script src="https://d3js.org/d3.v4.js"></script><script>// set the dimensions and margins of the graphvar 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 pagevar 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 datad3.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()
查看完整描述

2 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

您可以使用箭头函数而不是常规函数来使用您自己的绑定this来访问myColor变量。


<!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 src="https://d3js.org/d3-color.v1.min.js"></script>

<script src="https://d3js.org/d3-interpolate.v1.min.js"></script>


<script src="https://d3js.org/d3.v4.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.schemeRdYlBu[3][2];


      // 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", (d) => {

          return myColor;

        })

        .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>



查看完整回答
反对 回复 2022-07-21
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

这是获得所需结果的另一种方法

    var myColor = d3.scaleSequential()
    .interpolator( d3.interpolateRdYlBu)
    .domain([1.37, -.81])


查看完整回答
反对 回复 2022-07-21
  • 2 回答
  • 0 关注
  • 198 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号