来自json的多线图问题

时间:2017-12-20 10:32:05

标签: d3.js

以下是我的code。我一直试图在轴上以月为单位绘制湿度和露点。但我得到的数据错误未定义,轴上的月份也有数量。

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
    <title> Data Visualization - Binding Dataset to Shapes Using D3 </title>
    <script src="https://d3js.org/d3.v3.min.js"></script>
</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;
}

.legend {
    font-size: 16px;
    font-weight: bold;
    text-anchor: middle;
}

</style>
<body>

<script>

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

// Parse the date / time
var parseDate = d3.time.format("%b").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 priceline = d3.svg.line()   
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.dew); });

// 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.json("weatherdata.json", function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.history.date.mon);
        d.dew = +d.history.dailysummary[0].meandewptm;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain(d3.extent(data, function(d) { return d.dew; }));

    // Nest the entries by symbol
    var dataNest = d3.nest()
        .key(function(d) {return d.dew;})
        .entries(data);

    var color = d3.scale.category10();   // set the colour scale

    legendSpace = width/dataNest.length; // spacing for legend

    // Loop through each symbol / key
    dataNest.forEach(function(d,i) { 

        svg.append("path")
            .attr("class", "line")
            .style("stroke", function() { // Add the colours dynamically
                return d.color = color(d.key); })
            .attr("d", priceline(d.values));

        // Add the Legend
        svg.append("text")
            .attr("x", (legendSpace/2)+i*legendSpace) // spacing
            .attr("y", height + (margin.bottom/2)+ 5)
            .attr("class", "legend")    // style the legend
            .style("fill", function() { // dynamic colours
                return d.color = color(d.key); })
            .text(d.key);

    });

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

</html>

我不知道我将如何解决它。谁能请帮忙。我正在与data file分享current issue

我已按照model data的方式添加了它的样子:

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:0)

关于X轴日期格式的问题,抱歉,我回答是因为我无法发表评论(需要更多声望)。

我认为你需要做这样的事情

chart.xAxis
.tickFormat(function(d) {
return d3.time.format('%b')(format(d)); 
});

这只显示月份。