删除特定的图形网格线d3js

时间:2016-04-05 19:42:05

标签: d3.js linegraph gridlines

enter image description here

我是d3js的新手,我想手动安排网格线,我将分享我得到的结果和我想要的结果。请检查截图,有两个不同的图像。 第一个图像是我从下面的代码得到的输出,但我希望我的结果像第二个图像。

enter image description here

[<!doctype html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>Line Chart</title>
<style>

  .axis path,
  .axis line{
    fill: none;
    stroke: black;
  }

  .line{
    fill: none;
    stroke: blue;
    stroke-width: 2px;
  }

  .tick text{
    font-size: 12px;
  }

  .tick line{
    opacity: 1;
  }

</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>

var margin = {top: 20, right: 100, bottom: 30, left: 100},
    width = 350 ;
    height = 350 ;


var dataset = \[
  {x: 0, y: 0},
  {x: 1, y: 0.5},
  {x: 2, y: 1},
  {x: 3, y: 1.5},
  {x: 4, y: 2},
  {x: 5, y: 2.5},
  {x: 6, y: 3},
\];

var xScale = d3.scale.linear()
    .domain(\[0, d3.max(dataset, function(d){ return d.x; })\])
    .range(\[0, width\]);

var yScale = d3.scale.linear()
    .domain(\[0, d3.max(dataset, function(d){ return d.y; })\])
    .range(\[height, 0\]);

var xAxis = d3.svg.axis()
    .scale(xScale)
    .orient("bottom")
    .innerTickSize(-height)
    .outerTickSize(0)
    .tickPadding(10);

var yAxis = d3.svg.axis()
    .scale(yScale)
    .orient("left")
    .innerTickSize(-width)
    .outerTickSize(0)
    .tickPadding(10);

var line = d3.svg.line()
    .x(function(d) { return xScale(d.x); })
    .y(function(d) { return yScale(d.y); });

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

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)

  svg.append("path")
      .data(\[dataset\])
      .attr("class", "line")
      .attr("d", line);

</script>
</body>
</html>]

1 个答案:

答案 0 :(得分:0)

只需设置x轴生成器中的刻度数:

var xAxis = d3.svg.axis()
    .scale(xScale)
    .orient("bottom")
    .innerTickSize(-height)
    .outerTickSize(0)
    .tickPadding(10)
    .ticks(5);

这是一个演示:

var margin = {top: 20, right: 100, bottom: 30, left: 100},
    width = 350 ;
    height = 350 ;


var dataset = [
  {x: 0, y: 0},
  {x: 1, y: 0.5},
  {x: 2, y: 1},
  {x: 3, y: 1.5},
  {x: 4, y: 2},
  {x: 5, y: 2.5},
  {x: 6, y: 3},
];

var xScale = d3.scale.linear()
    .domain([0, d3.max(dataset, function(d){ return d.x; })])
    .range([0, width]);

var yScale = d3.scale.linear()
    .domain([0, d3.max(dataset, function(d){ return d.y; })])
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(xScale)
    .orient("bottom")
    .innerTickSize(-height)
    .outerTickSize(0)
    .tickPadding(10)
		.ticks(5);

var yAxis = d3.svg.axis()
    .scale(yScale)
    .orient("left")
    .innerTickSize(-width)
    .outerTickSize(0)
    .tickPadding(10);

var line = d3.svg.line()
    .x(function(d) { return xScale(d.x); })
    .y(function(d) { return yScale(d.y); });

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

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)

  svg.append("path")
      .data([dataset])
      .attr("class", "line")
      .attr("d", line);
  .axis path,
  .axis line{
    fill: none;
    stroke: black;
  }

  .line{
    fill: none;
    stroke: blue;
    stroke-width: 2px;
  }

  .tick text{
    font-size: 12px;
  }

  .tick line{
    opacity: 1;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>