What scale should be used to represent years only along the x axis in d3 js

I designed the area chart for the year (x axis) and revenue (y axis) in D3 Js. The data is as follows:

localData=[
        {"Revenue":"4.5","Year":"2011"},
        {"Revenue":"5.5","Year":"2010"},
        {"Revenue":"7.0","Year":"2012"},
        {"Revenue":"6.5","Year":"2013"}
       ]

I want the year along the x axis and the income along the y axis for the area chart. I am currently using the timeline for the x axis, but I do not know how to use it since I do not have a date format. I have only years of submission.

My current code is:

 var margin = { top: 20, right: 20, bottom: 30, left: 50 },
                width = 500 - margin.left - margin.right,
                height = 300 - margin.top - margin.bottom;
            var parseDate = d3.time.format("%Y").parse;

            var x = d3.time.scale()
                      .range([0, width]);


            var y = d3.scale.linear()
                .range([height, 0]);
            var xAxis = d3.svg.axis()
                            .scale(x)
                            .orient("bottom");

            var yAxis = d3.svg.axis()
                .scale(y)
                .orient("left");


            var area = d3.svg.area()
                .x(function (d) { return x(d.Year); })
                .y0(height)
                .y1(function (d) { return y(d.Revenue); });
            $("#chartArea").html("");
            var svg = d3.select("#chartArea").append("svg")
                        .attr("width", width + margin.left + margin.right)
                        .attr("height", height + margin.top + margin.bottom)
                      .append("g")
                        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
            x.domain(d3.extent(localData, function (d) { return d.Year; }));
            y.domain([0, d3.max(localData, function (d) { return d.Revenue; })]);
            svg.append("path")
                 .datum(localData)
                 .attr("class", "area")
                 .attr("d", area)
                .attr("fill",color);
            svg.append("g")
                      .attr("class", "x axis")
                      .attr("transform", "translate(0," + height + ")")
                      .call(xAxis);

            svg.append("g")
                .attr("class", "y axis")
                .call(yAxis)
            .append("text")
              .attr("transform", "rotate(-90)")
              .attr("y", 6)
              .attr("dy", ".71em")
              .style("text-anchor", "end")
              .text("Revenue (M)");

I am currently getting my X axis as .011, .012,013, .014 I need, like 2011,2012,2013,2014 I am new to D3 js, so dnt knows a lot about how to use the scales. Please help someone ... Thanks in advance.

+4
source share
2 answers

- x Axis:

  var xAxis = d3.svg.axis()
              .scale(x)
              .orient("bottom")
              .tickFormat(d3.time.format("%Y")); // <-- format
+3

v4 :

d3.axisBottom(x).ticks(d3.timeYear.every(1))
+2

Source: https://habr.com/ru/post/1606178/


All Articles