pr338
pr338

Reputation: 9170

Is there a way to parse times and be able to plot them directly using javascript and d3?

How can I make a scatterplot plotting time of day against date, which looks like this in using javascript and d3? The problem I am having is formatting the time of day data and axis. Input of the date is in the y column format below.

Sample data:

Day         Time
5-Feb-16    21:35:00
5-Feb-16    19:15:00
11-Dec-15   21:42:00
21-Jul-15   11:00:00

enter image description here

Code below inspired by: http://bl.ocks.org/d3noob/38744a17f9c0141bcd04

What I have so far:

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */

body { font: 12px Arial;}

path { 
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}

</style>
<body>

<!-- load the d3.js library -->    
<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.Day); })
    .y(function(d) { return y(d.Time); });

// Adds the svg canvas
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 + ")");

// Get the data
d3.csv("data.csv", function(error, data) {
    data.forEach(function(d) {
        console.log(d.Time);
        d.Day = parseDate(d.Day);
        d.Time = +d.Time;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.Day; }));
    y.domain([0, d3.max(data, function(d) { return d.Time; })]);

    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(data));

    // Add the scatterplot
    svg.selectAll("dot")
        .data(data)
      .enter().append("circle")
        .attr("r", 3.5)
        .attr("cx", function(d) { return x(d.Day); })
        .attr("cy", function(d) { return y(d.Time); });

    // Add the X Axis
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    // Add the Y Axis
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis);

});

</script>
</body>

My output:

enter image description here

Is there a way to parse times and be able to plot them directly using javascript and d3? Or do I need to convert them to decimal numbers like 12:30 = 12.5 to plot them?

Upvotes: 1

Views: 64

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102208

If you're using time in the y scale, you should parse the Time column as well:

var parseDate = d3.time.format("%d-%b-%y").parse;
var parseTime = d3.time.format("%H:%M:%S").parse;

Here is your code with just 4 points (the ones of the CSV you copy/pasted in your question):

var margin = {
    top: 30,
    right: 20,
    bottom: 30,
    left: 80
  },
  width = 600 - margin.left - margin.right,
  height = 270 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse;
var parseTime = d3.time.format("%H:%M:%S").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.time.scale().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
  .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
  .orient("left");

// Define the line
var valueline = d3.svg.line()
  .x(function(d) {
    return x(d.Day);
  })
  .y(function(d) {
    return y(d.Time);
  });

// Adds the svg canvas
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 + ")");

// Get the data
var data = d3.csv.parse(d3.select("#csv").text());

data.forEach(function(d) {
  d.Day = parseDate(d.Day);
  d.Time = parseTime(d.Time);
});

// Scale the range of the data
x.domain(d3.extent(data, function(d) {
  return d.Day;
}));
y.domain([parseTime("00:00:01"), parseTime("23:59:59")]);

// Add the valueline path.
svg.append("path")
  .attr("class", "line")
  .attr("d", valueline(data));

// Add the scatterplot
svg.selectAll("dot")
  .data(data)
  .enter().append("circle")
  .attr("r", 3.5)
  .attr("cx", function(d) {
    return x(d.Day);
  })
  .attr("cy", function(d) {
    return y(d.Time);
  });

// Add the X Axis
svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

// Add the Y Axis
svg.append("g")
  .attr("class", "y axis")
  .call(yAxis);
pre {
  display: none;
}

path {
  stroke: steelblue;
  stroke-width: 2;
  fill: none;
}

.axis path,
.axis line {
  fill: none;
  stroke: grey;
  stroke-width: 1;
  shape-rendering: crispEdges;
}
<script src="//d3js.org/d3.v3.min.js"></script>
<pre id="csv">Day,Time
5-Feb-16,21:35:00
1-Feb-16,19:15:00
11-Dec-15,21:42:00
21-Jul-15,11:00:00</pre>

Upvotes: 1

Related Questions