javascript - D3.js 饼图色阶

标签 javascript d3.js charts pie-chart

考虑这样的数据集:

var pie_data = [{
    "hard_skill": "Strategy",
    "course_area_percentage": 9
}, {
    "hard_skill": "Analytics",
    "course_area_percentage": 18
}, {
    "hard_skill": "Economics",
    "course_area_percentage": 11
}, {
    "hard_skill": "Finance",
    "course_area_percentage": 7
}, {
    "hard_skill": "HR",
    "course_area_percentage": 5
}, {
    "hard_skill": "Innovation",
    "course_area_percentage": 2
}, {
    "hard_skill": "International",
    "course_area_percentage": 5
}, {
    "hard_skill": "Marketing",
    "course_area_percentage": 7
}, {
    "hard_skill": "Operations",
    "course_area_percentage": 14
}, {
    "hard_skill": "Others",
    "course_area_percentage": 11
}, {
    "hard_skill": "Project",
    "course_area_percentage": 11
}]

我正在创建一个饼图,以便将所有这些技能显示为图表的切片。我们的想法是为百分比较高的技能提供更绿色的颜色(意味着它更重要),从红色到绿色的色阶。

我尝试使用具有不同范围和域的d3.scale.ordinal() 和d3.scale.linear(),但颜色与上述模式。它们似乎是颠倒的,像这样:

enter image description here

我的色阶代码:

var pie_colorScale = d3.scale.ordinal()
    //.domain([0,pie_data.length])
    //.range(["#d7191c", "#eef200", "#008e15"]);                    
    //.range(["#d7191c","#f19d00","#eef200","#3fe256", "#008e15"]);
    .range(["#008e15", "#3fe256", "#eef200", "#f19d00", "#d7191c"]);

是否必须手动设置颜色到值的映射?

最佳答案

鉴于您想要的结果...

the idea then is to give a greener color (meaning it is more important), from a red to green color scale, to the skill that has higher percentage.

...,序数尺度不是适当的选择。

您可以使用具有“红色”和“绿色”范围内两种颜色的线性刻度,但结果并不好。因此,该解决方案使用范围内的三种颜色:“红色”、“黄色”和“绿色”。

实现这一点的技巧是在域中使用三个值:

var color = d3.scale.linear()
    .domain([d3.min(pie_data, d => d.course_area_percentage),
        d3.mean(pie_data, d => d.course_area_percentage),
        d3.max(pie_data, d => d.course_area_percentage)
    ])
    .range(["red", "yellow", "green"]);

这是一个演示:

var pie_data = [{
  "hard_skill": "Strategy",
  "course_area_percentage": 9
}, {
  "hard_skill": "Analytics",
  "course_area_percentage": 18
}, {
  "hard_skill": "Economics",
  "course_area_percentage": 11
}, {
  "hard_skill": "Finance",
  "course_area_percentage": 7
}, {
  "hard_skill": "HR",
  "course_area_percentage": 5
}, {
  "hard_skill": "Innovation",
  "course_area_percentage": 2
}, {
  "hard_skill": "International",
  "course_area_percentage": 5
}, {
  "hard_skill": "Marketing",
  "course_area_percentage": 7
}, {
  "hard_skill": "Operations",
  "course_area_percentage": 14
}, {
  "hard_skill": "Others",
  "course_area_percentage": 11
}, {
  "hard_skill": "Project",
  "course_area_percentage": 11
}]
var width = 500,
  height = 500,
  radius = Math.min(width, height) / 2;


var color = d3.scale.linear()
	.domain([d3.min(pie_data, d=>d.course_area_percentage),
	d3.mean(pie_data, d=>d.course_area_percentage),
	d3.max(pie_data, d=>d.course_area_percentage)])
	.range(["red", "yellow", "green"]);

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

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

var svg = d3.select("body").append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

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

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

g.append("text")
.attr("text-anchor", "middle")
  .attr("transform", function(d) {
    return "translate(" + arc.centroid(d) + ")";
  })
  .attr("dy", ".35em")
  .text(function(d) {
    return d.data.hard_skill;
  });

function type(d) {
  d.population = +d.population;
  return d;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

作为替代解决方案,如果您想使用您在问题中编写的颜色数组...

.range(["#008e15", "#3fe256", "#eef200", "#f19d00", "#d7191c"]);

...您应该使用量化(或分位数)比例:

var color = d3.scale.quantize()
    .domain(d3.extent(pie_data, d => d.course_area_percentage))
    .range(["#d7191c", "#f19d00", "#eef200", "#3fe256", "#008e15"]);

这是演示:

var pie_data = [{
  "hard_skill": "Strategy",
  "course_area_percentage": 9
}, {
  "hard_skill": "Analytics",
  "course_area_percentage": 18
}, {
  "hard_skill": "Economics",
  "course_area_percentage": 11
}, {
  "hard_skill": "Finance",
  "course_area_percentage": 7
}, {
  "hard_skill": "HR",
  "course_area_percentage": 5
}, {
  "hard_skill": "Innovation",
  "course_area_percentage": 2
}, {
  "hard_skill": "International",
  "course_area_percentage": 5
}, {
  "hard_skill": "Marketing",
  "course_area_percentage": 7
}, {
  "hard_skill": "Operations",
  "course_area_percentage": 14
}, {
  "hard_skill": "Others",
  "course_area_percentage": 11
}, {
  "hard_skill": "Project",
  "course_area_percentage": 11
}]
var width = 500,
  height = 500,
  radius = Math.min(width, height) / 2;


var color = d3.scale.quantize()
	.domain(d3.extent(pie_data, d=>d.course_area_percentage))
	.range(["#d7191c", "#f19d00", "#eef200", "#3fe256", "#008e15"]);

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

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

var svg = d3.select("body").append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

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

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

g.append("text")
.attr("text-anchor", "middle")
  .attr("transform", function(d) {
    return "translate(" + arc.centroid(d) + ")";
  })
  .attr("dy", ".35em")
  .text(function(d) {
    return d.data.hard_skill;
  });

function type(d) {
  d.population = +d.population;
  return d;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

关于javascript - D3.js 饼图色阶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42539489/

相关文章:

javascript - 类型检查后类型上不存在属性

javascript - D3 Javascript 条形图

ios - 如何使用 iOS 图表在 LineChart 上设置不同的颜色间隔?

javascript - 如何串联 Promise

javascript - ReactJS:如果在 promise 解决后经过身份验证,则呈现私有(private)路由

javascript - 如何根据用户偏好过滤通过 d3.js 渲染的节点和边?

charts - 谷歌图表改变背景颜色

asp.net - ASP.NET 3.5 中的图表 : Whats the best option?

javascript - 如何使用 momentjs 解析日期?

javascript - d3.js 中对数刻度的奇怪行为