我是HTML的新手,所以如果这是一个愚蠢的问题,我很抱歉。
我目前的数据是一个外部的JSON,并调用HTML来加载。我只想将它复制到HTML中并保存为它的一部分,而不是加载它。我如何在不改变数据结构的情况下做到这一点呢?
示例数据JSON:
[
{
"User" : "COMP",
"Runtime" : 931.216111111
},
{
"User" : "AUTO",
"Runtime" : 600.404444444
}
]
我加载它的位置:
// load data
d3.json("data.json", function(error, data) {
data.forEach(function(d) {
d.User = d.User;
d.Runtime = +d.Runtime;
});
// scale the range of the data
x.domain(data.map(function(d) { return d.User; }));
y.domain([0, d3.max(data, function(d) { return d.Runtime; })]);
我已经查看了答案,但我的问题是如何将其复制到超文本标记语言中,同时仍然能够以相同的方式调用d.Runtime和d.User参数
发布于 2018-06-30 03:54:13
在HTML页面中,您需要添加一个脚本标记来保存JavaScript。将其放入正文中可能是一个好主意,这样它将在页面加载时执行。
<script type="text/javascript">
</script>
接下来,您需要为script标记内的数据定义一个常量。可以在等号后面复制和粘贴.json文件的内容:
const data = [
{
"User" : "COMP",
"Runtime" : 931.216111111
},
{
"User" : "AUTO",
"Runtime" : 600.404444444
}
]
现在,您可以在d3.json回调函数中添加代码:
data.forEach(function(d) {
d.User = d.User;
d.Runtime = +d.Runtime;
});
// scale the range of the data
x.domain(data.map(function(d) { return d.User; }));
y.domain([0, d3.max(data, function(d) { return d.Runtime; })]);
https://stackoverflow.com/questions/51104651
复制相似问题