样式不适用于Mozilla Firefox

时间:2018-05-08 14:13:22

标签: javascript d3.js

我想构建堆叠图表栏。 要构建它我使用的是d3js版本3。 我在codepen上找到了一些模板并改变了我的需求。

当我在Chrome中打开条形图时,条形图看起来不错: enter image description here

但是当我在Mozilla Firefox中打开它时,它显示没有颜色: enter image description here

我应该如何修改样式?

这是我的html和javascript代码:



<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>d3.js learning</title>
    <script data-require="d3@3.0.0" data-semver="3.0.0" src="https://d3js.org/d3.v3.min.js"></script>

  <style type="text/css">
  svg {
    font: 10px sans-serif;
    shape-rendering: crispEdges;
  }

  .axis path,
  .axis line {
    fill: none;
    stroke: #000;
  }

  path.domain {
    stroke: none;
  }

  .y .tick line {
    stroke: #ddd;
  }
  </style>

</head>
<body>
<script>

// Setup svg using Bostock's margin convention

/* Data in strings like it would be if imported from a csv */

var data = [
  { year: "2006", pid: "10", eid: "15"},
  { year: "2007", pid: "12", eid: "18"},
  { year: "2008", pid: "05", eid: "20"},
  { year: "2009", pid: "01", eid: "15"},
  { year: "2010", pid: "02", eid: "10"},
  { year: "2011", pid: "03", eid: "12"},
  { year: "2012", pid: "04", eid: "15"},
  { year: "2013", pid: "06", eid: "11"},
  { year: "2014", pid: "10", eid: "13"},
  { year: "2015", pid: "16", eid: "19"},

];

var margin = {top: 20, right: 160, bottom: 35, left: 30};

var width = 1000 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

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 + "," + margin.top + ")");




var parse = d3.time.format("%Y").parse;


// Transpose the data into layers
var dataset = d3.layout.stack()(["pid", "eid"].map(function(id) {
  return data.map(function(d) {
    return {x: parse(d.year), y: +d[id]};
  });
}));


// Set x, y and colors
var x = d3.scale.ordinal()
  .domain(dataset[0].map(function(d) { return d.x; }))
  .rangeRoundBands([10, width-10], 0.20);

var y = d3.scale.linear()
  .domain([0, d3.max(dataset, function(d) {  return d3.max(d, function(d) { return d.y0 + d.y; });  })])
  .range([height, 0]);

var colors = [ "3d3756", "574e7c"];


// Define and draw axes
var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left")
  .ticks(5)
  .tickSize(-width, 0, 0)
  .tickFormat( function(d) { return d } );

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom")
  .tickFormat(d3.time.format("%Y"));

svg.append("g")
  .attr("class", "y axis")
  .call(yAxis);

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis)
  .selectAll("text")
    .style("text-anchor", "end")
    .attr("dx", "-.8em")
    .attr("dy", ".15em")
    .attr("transform", "rotate(-65)");;


// Create groups for each series, rects for each segment
var groups = svg.selectAll("g.cost")
  .data(dataset)
  .enter().append("g")
  .attr("class", "cost")
  .style("fill", function(d, i) { return colors[i]; });

var rect = groups.selectAll("rect")
  .data(function(d) { return d; })
  .enter()
  .append("rect")
  .attr("x", function(d) { return x(d.x); })
  .attr("y", function(d) { return y(d.y0 + d.y); })
  .attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
  .attr("width", x.rangeBand())
  .on("mouseover", function() { tooltip.style("display", null); })
  .on("mouseout", function() { tooltip.style("display", "none"); })
  .on("mousemove", function(d) {
    var xPosition = d3.mouse(this)[0] - 15;
    var yPosition = d3.mouse(this)[1] - 25;
    tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
    tooltip.select("text").text(d.y);
  });


// Draw legend
var legend = svg.selectAll(".legend")
  .data(colors)
  .enter().append("g")
  .attr("class", "legend")
  .attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });

legend.append("rect")
  .attr("x", width - 18)
  .attr("width", 18)
  .attr("height", 18)
  .style("fill", function(d, i) {return colors.slice().reverse()[i];});

legend.append("text")
  .attr("x", width + 5)
  .attr("y", 9)
  .attr("dy", ".35em")
  .style("text-anchor", "start")
  .text(function(d, i) {
    switch (i) {
      case 0: return "EID";
      case 1: return "PID";
    }
  });


// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
  .attr("class", "tooltip")
  .style("display", "none");

tooltip.append("rect")
  .attr("width", 30)
  .attr("height", 20)
  .attr("fill", "white")
  .style("opacity", 0.5);

tooltip.append("text")
  .attr("x", 15)
  .attr("dy", "1.2em")
  .style("text-anchor", "middle")
  .attr("font-size", "12px")
  .attr("font-weight", "bold");

</script>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

通过向颜色添加哈希来修复它:

从:

const all_colors = ["3d3756", "f0ee42", "f4c7de"];

为:

const all_colors = ["#3d3756", "#f0ee42", "#f4c7de"];
相关问题