【问题标题】:Plot a graph using D3.JS使用 D3.JS 绘制图形
【发布时间】:2018-06-01 07:13:25
【问题描述】:

我正在尝试使用 D3.js 绘制图表。 file.json 是我的 JSON 文件。日期时间应该在 X 轴上,而销售额应该在 Y 轴上。

这是我的 JSON 结构,

[

    {

        Date : "2017-12-17 18:30:01",
        Sales : "50"

    },
    {   Date : "2017-12-17 17:30:00",
        Sales : "20"

    },
    {
        Date : "2017-12-17 16:30:00",
        Sales : "10"
    }

]

我想绘制日期与销售数量的图表。这是我的 JS :-

<!DOCTYPE html>
<meta charset="utf-8">
<style> 

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 2px;
}
</style>
<body>

<!-- load the d3.js library -->     
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

d3.json("file.json", function(data) {
   var canvas = d3.select("body").append("svg")
        .attr("width", 500)
        .attr("height", 500)
        .attr("border", "black")

   var group = canvas.append("g")
        .attr("transform", "translate(100,10)")

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

   group.selectAll("path")
        .data(data).enter()
        .append("path")
        .attr("d", function(d){ return line(d) })
        .attr("fill", "none")
        .attr("stroke", "green")
        .attr("stroke-width", 3);
});

</script>

显示错误:- Uncaught TypeError: Cannot read property 'line' of undefined 。我从 d3.svg.line() 更改为 d3.line() ,但现在显示空白页。

【问题讨论】:

  • 您使用的是 D3 v4。所以,应该是var line = d3.line()
  • 现在,它没有显示任何内容。填充=无
  • 那是因为你没有秤,这是另一个问题。
  • 我正在尝试添加比例,它正在显示,无法读取未定义的属性“线性”

标签: javascript jquery json d3.js svg


【解决方案1】:

这是您可能想要的示例(取自:https://bl.ocks.org/mbostock/3883245

var data = [
    {
        Date : "2017-12-17 18:30:01",
        Sales : "50"

    },
    {   Date : "2017-12-17 17:30:00",
        Sales : "20"

    },
    {
        Date : "2017-12-17 16:30:00",
        Sales : "10"
    }
].map(function(entry) {
  return {
    Date: d3.timeParse("%Y-%m-%d %H:%M:%S")(entry.Date),
    Sales: +entry.Sales
  }
});

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 30, left: 50},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scaleTime()
    .rangeRound([0, width]);

var y = d3.scaleLinear()
    .rangeRound([height, 0]);

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

x.domain(d3.extent(data, function(d) { return d.Date; }));
y.domain(d3.extent(data, function(d) { return d.Sales; }));

g.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x))

g.append("g")
    .call(d3.axisLeft(y))

g.append("path")
    .datum(data)
    .attr("fill", "none")
    .attr("stroke", "steelblue")
    .attr("stroke-linejoin", "round")
    .attr("stroke-linecap", "round")
    .attr("stroke-width", 1.5)
    .attr("d", line);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.0/d3.min.js"></script>'

<svg width="600" height="180"></svg>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多