D3 堆积条形图在支流中有效,但在其他地方不起作用
D3 Stacked bar graph works in Tributary but nowhere else
这具有我想要的预期结果,但是当我将代码作为脚本导入HTML文件时,它根本没有显示任何内容。
var PUBLIC = [50,40,10];
var NONPROFIT = [30,40,30];
var FOR_PROFIT = [70,15,15];
var data = [
{"key":"PUBLIC", "pop1":PUBLIC[0], "pop2":PUBLIC[1], "pop3":PUBLIC[2]},
{"key":"NONPROFIT", "pop1":NONPROFIT[0], "pop2":NONPROFIT[1], "pop3":NONPROFIT[2]},
{"key":"FORPROFIT", "pop1":FOR_PROFIT[0], "pop2":FOR_PROFIT[1], "pop3":FOR_PROFIT[2]}
];
var n = 3, // Number of layers
m = data.length, // Number of samples per layer
stack = d3.layout.stack(),
labels = data.map(function(d) { return d.key; }),
// Go through each layer (pop1, pop2 etc, that's the range(n) part)
// then go through each object in data and pull out that objects's population data
// and put it into an array where x is the index and y is the number
layers = stack(d3.range(n).map(function(d)
{
var a = [];
for (var i = 0; i < m; ++i)
{
a[i] = { x: i, y: data[i]['pop' + (d+1)] };
}
return a;
})),
// The largest single layer
yGroupMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y; }); }),
// The largest stack
yStackMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y0 + d.y; }); });
var margin = {top: 40, right: 10, bottom: 20, left: 50},
width = 677 - margin.left - margin.right,
height = 533 - margin.top - margin.bottom;
var y = d3.scale.ordinal()
.domain(d3.range(m))
.rangeRoundBands([2, height], .08);
var x = d3.scale.linear()
.domain([0, yStackMax])
.range([0, width]);
var color = d3.scale.linear()
.domain([0, n - 1])
.range(["#aad", "#556"]);
var svg = d3.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var layer = svg.selectAll(".layer")
.data(layers)
.enter().append("g")
.attr("class", "layer")
.style("fill", function(d, i) { return color(i); });
layer.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("y", function(d) { return y(d.x); })
.attr("x", function(d) { return x(d.y0); })
.attr("height", y.rangeBand())
.attr("width", function(d) { return x(d.y); });
var yAxis = d3.svg.axis()
.scale(y)
.tickSize(1)
.tickPadding(6)
.tickValues(labels)
.orient("left");
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
我相信我已经导入了所有必需的库,然后导入了一些:
<!-- D3 Library -->
<script src='https://d3js.org/d3.v3.min.js' charset='utf-8'></script>
<!-- jQuery Mobile -->
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<!-- jQuery Main -->
<script src='https://code.jquery.com/jquery-2.2.3.min.js' charset='utf-8'></script>
虽然获取变量的代码有点简化(即简单地设置我的数组),但它们与数据数组中的格式相同。
此外,当我导入 Tributary 用于其基本库的所有内容时,此示例在 CodePen 中也不起作用。虽然,同样,这不是我创建代码的 100%,但 Tributary 上更简单的工作示例不适用于 CodePen。
在过去的几周里,D3 除了踢我的屁股之外什么也没做,我需要一些指导。谢谢。
您需要等待页面完全加载,或者您可以将代码放在结束</body>
标记之前。
解决方案1:
$(function() {
//Put your code here;
});
解决方案2:
<body>
<svg></svg>
<script>
//your code here
</script>
</body>
演示:https://jsfiddle.net/iRbouh/ag6p4kkg/
相关文章:
- Array.length似乎不起作用;console.log则显示其他情况
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- jQuery Mobile到其他页面的锚链接不起作用
- 带有Safari的Javascript;不起作用——所有其他浏览器都起作用
- 使用 jquery 在我的网站中加载其他网页不起作用
- 将JSON对象转换为敲除observable在IE8中不起作用,但在所有其他浏览器中都起作用
- jQuery.remove()不起作用,而其他内置函数则起作用
- 引导程序3在IE8中不起作用(已检查其他答案)
- 是jQuery 1.3.2导致此操作不起作用,还是其他原因
- DomElement.click() 事件在 chrome 中不起作用,但其他事件可以工作
- 为什么除了数据库(WebSQL)之外,其他数据库名称在android中不起作用
- Pjax.页面上的其他JS脚本不会;不起作用
- 主菜单JS在IE和Firefox中不起作用(其他菜单起作用)
- 滑动在IE中不起作用.但它在所有其他浏览器中都能正常工作
- 滑块图像在Internet Explorer中不起作用,但在其他浏览器中有效
- 数据表删除按钮在其他页面上不起作用,除了第一个
- 虽然循环不起作用(在其他类似问题中找不到答案)
- jQuery事件在Mozilla上不起作用,并且适用于其他浏览器
- Javascript按钮在IE中不起作用;在其他浏览器中工作
- 为什么我的“其他”不起作用