D3图形未显示来自变量

时间:2019-01-04 01:59:08

标签: json d3.js graph

我想从变量中加载JSON数据,而不是从CSV文件中加载数据。

let jsonData = [
{"Date": "1/1/2014 01:00", "Air_Temp": 3.1},
{"Date": "1/1/2014 02:00", "Air_Temp": 3.2},
{"Date": "1/1/2014 03:00", "Air_Temp": 1.6},
{"Date": "1/1/2014 04:00", "Air_Temp": 1.0},
{"Date": "1/1/2014 05:00", "Air_Temp": 2.3}
];

显示的图形没有错误记录到控制台,但是没有加载数据。

https://jsfiddle.net/s9cbwvL1/1/

2 个答案:

答案 0 :(得分:0)

您需要转换日期

var parseDate = d3.timeParse("%m/%d/%Y %H:%M");

jsonData.forEach( d => { d.Date = parseDate(d.Date); });

并将数据集作为一个传递给path元素

Line_chart.append("path")
    .data([jsonData])
    .attr("class", "line")
    .attr("d", line);

context.append("path")
    .data([jsonData])
    .attr("class", "line")
    .attr("d", line2);

答案 1 :(得分:0)

费城

  

您没有使用自己创建的功能

parseDate函数和type函数

当我阅读该功能时,我认为有人设法重新排列数据上的d.Date

结果

您的轴域为[ invalid date, invalid date ]

解决方案

我将d.Date转换为正确的格式

  

您发送的数据不在数组中

结果

您的图表未绘制也没有错误

解决方案

我在jsonData上使其成为双精度数组

Line_chart.data([jsonData])

context.data([jsonData])

看看

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style>

 .line {
        fill: none;
        stroke: steelblue;
        stroke-width: 1.5px;
    }
.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}

</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>

</head>
<body>

<script type="text/javascript">
  
  //Example: https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f


let jsonData1 = 
[
{"Date": "1/1/2014 01:00", "Air_Temp": 3.1},
{"Date": "1/1/2014 02:00", "Air_Temp": 3.2},
{"Date": "1/1/2014 03:00", "Air_Temp": 1.6},
{"Date": "1/1/2014 04:00", "Air_Temp": 1.0},
{"Date": "1/1/2014 05:00", "Air_Temp": 2.3}
];

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 110, left: 40},
    margin2 = {top: 430, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

var parseDate = d3.timeParse("%m/%d/%Y %H:%M");
let jsonData = jsonData1.map(function(d,i){ d.Date = parseDate(d.Date); return d })

var x = d3.scaleTime().range([0, width]),
    x2 = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]);

var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

var brush = d3.brushX()
    .extent([[0, 0], [width, height2]])
    .on("brush end", brushed);

var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .translateExtent([[0, 0], [width, height]])
    .extent([[0, 0], [width, height]])
    .on("zoom", zoomed);

    var line = d3.line()
        .x(function (d) { return x(d.Date); })
        .y(function (d) { return y(d.Air_Temp); });

    var line2 = d3.line()
        .x(function (d) { return x2(d.Date); })
        .y(function (d) { return y2(d.Air_Temp); });

    var clip = svg.append("defs").append("svg:clipPath")
        .attr("id", "clip")
        .append("svg:rect")
        .attr("width", width)
        .attr("height", height)
        .attr("x", 0)
        .attr("y", 0); 


    var Line_chart = svg.append("g")
        .attr("class", "focus")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
        .attr("clip-path", "url(#clip)");


    var focus = svg.append("g")
        .attr("class", "focus")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

  x.domain(d3.extent(jsonData, function(d) { return d.Date; }));
  y.domain([0, d3.max(jsonData, function (d) { return d.Air_Temp; })]);
  x2.domain(x.domain());
  y2.domain(y.domain());


    focus.append("g")
        .attr("class", "axis axis--x")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    focus.append("g")
        .attr("class", "axis axis--y")
        .call(yAxis);

    Line_chart.selectAll('path')
        .data([jsonData])
        .enter()
        .append("path")
        .attr("class", "line")
        .attr("d", function (d){ line });

    context.append("path")
        .data([jsonData])
        .attr("class", "line")
        .attr("d", line2);


  context.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

  svg.append("rect")
      .attr("class", "zoom")
      .attr("width", width)
      .attr("height", height)
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);




function brushed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
  var s = d3.event.selection || x2.range();
  x.domain(s.map(x2.invert, x2));
  Line_chart.select(".line").attr("d", line);
  focus.select(".axis--x").call(xAxis);
  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
}

function zoomed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
  var t = d3.event.transform;
  x.domain(t.rescaleX(x2).domain());
  Line_chart.select(".line").attr("d", line);
  focus.select(".axis--x").call(xAxis);
  context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}

function type(d) {
  d.Date = parseDate(d.Date);
  d.Air_Temp = +d.Air_Temp;
  return d;
}

</script>

</body>
</html>