0

Similar to question here, I've been working on the same thing as in the last question. My question now is similar to the link, but is more about the implementation. When I run my code, I get an error in my log that says "TypeError: x.ticks is not a function". This is the piece of code it refers to:

svg.selectAll("g.grid")
    .data(y.ticks()).enter()
    .append("g").attr("class", "grid")
    .selectAll("rect")
        .data(x.ticks())
        .enter()
        .append("rect")
        .attr("x", function(d, i, j) {
                return xScale(j);
        })
         .attr("y", function(d, i, j) {
                return yScale(i);
        })
        .attr("width", xScale.rangeBand())
        .attr("height", yScale.rangeBand())
         .attr("fill", function(d, i) {
              return ((i) % 2) == 1 ? "green" : "blue";
         });

This code works perfectly well in this fiddle, but gives me an error while running it in my code, as seen here. Any help?

Community
  • 1
  • 1
Alexey Ayzin
  • 209
  • 2
  • 21

2 Answers2

1

In your example you are trying to call ticks on an ordinal scale instead of a linear scale. Utilising rangeBoundBands, like in the question you've linked to, is probably the way to go.

keoghpe
  • 406
  • 5
  • 18
1

Your problem is you're using an ordinal scale. D3 makes you use a linear scale if you want to call .ticks(), otherwise it throws an error