使用Twitter Bootstrap时D3绘制颜色方案问题

时间:2016-08-29 18:25:12

标签: javascript css twitter-bootstrap d3.js

我很难获得一些D3图来使用我指定的配色方案。我正在使用烧瓶,Twitter Bootstrap和D3。

具体来说,我正在尝试使用CSV文件中的数据制作饼图(请参阅下面的代码)。即使我为图表指定了自己的颜色(参见代码部分:“。range([”#FF4A29“,”#8a89a6“,”#7b6888“,”#6b486b“,”#a05d56“,”# d0743c“,”#ff8c00“]);”)生成的图表有奇怪的紫色和绿色(见下文)。这些相同的紫色和绿色出现在我在页面上制作的其他D3图中。

当我在非Bootstrap页面上使用此javascript代码时,它看起来正确!我猜测Bootstrap CSS中的某些东西是否覆盖了javascript代码?无论如何强制我指定的颜色?

<style>

.arc text {
  font: 12px sans-serif;
  text-anchor: middle;
}

.arc path {
  stroke: #fff;
}

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

var width = 500,
    height = 500,
    radius = Math.min(width, height) / 3;

var color = d3.scale.ordinal()
    .range(["#FF4A29", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var arc = d3.svg.arc()
    .outerRadius(radius - 10)
    .innerRadius(0);

var labelArc = d3.svg.arc()
    .outerRadius(radius + 60)
    .innerRadius(radius - 40);

var pie = d3.layout.pie()
    .sort(null)
    .value(function(d) { return d.mentions; });

var svg2 = d3.select("#competitors").append("svg")
    .attr("width", width)
    .attr("height", height)
  .append("g")
    .attr("transform", "translate(" + width / 2.3 + "," + height / 2.3 + ")");

d3.csv("static/competitor_mentions.csv", type, function(error, data) {
  if (error) throw error;

  var g = svg2.selectAll(".arc")
      .data(pie(data))
    .enter().append("g")
      .attr("class", "arc");

  g.append("path")
      .attr("d", arc)
      .style("fill", function(d) { return color(d.data.company); });

  g.append("text")
      .attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")"; })
      .attr("dy", ".35em")
      .text(function(d) { return d.data.company; });
});

function type(d) {
  d.mentions = +d.mentions;
  return d;
}

</script>

Pie chart with weird colors

0 个答案:

没有答案