score:1
Given your desired outcome...
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.
..., an ordinal scale is not the adequate choice.
You can use a linear scale with two colours in the range, "red" and "green", but the result is not nice. So, this solution uses three colours in the range: "red", "yellow" and "green".
The trick to do that is using three values in the domain:
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"]);
Here is a demo:
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>
As an alternative solution, if you want to use the color array you wrote in the question...
.range(["#008e15", "#3fe256", "#eef200", "#f19d00", "#d7191c"]);
... you should use a quantize (or quantile) scale instead:
var color = d3.scale.quantize()
.domain(d3.extent(pie_data, d => d.course_area_percentage))
.range(["#d7191c", "#f19d00", "#eef200", "#3fe256", "#008e15"]);
Here is the demo:
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>
Source: stackoverflow.com
Related Query
- D3.js pie chart color scale
- How to customize color in pie chart of NVD3
- Same color is showing in different arc of d3 pie chart
- How do I color labels for my pie chart in D3?
- Assign specific color to specific segment in D3 Pie Chart
- Pie chart avoiding same adjacent color when rotating colors
- changing color of d3.js pie chart label when hovering over slice?
- D3 pie chart zero data point changes arc fill color
- d3 pie chart color assignment to arcs
- Get Selected Slice Color from Pie chart using DC.js
- How can I draw pie chart with custom color in d3.js
- dc.js color pie chart slice color with color domain
- D3 pie chart rendering in only one color
- D3.js: Pie graph, different color of chart separators?
- how can set customize color to pie chart using D3.js?
- How to set text color for my d3 chart title?
- d3 color scale - linear with multiple colors?
- D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain
- How to customize the color scale in a D3 line chart?
- Using an ordinal scale ('d3.scale.ordinal') for the x-axis in a bar chart
- How to add drop shadow to d3.js pie or donut chart
- Pie (donut) chart segment order in D3
- D3 - Pie Chart & Force Directed Labels
- d3 show number instead of percentages on pie chart
- d3.js pie chart with angled/horizontal labels
- Preventing overlap of text in D3 pie chart
- Continuous color scale from discrete domain of strings?
- Space out part of the pie chart in d3?
- D3 put arc labels in a Pie Chart if there is enough space
- How to make a color scale in D3 JS to use in fill attribute?
More Query from same tag
- d3 enter() / merge() creates copies instead of updating
- Why does the d3 force layout seem to crash and how should I overcome it?
- Have to save text height in d using d3 library
- Rotation transformation of labels on x-axis
- Set domain on ordinal scale from tsv data
- D3 treemap node order
- Update Pattern D3
- enter() d3.js doesn't work on a html.erb file ruby rails
- d3.nest adding extra variables
- d3 contextmenu not firing
- D3 LineGraph Circles Remove Overlap && Sort dates
- d3 Bar Chart not showing up or giving me any errors to work from
- d3 custom number formatting
- D3 V5 Word Wrapping in Circle
- d3.js bisector with unix timestamp
- Cubism - Display of values in axis
- d3 bar graph error says xScale.bandwidth() is not a function
- D3 update x coordinate element
- Confused by this Javascript shorthand
- Non-overlapping nodes in Forced-Directional Graph
- d3 reduce method is returning NaN
- d3.js Les Miserables JQuery Visualisation
- D3 scatter chart dynamic axis positions
- angular.js and d3.js - geometric example
- Missing quotes in Json file
- user-scalable=no but still able to scale entire page on web map application?
- How to approach the Data Visualization aspect of D3?
- Is there a way to add a highlight to pie chart in d3?
- How to format D3 axis using a unix timestamp with timezone from postgres
- Basic D3.js: how to use joins within a function?