0

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!

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

@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>	

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.