Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我想以月份格式绘制x轴。。通过这个代码,我得到了一年的智慧_Javascript_D3.js - Fatal编程技术网

Javascript 我想以月份格式绘制x轴。。通过这个代码,我得到了一年的智慧

Javascript 我想以月份格式绘制x轴。。通过这个代码,我得到了一年的智慧,javascript,d3.js,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-M

样本输入

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);
});

查看轴的
勾号
-选项:

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

查看轴的
勾号
-选项:

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(d3.time.months);
必须在轴中设置:

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(d3.time.format("%b"));
下面是一个工作演示:

var数据=[
{“日期”:“2007年4月24日”},
{“日期”:“2007年4月25日”},
{“日期”:“2007年4月26日”},
{“日期”:“2007年4月27日”},
{“日期”:“2007年4月30日”},
{“日期”:“2007年5月1日”},
{“日期”:“2007年5月2日”},
{“日期”:“2007年5月3日”},
{“日期”:“2007年5月4日”},
{“日期”:“2007年5月5日”},
{“日期”:“2007年5月6日”},
{“日期”:“2007年5月7日”},
{“日期”:“2007年5月8日”},
{“日期”:“2007年5月9日”},
{“日期”:“2007年5月10日”},
{“日期”:“2007年5月11日”},
{“日期”:“2007年5月12日”},
{“日期”:“2007年5月13日”},
{“日期”:“2007年5月14日”},
{“日期”:“2007年5月15日”},
{“日期”:“2007年5月16日”}
];
变量宽度=600,高度=200,
parseDate=d3.time.format(“%d-%b-%y”).parse;
data.forEach(函数(d){
d、 日期=解析日期(d.date);
});
var svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var xScale=d3.time.scale()
.domain(d3.extent(数据,函数(d){返回d.date;}))
.范围([0,宽度-20]);
var xAxis=d3.svg.axis()
.orient(“底部”)
.scale(xScale)
.tickFormat(d3.time.format(“%b”);
svg.append(“g”)
.attr(“类”、“轴”)
.attr(“变换”、“平移(0,50)”)
.呼叫(xAxis)
.axis path、.axis line{
填充:无;
笔画:黑色;
形状渲染:边缘清晰;
}
必须在轴中设置:

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(d3.time.format("%b"));
下面是一个工作演示:

var数据=[
{“日期”:“2007年4月24日”},
{“日期”:“2007年4月25日”},
{“日期”:“2007年4月26日”},
{“日期”:“2007年4月27日”},
{“日期”:“2007年4月30日”},
{“日期”:“2007年5月1日”},
{“日期”:“2007年5月2日”},
{“日期”:“2007年5月3日”},
{“日期”:“2007年5月4日”},
{“日期”:“2007年5月5日”},
{“日期”:“2007年5月6日”},
{“日期”:“2007年5月7日”},
{“日期”:“2007年5月8日”},
{“日期”:“2007年5月9日”},
{“日期”:“2007年5月10日”},
{“日期”:“2007年5月11日”},
{“日期”:“2007年5月12日”},
{“日期”:“2007年5月13日”},
{“日期”:“2007年5月14日”},
{“日期”:“2007年5月15日”},
{“日期”:“2007年5月16日”}
];
变量宽度=600,高度=200,
parseDate=d3.time.format(“%d-%b-%y”).parse;
data.forEach(函数(d){
d、 日期=解析日期(d.date);
});
var svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var xScale=d3.time.scale()
.domain(d3.extent(数据,函数(d){返回d.date;}))
.范围([0,宽度-20]);
var xAxis=d3.svg.axis()
.orient(“底部”)
.scale(xScale)
.tickFormat(d3.time.format(“%b”);
svg.append(“g”)
.attr(“类”、“轴”)
.attr(“变换”、“平移(0,50)”)
.呼叫(xAxis)
.axis path、.axis line{
填充:无;
笔画:黑色;
形状渲染:边缘清晰;
}