我想用月份格式绘制x轴..通过这段代码我得到了明智的一年

时间:2016-09-30 10:39:24

标签: javascript d3.js

示例输入

24-Apr-07   93.24
25-Apr-07   95.35
26-Apr-07   98.84
27-Apr-07   99.92
30-Apr-07   99.80
1-May-07    99.47
2-May-07    100.39
3-May-07    100.40
4-May-07    100.81
7-May-07    103.92
8-May-07    105.06
9-May-07    106.88
10-May-07   107.34
11-May-07   108.74
14-May-07   109.36
15-May-07   107.52
16-May-07   107.34  


var margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = 900 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var formatDate = d3.time.format("%d-%b-%y");

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 line = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.close); });

var svg = d3.select('#JsonBarChart').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 + ")");

d3.tsv("data2.tsv",function(error, data) {
  if (error) throw error;
  data.forEach(function(d) {
        d.date = formatDate.parse(d.date);
        d.close = +d.close;
    });

  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain(d3.extent(data, function(d) { return d.close; }));

  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("Price ($)");

  svg.append("path")
      .datum(data)
      .attr("class", "line")
      .attr("d", line);
});

2 个答案:

答案 0 :(得分:1)

查看轴的ticks - 选项:

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(d3.time.months);

答案 1 :(得分:1)

您必须在轴上设置tickFormat

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(d3.time.format("%b"));

这是一个有效的演示:



var data = [
  {"date": "24-Apr-07"},
  {"date": "25-Apr-07"},
  {"date": "26-Apr-07"},
  {"date": "27-Apr-07"},
  {"date": "30-Apr-07"},
  {"date": "01-May-07"},
  {"date": "02-May-07"},
  {"date": "03-May-07"},
  {"date": "04-May-07"},
  {"date": "05-May-07"},
  {"date": "06-May-07"},
  {"date": "07-May-07"},
  {"date": "08-May-07"},
  {"date": "09-May-07"},
  {"date": "10-May-07"},
  {"date": "11-May-07"},
  {"date": "12-May-07"},
  {"date": "13-May-07"},
  {"date": "14-May-07"},
  {"date": "15-May-07"},
  {"date": "16-May-07"}
];

var width = 600, height = 200, 
        parseDate = d3.time.format("%d-%b-%y").parse;

data.forEach(function(d){
    d.date = parseDate(d.date);
});
        
var svg = d3.select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);    
            
var xScale = d3.time.scale()
    .domain(d3.extent(data, function(d) { return d.date; }))
    .range([0, width - 20]);
        
var xAxis = d3.svg.axis()
    .orient("bottom")
    .scale(xScale)
    .tickFormat(d3.time.format("%b"));
  
svg.append("g")
    .attr("class", "axis")  
    .attr("transform", "translate(0,50)")
    .call(xAxis);

.axis path, .axis line {
	fill: none;
	stroke: black;
	shape-rendering: crispEdges;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;