简体   繁体   English

如何创建图例d3?

[英]How to create legend d3?

I took this example from internet:It works fine except that it creates only two rects. 我从互联网上举了一个例子:除了只创建两个矩形外,它工作正常。 I also fail to see why those rectangles are created since I would expect only one. 我也看不到为什么要创建这些矩形,因为我希望只有一个。 I created this fiddle which seem not to work. 我创建了这个小提琴,似乎不起作用。 https://jsfiddle.net/q4py7wxg/ . https://jsfiddle.net/q4py7wxg/ Please help. 请帮忙。

var color = d3.scale.linear()
          .range(["rgb(213,222,217)", "rgb(69,173,168)", "rgb(84,36,55)", "rgb(217,91,67)"]);

        var legendText = ["0 - 1000", "1000 - 2000", "2000 - 3000", "3000 - 5000"];
        var legend = d3.select("body").append("svg")
            .attr("class", "legend")
            .attr("width", 140)
            .attr("height", 200)
            .selectAll("g")
            .data(color.domain().slice().reverse())
            .enter()
            .append("g")
            .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

        legend.append("rect")
              .attr("width", 18)
              .attr("height", 18)
              .style("fill", color);

        legend.append("text")
              .data(legendText)
              .attr("x", 24)
              .attr("y", 9)
              .attr("dy", ".35em")
              .text(function(d) { return d; });

You wonder why your code... 您想知道为什么您的代码...

Creates only two rects. 仅创建两个矩形。

This is the reason: in D3, if you don't set the domain, the default domain is: 原因是:在D3中,如果您未设置域,则默认域为:

[0, 1]

And that's why you're seeing only two rectangles. 这就是为什么您只看到两个矩形的原因。 Let's see it in this snippet, where color.domain().slice().reverse() is the data you're passing in your code: 让我们在此代码段中查看它,其中color.domain().slice().reverse()是您在代码中传递的数据:

 //look Ma, no domain! var color = d3.scaleLinear(); console.log(color.domain().slice().reverse()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.min.js"></script> 

As you can see, your data is an array with only two elements. 如您所见,您的数据是一个只有两个元素的数组。

Solution : first, change your domain: 解决方案 :首先,更改您的域:

.domain(d3.range(4))

Which is the same of: 相同的是:

.domain([0,1,2,3])

And change your data accordingly: 并相应地更改数据:

.data(d3.range(4))

PS: do not change your data without changing your domain, this will extrapolate the colours, that is, the scale will return colours that are not in the range. PS:在不更改域的情况下不要更改数据,这会推断颜色,即比例尺将返回不在此范围内的颜色。

PS2: you're using the wrong scale for the task. PS2:您为任务使用了错误的比例。 It's working (kind of), but it's a strange code to any seasoned d3 coder. 它可以工作,但对于任何经验丰富的d3编码器来说,这都是一个奇怪的代码。 Consider using scales with discrete ranges, like scaleQuantize , scaleQuantile or scaleThreshold . 考虑使用具有离散范围的标尺,例如scaleQuantizescaleQuantilescaleThreshold

Here is the fiddle: https://jsfiddle.net/9oxu4xyh/ 这是小提琴: https : //jsfiddle.net/9oxu4xyh/

Your are using the wrong version of D3, try d3.scaleLinear(). 您使用的D3版本不正确,请尝试d3.scaleLinear(). instead of d3.scale.linear() 而不是d3.scale.linear()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM