Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the size of a d3 symbol?

I can't figure out how to set the size of my d3 symbols in this:

        legendRectE
            .append('path')
            .attr("d", d3.svg.symbol().type((d) => { return d[2] }))
            .style("fill", function (d) {
                return d[1];
            })
            .attr('stroke', 'black');

I tried to use .size() like this, but it didn't work:

        legendRectE
            .append('path')
            .attr("d", d3.svg.symbol().type((d) => { return d[2] }))
            .style("fill", function (d) {
                return d[1];
            })
            .attr('stroke', 'black')
            .size(20*20);

When I try using .size() like that, I get an error: [ts] Supplied parameters do not match any signature of call target.

So how can I set the size of my symbols in this case?

like image 672
Marc Pincince Avatar asked Mar 10 '17 14:03

Marc Pincince


People also ask

What format does d3 use to create graphics?

D3 uses SVG to create and modify the graphical elements of the visualization. Because SVG has a structured form, D3 can make stylistic and attribute changes to the shapes being drawn.

What is the correct syntax to draw a circle in d3?

var circle = d3. selectAll("circle"); With a selection, we can make various changes to selected elements.

How to adding symbols to the chart in d3 js?

Built-in Symbols To use them, assign them in the . type(symbol) method on the symbol generator: var square = d3. symbol().

How do you make a triangle in d3 JS?

I have the following code that draws a triangle in d3: var trianglePoints = xScale(3) + ' ' + yScale(18) + ', ' + xScale(1) + ' ' + yScale(0) + ', ' + xScale(12) + ' ' + yScale(3) + ' ' + xScale(12) + ', ' + yScale(3) + ' ' + xScale(3) + ' ' + yScale(18); console. log(trianglePoints); svg. append('polyline') .


1 Answers

You have to define the size in the symbol generator (the d3.svg.symbol function):

d3.svg.symbol().size(size).type(foo);
//size here-----------^

Here is a demo:

var svg = d3.select("svg");

var sizes = ["10", "50", "100", "200", "500", "1000", "2000"];

sizes.forEach((d, i) => {
  svg.append('path')
    .attr("transform", "translate(" + (10 + (i+1) * (i+1) * 5) + ",100)")
    .attr("d", d3.svg.symbol().size(d).type("diamond"))
    .style("fill", "gold")
    .attr('stroke', 'black');
});
<script src="https://d3js.org/d3.v3.js"></script>
<svg></svg>
like image 100
Gerardo Furtado Avatar answered Nov 14 '22 22:11

Gerardo Furtado