繁体   English   中英

Jupyter notebook 不显示 d3.js 可视化的 output

[英]Jupyter notebook does not display output for d3.js visualization

我正在尝试在 Jupyter 笔记本的 d3.js 中创建分组条形图。 Jupyter没有显示任何output,没有错误,控制台也没有显示任何错误。 我不知道缺少什么或为什么没有 output,我非常感谢任何帮助。

请让我知道 Jupyter 中缺少什么,以及为什么 output 是空的。

 %%javascript
require(['d3v4'], function(d3) { 

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

var color = d3.scaleOrdinal(d3.schemeCategory10);

var x = d3.scaleBand().rangeRound([0, width])
  .padding(0.1),
  y = d3.scaleLinear().rangeRound([height, 0]);

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

var data = [{
  "Group": "Mars",
  "count": 10,
  "months": "June"
}, {
  "Group": "Jupiter",
  "count": 50,
  "months": "June"
}, {
  "Group": "Mars",
  "count": 70,
  "months": "July"
}, {
  "Group": "Jupiter",
  "count": 60,
  "months": "July"
}];

var ymaxdomain = d3.max(data, function(d) {
  return d.count;
});
x.domain(data.map(function(d) {
  return d.months
}));
y.domain([0, ymaxdomain]);

var x1 = d3.scaleBand()
  .rangeRound([0, x.bandwidth()])
  .padding(0.05)
  .domain(data.map(function(d) {
    return d.Group;
  }));

color.domain(data.map(function(d) {
  return d.Group;
}));

var groups = g.selectAll(null)
  .data(data)
  .enter()
  .append("g")
  .attr("transform", function(d) {
    return "translate(" + x(d.months) + ",0)";
  })
.selectAll("rect")
  .data(function(d) {
    return [d]
  })
  .enter()
  .append("rect")
  .attr("x", function(d, i) {
    return x1(d.Group)
  })
  .attr("y", function(d) {
    return y(d.count);
  })
  .attr("width", x1.bandwidth())
  .attr("height", function(d) {
    return height - y(d.count);
  })
  .attr("fill", function(d) {
    return color(d.Group)
  })

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

g.append("g")
  .attr("class", "axis")
  .call(d3.axisLeft(y).ticks(null, "s"))
  .append("text")
  .attr("x", 2)
  .attr("y", y(y.ticks().pop()) + 0.5)
  .attr("dy", "0.32em")
  .attr("fill", "#000")
  .attr("font-weight", "bold")
  .attr("text-anchor", "start")
  .text("count");

})

浏览器本身可能没有解释 d3。 当我遇到类似问题时,此解决方案对我有用。

只需将以下内容添加到代码的开头:

from IPython.core.display import display, HTML

HTML('<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM