Home > Blockchain >  How to set up the scale with d3js?
How to set up the scale with d3js?

Time:12-31

I'm trying to build a bar chart but keep getting this error:

SyntaxError: missing ) after argument list

It says the problem is in the line of the .range() method but I don't see it. Everything looks fine to me.

I downloaded the dataset from here: https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json

let url = "data.json";

var svg = d3.select('body')
    .append('svg')
    .attr('width', 900)
    .attr('height', 460);

var yScale;

var arr = [];
    d3.json(url).then(function(data) {
        for (let i = 0; i < data.data.length; i  ) arr[i] = data.data[i];
        }).then(() => 

          yScale = d3.scaleLinear()
                     .domain([0, d3.max(arr, (d) => d[1])])
                     .range([460, 0]);

    svg.selectAll('rect')
        .data(arr)
        .enter()
        .append('rect')
        .attr('fill', 'blue')
        .attr('height', d => d[1])
        .attr('width', 3)
        .attr('x', (d, i) => 4 * i)
        .attr('y', (d, i) => yScale(d[1]))

        );
        <script src="https://d3js.org/d3.v6.min.js"></script>

CodePudding user response:

With multiple lines of code, your arrow function needs curly braces

d3.json(url).then(function(data) {
    for (let i = 0; i < data.data.length; i  ) arr[i] = data.data[i];
    }).then(() => {

      yScale = d3.scaleLinear()
                 .domain([0, d3.max(arr, (d) => d[1])])
                 .range([460, 0]);

    svg.selectAll('rect')
    .data(arr)
    .enter()
    .append('rect')
    .attr('fill', 'blue')
    .attr('height', d => d[1])
    .attr('width', 3)
    .attr('x', (d, i) => 4 * i)
    .attr('y', (d, i) => yScale(d[1]))

   });
  • Related