D3JS - 我的X轴网格线没有显示出来

时间:2014-05-17 23:17:16

标签: javascript css svg d3.js

我不知道为什么我的X轴线显示完全正常,但不是Y.有什么想法吗?

我99%确定它与

有关
.attr("transform", "translate(0," + height + ")")

行,因为每次我调整它显示X轴片,只是在错误的空间。

整个代码如下:

<html>

<head>

<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;
}

.grid .tick {
    stroke: lightgrey;
    opacity: 0.7;
}
.grid path {
      stroke: lightgrey;
      stroke-width: 0;
}

</style>

</head>

<body>

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

<h1>Temperature Chart Over Time</h1>

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

// Display the date and time
var     parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;

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

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

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

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

        // 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 + ")");

//Draw X axis grid tick marks
function make_x_axis() {
    return d3.svg.axis()
        .scale(x)
         .orient("bottom")
         .ticks(5)
}

//Draw Y axis grid tick marks
function make_y_axis() {
    return d3.svg.axis()
        .scale(y)
        .orient("left")
        .ticks(5)
}

//draw x axis grid lines
svg.append("g")
        .attr("class", "grid")
        .attr("transform", "translate(0," + height + ")")
        .call(make_x_axis()
            .tickSize(-height, 0, 0)
            .tickFormat("")
        )
//draw y axis grid lines
    svg.append("g")
        .attr("class", "grid")
        .call(make_y_axis()
            .tickSize(-width, 0, 0)
            .tickFormat("")
        )

// Get the data
d3.json("data.php", function(error, data) {
        data.forEach(function(d) {
                d.measurementTime = parseDate(d.measurementTime);
                d.temperature = +d.temperature;
        });

        // Center the line on the graph
        x.domain(d3.extent(data, function(d) { return d.measurementTime; }));
        y.domain([60, d3.max(data, function(d) { return d.temperature; })+10]);

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

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

</html>

0 个答案:

没有答案