javascript - 白天时间热图 - 颜色未显示

标签 javascript jquery css d3.js svg

这是我的 d3.js 热图代码,问题出在颜色上。从 http://bl.ocks.org/tjdecke/5558084 获取代码并将其修改为从 php 读取,该 php 以 json 格式回显数据。 因此,修改后,我得到了一个热图,其中所有网格均为黑色,并带有一些白色间隙。

我是 d3 图表的新手,如果您在代码中发现任何小错误,请原谅。

enter image description here

<!DOCTYPE html>
<meta charset="utf-8">
<html>
  <head>
    <style>
      rect.bordered {
        stroke: #E6E6E6;
        stroke-width:2px;   
      }

      text.mono {
        font-size: 9pt;
        font-family: Consolas, courier;
        fill: #aaa;
      }

      text.axis-workweek {
        fill: #000;
      }

      text.axis-worktime {
        fill: #000;
      }
    </style>
    <script src="http://d3js.org/d3.v3.js"></script>
  </head>
  <body>
    <div id="chart"></div>
    
    <script type="text/javascript">
      var margin = { top: 50, right: 0, bottom: 100, left: 30 },
          width = 960 - margin.left - margin.right,
          height = 430 - margin.top - margin.bottom,
          gridSize = Math.floor(width / 24),
          legendElementWidth = gridSize*2,
          buckets = 9,
          colors = ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"], // alternatively colorbrewer.YlGnBu[9]
          days = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
          times = ["1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a", "12a", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p", "12p"];
         // datasets = ["data1.tsv", "data2.tsv"];


	
      d3.json("heatmaptry2.php", function(error, data) {


		data.forEach(function(d) {
		console.log(d);
		day= +d.day; 
		hour= +d.hour;
		value= +d.value; 
		});	
	  var colorScale = d3.scale.quantile();
	
      var svg = d3.select("#chart").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 + ")");

      var dayLabels = svg.selectAll(".dayLabel")
          .data(days)
          .enter().append("text")
            .text(function (d) { return d; })
            .attr("x", 0)
            .attr("y", function (d, i) { return i * gridSize; })
            .style("text-anchor", "end")
            .attr("transform", "translate(-5," + gridSize / 1.5 + ")")
            .attr("class", function (d, i) { return ((i >= 0 && i <= 4) ? "dayLabel mono axis axis-workweek" : "dayLabel mono axis"); });

      var timeLabels = svg.selectAll(".timeLabel")
          .data(times)
          .enter().append("text")
            .text(function(d) { return d; })
            .attr("x", function(d, i) { return i * gridSize; })
            .attr("y", 0)
            .style("text-anchor", "middle")
            .attr("transform", "translate(" + gridSize / 2 + ", -5)")
            .attr("class", function(d, i) { return ((i >= 7 && i <= 16) ? "timeLabel mono axis axis-worktime" : "timeLabel mono axis"); });
    
	  var heatMap = svg.selectAll(".hour")
          .data(data)
          .enter().append("rect")
          .attr("x", function(d) { return (d.hour) * gridSize; })
          .attr("y", function(d) { return (d.day ) * gridSize; })
          .attr("rx", 4)
          .attr("ry", 4)
          .attr("class", "hour bordered")
          .attr("width", gridSize)
          .attr("height", gridSize)
          .style("fill", colors[0]);

      heatMap.transition().duration(1000)
          .style("fill", function(d) { return colorScale(d.value); });

      heatMap.append("title").text(function(d) { return d.value; });
     heatMap.exit().remove();     

	 var legend = svg.selectAll(".legend")
              .data([0].concat(colorScale.quantiles()), function(d) { return d; });

          legend.enter().append("g")
              .attr("class", "legend");

          legend.append("rect")
            .attr("x", function(d, i) { return legendElementWidth * i; })
            .attr("y", height)
            .attr("width", legendElementWidth)
            .attr("height", gridSize / 2)
            .style("fill", function(d, i) { return colors[i]; });

          legend.append("text")
            .attr("class", "mono")
            .text(function(d) { return "≥ " + Math.round(d); })
            .attr("x", function(d, i) { return legendElementWidth * i; })
            .attr("y", height + gridSize);

          legend.exit().remove();

        });  
      

      
    </script>
  </body>
</html>

最佳答案

而不是这个:

var colorScale = d3.scale.quantile();

应该是(您没有设置域)

 var colorScale = d3.scale.quantile()
              .domain([0, buckets - 1, d3.max(data, function (d) { return d.value; })])
              .range(colors);

工作代码here

关于javascript - 白天时间热图 - 颜色未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36301945/

相关文章:

php - 将 php 变量显示到 JavaScript 弹出框中

javascript - 根据父级的当前宽度将元素附加到父级和位置

jquery - 如何将元素定义为 "this"

javascript 表格技巧隐藏和显示点击按钮的除法

javascript - ruby 变量在 driver.execute_script 之后返回 nil

尝试删除第一个分隔符时不显示 css 菜单分隔符

javascript - 有没有一种方法可以在 Google 的 Materialise CSS 中创建垂直旋转木马而无需自定义构建?

Javascript for循环添加 "n"个选项元素?

jquery - 光滑的旋转木马图像不垂直适合屏幕且不垂直居中

javascript - CSS/JS 引用差异,主文件夹与子文件夹