【发布时间】:2021-05-29 12:30:18
【问题描述】:
所以我在 d3.js 中完成了这个颜色编码的网格。有些方块应该是一半一种颜色,一半是另一种颜色。我已经根据其他示例布置了渐变。但是当我调用 url(#gridfill7) 时,它不会返回渐变 - 使用渐变的正方形是空的。
看其他问题,可能是浏览器的bug,但我不能确定。
这是一个演示问题的 sn-p:
var gridvis = null;
function highlightGrid() {
var gridData = [];
var squareSize = 30;
var squarePad =5;
var numPerRow = 9;
var margin = {top: 10, right: 30, bottom: 30, left: 60};
var width = 750 - margin.left - margin.right;
var height = 520 - margin.top - margin.bottom;
var d = d3.csvParse(d3.select("pre").remove().text());
for (var i = 0; i < d.length; i++) {
// console.log(d[i].report_num);
// console.log(d[i].platform_medium);
d[i].report_num = +d[i].report_num;
d[i].platform_medium = +d[i].platform_medium;
d[i].col = i % numPerRow;
d[i].x = d[i].col * (squareSize + squarePad);
d[i].row = Math.floor(i/numPerRow);
d[i].y = d[i].row * (squareSize + squarePad);
gridData.push(d[i]);
}
let platformData = gridData;
//console.log(gridData);
var gridvis = d3.select("#chart2")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// square grid
// @v4 Using .merge here to ensure
// new and old data have same attrs applied
//console.log(platformData);
var squares = gridvis.selectAll('.square').data(platformData, function (d) { return d.report_num; });
var gridFill7 = gridvis
.append("linearGradient")
.attr("id", "gridFill7")
.attr("x1", "0%")
.attr("x2", "0%")
.attr("y1", "0%")
.attr("y2", "100%")//since its a vertical linear gradient
.attr("gradientUnits", "userSpaceOnUse");
gridFill7.append("stop")
.style("offset", "50%")
.style("stop-color", "#0652DD")
.style("stop-opacity", 1);
gridFill7.append("stop")
.style("offset", "100%")
.style("stop-color", "#C4E538")
.style("stop-opacity", 1);
var gridFill8 = gridvis
.append("linearGradient")
.attr("id", "gridFill8")
.attr("x1", "0%")
.attr("x2", "0%")
.attr("y1", "0%")
.attr("y2", "100%")//since its a vertical linear gradient
.attr("gradientUnits", "userSpaceOnUse");
gridFill8.append("stop")
.style("offset", "50%")
.style("stop-color", "#0652DD")
.style("stop-opacity", 1);
gridFill8.append("stop")
.style("offset", "100%")
.style("stop-color", "#EE5A24")
.style("stop-opacity", 1);
var squaresE = squares.enter()
.append('rect')
.classed('square', true);
var squares = squares.merge(squaresE)
.attr('width', squareSize)
.attr('height', squareSize)
.attr('fill', "#fff")
.classed('fill-square', function (d) { return d.platform_medium; })
.attr('x', function (d) { return d.x;})
.attr('y', function (d) { return d.y;})
.attr('opacity', 1);
gridvis.selectAll('.fill-square')
.transition()
.duration(800)
.attr('opacity', 1.0)
.attr('fill', function (d) {
if (d.platform_medium===1) {return "#0652DD";}
else if (d.platform_medium===2) {return "#9980FA";}
else if (d.platform_medium===3) {return "#C4E538";}
else if (d.platform_medium===4) {return "#ED4C67";}
else if (d.platform_medium===5) {return "#F79F1F";}
else if (d.platform_medium===6) {return "#EE5A24";}
else if (d.platform_medium===7) {return "url(#gridfill7)";}
else if (d.platform_medium===8) {return "url(#gridfill8)";}
});
}
highlightGrid();
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>
<div id="chart2"></div>
<pre>report_num,covid_related,platform_medium
1,0,1
2,0,1
3,0,1
4,0,1
5,0,1
6,0,3
7,0,1
8,0,3
9,0,1
10,0,3
11,0,1
12,0,3
13,1,2
14,1,1
15,1,3
16,1,1
17,1,1
18,1,1
19,1,1
20,1,1
21,1,1
22,1,1
23,1,1
24,0,1
25,0,7
26,1,1
27,1,1
28,1,1
29,1,1
30,1,1
31,1,1
32,1,1
33,1,1
34,1,1
35,1,1
36,0,1
37,0,1
38,0,2
39,0,7
40,1,1
41,1,1
42,1,1
43,1,1
44,1,1
45,1,1
46,0,5
47,0,5
48,0,1
49,0,1
50,0,1
51,0,1
52,1,1
53,1,1
54,1,1
55,0,1
56,0,5
57,0,1
58,0,1
59,0,1
60,0,1
61,0,7
62,0,1
63,0,1
64,0,1
65,0,1
66,0,1
67,1,5
68,1,1
69,1,1
70,1,3
71,1,3
72,1,1
73,1,1
74,1,7
75,1,8
76,1,1
77,1,4
78,1,2</pre>
【问题讨论】:
标签: javascript svg d3.js