最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Format ticks in D3 - Stack Overflow

programmeradmin5浏览0评论

I'm plotting the graphs with "number of clicks" as Y axis and "date" as X axis. Because of the large amount of data, the X axis is jumbled and couldn't display all the dates. I tried to use ticks(d3.time.months, 1) and tickFormat('%b %Y') to cur off some of the ticks. When I run the code, I got "getMonth() is not defined" for my data.

.tsv file:

date count
2013-01-01 4
2013-03-02 5

sample code:

var x = d3.scale.ordinal()
        .rangeRoundBands([0, width], .1, 0);

var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom")            
        .ticks(d3.time.months, 1)
        .tickFormat(d3.time.format('%b %Y'))

d3.tsv("data_Bar_Chart_Paino_Kristen.tsv", type, function(error, data) {
      x.domain(data.map(function(d) { return d.date; }));
      y.domain([0, d3.max(data, function(d) { return d.hits; })]);


      var temp = height + 30; //+15

        svg.append("g")
          .attr("class", "x axis")
          .attr("transform", "translate(-8," + temp + ")")
          .call(xAxis);
       svg.selectAll(".bar")
          .data(data)
          .enter().append("rect")
          .attr("class", "bar")
          .attr("x", function(d) { return x(d.date); })
          .attr("width", x.rangeBand())
          .attr("y", function(d) { return y(d.hits); })
          .attr("height", function(d) {return height - y(d.hits); });  
}

Is there a way to solve my problem and show ticks properly?

I'm plotting the graphs with "number of clicks" as Y axis and "date" as X axis. Because of the large amount of data, the X axis is jumbled and couldn't display all the dates. I tried to use ticks(d3.time.months, 1) and tickFormat('%b %Y') to cur off some of the ticks. When I run the code, I got "getMonth() is not defined" for my data.

.tsv file:

date count
2013-01-01 4
2013-03-02 5

sample code:

var x = d3.scale.ordinal()
        .rangeRoundBands([0, width], .1, 0);

var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom")            
        .ticks(d3.time.months, 1)
        .tickFormat(d3.time.format('%b %Y'))

d3.tsv("data_Bar_Chart_Paino_Kristen.tsv", type, function(error, data) {
      x.domain(data.map(function(d) { return d.date; }));
      y.domain([0, d3.max(data, function(d) { return d.hits; })]);


      var temp = height + 30; //+15

        svg.append("g")
          .attr("class", "x axis")
          .attr("transform", "translate(-8," + temp + ")")
          .call(xAxis);
       svg.selectAll(".bar")
          .data(data)
          .enter().append("rect")
          .attr("class", "bar")
          .attr("x", function(d) { return x(d.date); })
          .attr("width", x.rangeBand())
          .attr("y", function(d) { return y(d.hits); })
          .attr("height", function(d) {return height - y(d.hits); });  
}

Is there a way to solve my problem and show ticks properly?

Share Improve this question edited Jul 19, 2013 at 3:45 whileone asked Jul 19, 2013 at 3:36 whileonewhileone 2,8555 gold badges23 silver badges30 bronze badges 3
  • Are you parsing the dates first? – Adam Pearce Commented Jul 19, 2013 at 3:38
  • @AdamPearce Yes, I parsed x axis, which is date, first. – whileone Commented Jul 19, 2013 at 3:47
  • 3 You need to use a time scale to use that kind of tick formatting, i.e. d3.time.scale. – Lars Kotthoff Commented Jul 19, 2013 at 5:36
Add a ment  | 

2 Answers 2

Reset to default 5

You'll need to tell D3 that your axis is a date. Try this:

//assumes the data is sorted chronologically
var xMin = data[0].dateFieldName;
var xMax = data[data.length-1].dateFieldName;

//set the scale for the x axis
var x = d3.time.scale().domain([xMin, xMax]).range([0, width]); 

//straight from your code
var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom")            
        .ticks(d3.time.months, 1)
        .tickFormat(d3.time.format('%b %Y'))

The trick here is to use the d3.time.scale() method.

If you use the d3.time.scale() you have another problem, you can't use x.rangeBand() when you draw the rect.

发布评论

评论列表(0)

  1. 暂无评论