6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

d3.js v5 で、x軸に日付を入れた折れ線グラフを作る

Last updated at Posted at 2018-04-10

v5の簡単なサンプルが少ない

2018年4月現在、簡単なサンプルが少ないです。

やりたいことは単に「x軸に日付・y軸に値」を取ったシンプルなグラフを書きたいことだけなんですが、v3かv4向けだったり、あるいは最初のステップとしては読解が難しいサンプルだったりして、作成に時間が掛かってしまいました。

最終的に欲しかったのは、下のようなやつだったので残しておきます。

(TODO: かなり参考にさせてもらったサイトがあったのに、リンクを消失してしまったので再発見したら貼る)

折れ線グラフのサンプル

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>d3.js v5 Simple Liner Graph</title>
  <style>
  .axis { font: 14px sans-serif; }
  .line {
    fill: none;
    stroke: steelblue;
    stroke-width: 2px;
  }
  </style>
</head>
<body>
  <script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var dataset = [
    {
      date: "2007-10",
      value: 20
    },
    {
      date: "2007-11",
      value: 90
    },
    {
      date: "2007-12",
      value: 50
    },
    {
      date: "2008-01",
      value: 33
    },
    {
      date: "2008-02",
      value: 95
    },
    {
      date: "2008-03",
      value: 12
    },
    {
      date: "2008-04",
      value: 44
    },
    {
      date: "2008-05",
      value: 67
    },
    {
      date: "2008-06",
      value: 21
    },
    {
      date: "2009-12",
      value: 88
    },
  ];
  var width = 720;
  var height = 300;

  var svg = d3.select("body").append("svg").attr("width", width).attr("height", height);
  var padding = 30;

  var timeparser = d3.timeParse("%Y-%m");
  dataset = dataset.map(function(d){
    return  { date: timeparser(d.date), value:d.value } ;
  });

  var xScale = d3.scaleTime()
    .domain([d3.min(dataset, function(d){return d.date;}), d3.max(dataset, function(d){return d.date;})])
    .range([padding, width - padding]);

  var yScale = d3.scaleLinear()
    .domain([0, d3.max(dataset, function(d){return d.value;})])
    .range([height - padding, padding]);

  var axisx = d3.axisBottom(xScale)
  .ticks(10)
  .tickFormat(d3.timeFormat("%Y-%m"));
  var axisy = d3.axisLeft(yScale);

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

  svg.append("g")
    .attr("transform", "translate(" + 0 + "," + (height - padding) + ")")
    .call(axisx);

  svg.append("g")
    .attr("transform", "translate(" + padding + "," + 0 + ")")
    .call(axisy);

  svg.append("path")
    .datum(dataset)
    .attr("fill", "none")
    .attr("stroke", "steelblue")
    .attr("stroke-linejoin", "round")
    .attr("stroke-linecap", "round")
    .attr("stroke-width", 1.5)
    .attr("d", line);
  </script>
</body>
</html>

散布図のサンプル(折れ線グラフに至る直前の途中経過)

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>d3.js v5 Simple Liner Graph</title>
  <style>
  .axis { font: 14px sans-serif; }
  .line {
    fill: none;
    stroke: steelblue;
    stroke-width: 2px;
  }
  </style>
</head>
<body>
  <script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var dataset = [
    {
      date: "2007-10",
      value: 20
    },
    {
      date: "2007-11",
      value: 90
    },
    {
      date: "2007-12",
      value: 50
    },
    {
      date: "2008-01",
      value: 33
    },
    {
      date: "2008-02",
      value: 95
    },
    {
      date: "2008-03",
      value: 12
    },
    {
      date: "2008-04",
      value: 44
    },
    {
      date: "2008-05",
      value: 67
    },
    {
      date: "2008-06",
      value: 21
    },
    {
      date: "2009-12",
      value: 88
    },
  ];
  var width = 720;
  var height = 300;

  var svg = d3.select("body").append("svg").attr("width", width).attr("height", height);
  var padding = 30;

  var timeparser = d3.timeParse("%Y-%m");
  dataset = dataset.map(function(d){
    return  { date: timeparser(d.date), value:d.value } ;
  });

  var xScale = d3.scaleTime()
    .domain([d3.min(dataset, function(d){return d.date;}), d3.max(dataset, function(d){return d.date;})])
    .range([padding, width - padding]);

  var yScale = d3.scaleLinear()
    .domain([0, d3.max(dataset, function(d){return d.value;})])
    .range([height - padding, padding]);

  var axisx = d3.axisBottom(xScale)
  .ticks(10)
  .tickFormat(d3.timeFormat("%Y-%m"));
  var axisy = d3.axisLeft(yScale);

  svg.append("g")
    .attr("transform", "translate(" + 0 + "," + (height - padding) + ")")
    .call(axisx);

  svg.append("g")
    .attr("transform", "translate(" + padding + "," + 0 + ")")
    .call(axisy);

  svg.append("g")
    .selectAll("circle")
   .data(dataset)
   .enter()
   .append("circle").attr("cx", function(d) {
        return xScale(d.date);
   })
   .attr("cy", function(d) {
        return yScale(d.value);
   })
   .attr("fill", "SkyBlue")
   .attr("r", 4);
 </script>
</body>
</html>

以上

これを叩き台にしてやっとスタート地点に立てているといいな。

6
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?