d3. js Ошибка: <path>атрибут d: Ожидаемое число, «MNaN, 360LNaN, 43,9…» - PullRequest
0 голосов
/ 18 июня 2020

Я получаю сообщение об ошибке

Error: <path> attribute d: Expected number, "MNaN,360LNaN,43.9…".

При попытке отобразить график d3. js на сервере Django.

график. html:

<div id="my_dataviz"></div>

<script>
// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
  .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 + ")");

//Read the data
d3.csv("{{ TEMPCSV }}",

  // When reading the csv, I must format variables:
  function(d){
    return { value : d.value, date : d3.utcParse("%Y-%m-%dT%H:%M:%S.%LZ")(d.date) }
  },

  // Now I can use this dataset:
  function(data) {

    // Add X axis --> it is a date format
    var x = d3.scaleTime()
      .domain(d3.extent(data, function(d) { return d.date; }))
      .range([ 0, width ]);
    svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

    // Max value observed:
    const max = d3.max(data, function(d) { return +d.value; })

    // Add Y axis
    var y = d3.scaleLinear()
      .domain([0, max])
      .range([ height, 0 ]);
    svg.append("g")
      .call(d3.axisLeft(y));

    // Set the gradient
    svg.append("linearGradient")
      .attr("id", "line-gradient")
      .attr("gradientUnits", "userSpaceOnUse")
      .attr("x1", 0)
      .attr("y1", y(0))
      .attr("x2", 0)
      .attr("y2", y(max))
      .selectAll("stop")
        .data([
          {offset: "0%", color: "blue"},
          {offset: "100%", color: "red"}
        ])
      .enter().append("stop")
        .attr("offset", function(d) { return d.offset; })
        .attr("stop-color", function(d) { return d.color; });

    // Add the line
    svg.append("path")
      .datum(data)
      .attr("fill", "none")
      .attr("stroke", "url(#line-gradient)" )
      .attr("stroke-width", 2)
      .attr("d", d3.line()
        .x(function(d) { return x(d.date) })
        .y(function(d) { return y(d.value) })
        )

})

</script>

И мои данные csv имеют следующий формат:

value,  date
72, 2020-06-15 14:12:00+00:00
72, 2020-06-15 14:12:12+00:00
72  2020-06-15 14:12:23+00:00

Вы можете просмотреть полный файл здесь: https://github.com/lambrou/graphdata/blob/master/tempcsv.csv Примечание: {{TEMPCSV}} на графике. html - это строка, содержащая файл github tempcsv.csv ^

Я считаю, что проблема в том, как отформатированы мои данные. Я пробовал несколько форматов

d3.utcParse("%Y-%m-%dT%H:%M:%S.%LZ")(d.date)

, но, похоже, ни один из них не работает. Я также просмотрел другие вопросы, которые кажутся похожими на этот, но они не ответили на мой вопрос. Спасибо за ваше время.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...