Kyle
Kyle

Reputation: 1183

Creating a line graph from array of data objects

I'm trying to learn D3 and am having problems creating a simple line graph from an array of data objects.

I have an array of data objects that look like this...

[
    {date: "03/04/15", rain: "1.2"},
    {date: "03/05/15", rain: "2.3"},
    {date: "03/06/15", rain: "0.0"},
    {date: "03/07/15", rain: "4.2"},
    {date: "03/08/15", rain: "0.3"},
    {date: "03/09/15", rain: "0.0"}
]

I've tried following a simple tutorial that creates a line graph, but when I plug in the data, neither the x axes or line will display. Does this have to do with date formatting?

I have an example up on JS Bin.

I really don't understand what the problem is, please help!

Upvotes: 0

Views: 1239

Answers (2)

Miguel
Miguel

Reputation: 20633

@Satyajit's is right. Here is an example:

	var lineData = 	[
						{date: "03/04/15", rain: "1.2"},
						{date: "03/05/15", rain: "2.3"},
						{date: "03/06/15", rain: "0.0"},
						{date: "03/07/15", rain: "4.2"},
						{date: "03/08/15", rain: "0.3"},
						{date: "03/09/15", rain: "0.0"}
					].map(function(d) {
                      return {
                        date: new Date(Date.parse(d.date)),
                        rain: d.rain
                      };
                    });


  var vis = d3.select("#visualisation"),
    WIDTH = 600,
    HEIGHT = 250,
    MARGINS = {
      top: 20,
      right: 20,
      bottom: 20,
      left: 50
    },
    xRange = d3.time.scale()
   .range([MARGINS.left, WIDTH - MARGINS.right])
  .domain([d3.min(lineData, function (d) {
        return d.date;
      }),
      d3.max(lineData, function (d) {
        return d.date;
      })
    ]),

    yRange = d3.scale.linear()
  .range([HEIGHT - MARGINS.top, MARGINS.bottom])
  .domain([d3.min(lineData, function (d) {
        return d.rain;
      }),
      d3.max(lineData, function (d) {
        return d.rain;
      })
    ]),

    xAxis = d3.svg.axis()
      .scale(xRange)
      .tickSize(3),

    yAxis = d3.svg.axis()
      .scale(yRange)
      .tickSize(3)
      .orient("left");

  vis.append("svg:g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
    .call(xAxis);

  vis.append("svg:g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + (MARGINS.left) + ",0)")
    .call(yAxis);

  var lineFunc = d3.svg.line()
  .x(function (d) {
    return xRange(d.date);
  })
  .y(function (d) {
    return yRange(d.rain);
  })
  .interpolate('linear');

vis.append("svg:path")
  .attr("d", lineFunc(lineData))
  .attr("stroke", "blue")
  .attr("stroke-width", 2)
  .attr("fill", "none");
<script src="http://d3js.org/d3.v3.min.js"></script>
<svg id="visualisation" width="600" height="400"></svg>	

Upvotes: 1

Satyajit
Satyajit

Reputation: 3859

The X axis should be d3.time.scale and since your date property are strings you will have to convert it to proper Date objects by doing new Date();

Upvotes: 2

Related Questions