Вопрос

I'm trying to plot a line chart with dates along the x axis and time in minutes and seconds on the y axis.

The axis are all fine but when I try to plot the line graph it's not displaying as I expected.

I end up with lines along the axis.

Here is my js code.

/**
 * Function for sorting times
 */
function numOrdA(a, b){ return (a[1]-b[1]); }

/**
 * Array of timestamps and times in seconds
 */
var data = [
    [1395237411, 130],
    [1395950916, 90],
    [1397557328, 95],
    [1398353666, 87]
];

/**
 * Sort the data by the time in seconds
 */
data.sort(numOrdA);

var width = 700,
    height = 400,
    padding = 100;

/**
 * Create an svg container
 */
var vis = d3.select("body").
    append("svg:svg")
    .attr("width", width)
    .attr("height", height);

/**
 * Create min and max dates for x axis
 */
var mindate = new Date,
    maxdate = new Date;

mindate.setTime(data[0][0] * 1000);
maxdate.setTime(data[data.length - 1][0] * 1000);

/**
 * Create min and max times for y axis
 */
var mintime = new Date,
    maxtime = new Date;

mintime.setTime(data[0][1] * 1000);
maxtime.setTime(data[data.length - 1][1] * 1000);

/**
 * Create y scale using min and max times
 */
var yScale = d3.time.scale()
    .domain([mintime, maxtime])
    .range([height - padding, padding]);

/**
 * Create x scale with min and max dates
 */
var xScale = d3.time.scale()
    .domain([mindate, maxdate])
    .range([padding, width - padding * 2]);

/**
 * Create the y axis with time in minutes and seconds
 */
var yAxis = d3.svg.axis()
    .orient("left")
    .scale(yScale)
    .tickFormat(d3.time.format("%M:%S"));

/**
 * Create the x axis with a date format
 */
var xAxis = d3.svg.axis()
    .orient("bottom")
    .scale(xScale)
    .tickFormat(d3.time.format("%a %x"));

/**
 * Append y axis
 */
vis.append("g")
    .attr("transform", "translate("+padding+",0)")
    .call(yAxis);

/**
 * Append the x axis
 */
vis.append("g")
    .attr("class", "xaxis")
    .attr("transform", "translate(0," + (height - padding) + ")")
    .call(xAxis);

/**
 * Rotate the text on the x axis
 */
vis.selectAll(".xaxis text")
    .attr("transform", function(d) {
        return "translate(" + this.getBBox().height*-2 + "," + this.getBBox().height + ")rotate(-45)";
    });

/**
 * Line function to plot the times (y axis) against dates (x axis)
 */
var line = d3.svg.line()
        .x(function(d) {
            return xScale(d[1]);
        })
        .y(function(d) {
            return yScale(d[0]);
        });

/**
 * Add the path to the svg
 */
vis.append("svg:path").attr("d", line(data));

Here is the current code on jsfiddle

Thanks for any help

Это было полезно?

Решение

One of the main problems was not plotting the data as time objects in the line function.

I found this tutorial http://www.sitepoint.com/creating-simple-line-bar-charts-using-d3-js/ that I followed for plotting a line chart.

I then worked out how to convert everything to be time based.

I've posted the final code on Codepen.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top