【问题标题】:How do I get d3.scaleOrdinal我如何获得 d3.scaleOrdinal
【发布时间】:2020-03-29 05:08:28
【问题描述】:

我有这个 - 我认为比较简单 - 代码片段。它像我想要的那样工作:我得到一个根据基础数据划分的饼图。但我无法获得我设计的配色方案来显示。最初我在对 scaleOrdinal 的调用中有 basedata ,但这没有区别。 如何获取 baseata 中相应条目的显示颜色?

  const svgContainer = d3.select("#d3").append("svg")
    .attr("viewBox", [-60, -60, 120, 120])
    .attr("width", 120)
    .attr("height", 120)
    .attr("style", "display: inline; float: left");

  // Generate the pie
  var basedata = [50, 1.7, 1.7, 1.7, 39.8, 1.7, 1.7, 1.7];

  var myColor = d3.scaleOrdinal()
    .domain([   50,        1.7,        1.7,          1.7,         39.8,          1.7,         1.7,          1.7    ])
    .range( ["yellow", "royalblue", "mediumblue", "darkblue", "midnightblue", "darkblue", "mediumblue", "royalblue"]);

  const arcs = d3.pie()
    .startAngle(-0.5 * Math.PI)
    .sort(null)(basedata);

  const arc = d3.arc()
    .innerRadius(0)
    .outerRadius(120 / 2 - 1);

  const pie = d3.select("svg").append("g")
      .attr("stroke", "black")
    .selectAll("path")
    .data(arcs)
    .join("path")
      .attr("d", arc)
      .attr("fill", function (d) { return myColor(d) });

【问题讨论】:

  • 尝试传递哈希码而不是颜色名称。
  • 感谢您的回复。我解决了。见下文。

标签: javascript d3.js color-scheme


【解决方案1】:

我找到了答案here

不要使用 scaleOrdinal。 将填充命令替换为:.attr("fill", function (d, i) { return myColor[i] })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-29
    • 2016-02-04
    • 2016-07-21
    • 1970-01-01
    • 2016-03-27
    • 2016-03-05
    • 2021-12-09
    • 2021-05-16
    相关资源
    最近更新 更多