首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTML Javascript显示在Firefox中,但不显示Chrome或IE怎么办?

HTML Javascript显示在Firefox中,但不显示Chrome或IE怎么办?
EN

Stack Overflow用户
提问于 2018-08-07 00:39:12
回答 1查看 0关注 0票数 0

我已经阅读了所有与我的问题相关的堆叠溢出问题,但我仍然想不出如何解决这个问题。下面的代码读取data.JSON,然后以条形图的形式输出数据,由于某种原因,在Chrome或IE中没有出现。

我读到的一些答案是将html的所有引用都引用到txt或设置SVG的边界,但据我所知,就我有限的经验而言,这已经完成了 :

代码语言:txt
复制
  <!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

<head>
    <style>

  .bar{
    fill: darkblue;
  }

  .bar:hover{
    fill: darkred;
  }

    .axis {
      font: 20px sans-serif;
    }

    .axis path,
    .axis line {
      fill: none;
      stroke: #000;
      shape-rendering: crispEdges;
    }

    </style>

    <h2>User Runtime by Hours</h2>
    <h3>Generate on: </h3>

</head>

<body>

<script src="d3js.org/d3.min.js"></script>
<script src="js/jquery.min.js"></script>

<script>
// set the dimensions of the canvas
var margin = {top: 20, right: 20, bottom: 100, left: 80},
    width = 1600 - margin.left - margin.right,
    height = 700 - margin.top - margin.bottom;


// set the ranges
var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

var y = d3.scale.linear().range([height, 0]);

// define the axis
var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")


var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(10);


// add the SVG element
var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", 
          "translate(" + (margin.left + 20) + "," + margin.top + ")");


// 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; })]);

  // add axis
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .selectAll("text")
      .style("text-anchor", "end")
      .attr("dx", "-.7em")
      .attr("dy", "-.3em")
      .attr("transform", "rotate(-90)" );

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("hour")
      .attr("transform", "rotate(-90)")
      .attr("y", 5)
      .attr("dy", "-30em")
      .style("text-anchor", "start")
      .text("Hours");


  // Add bar chart
  svg.selectAll("bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.User); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.Runtime); })
      .attr("height", function(d) { return height - y(d.Runtime); });

});

</script>

</body>

示例数据JSON:

代码语言:txt
复制
[
{ 
    "User" : "xxxx",
    "Runtime" : 931.216111111
},
{ 
    "User" : "yyyy",
    "Runtime" : 600.404444444
}
]

Chrome错误代码:

代码语言:txt
复制
d3.min.js:1 Failed to load file:///C:/EUV/logbook/data.json: Cross origin 
requests are only supported for protocol schemes: http, data, chrome, 
chrome-extension, https.
Cn.i.send @ d3.min.js:1
index.html:76 Uncaught TypeError: Cannot read property 'forEach' of 
undefined
at Object.<anonymous> (index.html:76)
at Object.t (d3.min.js:1)
at XMLHttpRequest.u (d3.min.js:1)
at Object.Cn.i.send (d3.min.js:1)
at Object.i.(anonymous function) [as get] (file:///C:/EUV/logbook/d3js.org/d3.min.js:1:10862)
at Cn (d3.min.js:1)
at Object.oa.json (d3.min.js:5)
at index.html:74
EN

回答 1

Stack Overflow用户

发布于 2018-08-07 09:40:14

你正在使用file://因为他们不是http://

在本地pc中安装一个web服务器,或者将模型上传到某个地方。

如果你使用的是ATOM或VS代码,则有本地服务器的扩展。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100001969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档