[英]CSV color data not rendering
I'm trying to color the circles per a .csv data, column "COLOR". 我正在尝试根据.csv数据,“COLOR”列为每个圆圈着色。 The "COLOR" includes "red", "yellow" , "green" -but right now the colors are not transferring... just the default black...
“颜色”包括“红色”,“黄色”,“绿色” - 但是现在颜色没有转移...只是默认的黑色......
var col = function(d) {return d.COLOR};
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 15)
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", col)
.style("opacity", ".5")
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d["Cereal Name"] + "<br/> (" + xValue(d)
+ ", " + yValue(d) + ")")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
data.forEach(function(d) {
d.POPULATION = +d.POPULATION;
d.REVENUE = +d.REVENUE;
d.COLOR = +d.COLOR;
Your COLOR
values in your CSV contain quotation marks "
which will be part of the parsed strings in data
. Therefore, you end up with attribute values like fill=""yellow""
which is not valid. Hence, the black default color. CSV中的
COLOR
值包含引号"
,它将成为data
已解析字符串的一部分。因此,最终会出现无效的fill=""yellow""
等属性值。因此,黑色默认颜色。
One way around this might be to get rid of the quotation marks in the CSV itself. 解决这个问题的一种方法可能是删除CSV本身中的引号。 If this is not feasible, you might adjust your color accessor function
col
to something like the following: 如果这不可行,您可以将颜色访问器功能
col
调整为如下所示:
var col = function(d) {return d.COLOR.substring(1, d.COLOR.length - 1)};
Have a look at this working demo: 看看这个工作演示:
var data = [{ COLOR: '"yellow"'}]; var col = function(d) { return d.COLOR.substring(1, d.COLOR.length - 1); }; d3.select("body").append("svg") .selectAll("circle") .data(data) .enter().append("circle") .attr("cx", 100) .attr("cy", 100) .attr("r", 10) .attr("fill", col);
<script src="https://d3js.org/d3.v4.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.