score:29
Accepted answer
You want to split the problem up. First define a scale for your heatmap that maps 0-1 to your colours. Then define a second (dynamic) scale that maps your dataset to 0-1. You can then combine the scales to paint your shapes.
var colours = ["#6363FF", "#6373FF", "#63A3FF", "#63E3FF", "#63FFFB", "#63FFCB",
"#63FF9B", "#63FF6B", "#7BFF63", "#BBFF63", "#DBFF63", "#FBFF63",
"#FFD363", "#FFB363", "#FF8363", "#FF7363", "#FF6364"];
var heatmapColour = d3.scale.linear()
.domain(d3.range(0, 1, 1.0 / (colours.length - 1)))
.range(colours);
// dynamic bit...
var c = d3.scale.linear().domain(d3.extent(dataset)).range([0,1]);
// use the heatmap to fill in a canvas or whatever you want to do...
canvas.append("svg:rect")
.data(dataset)
.enter()
// snip...
.style("fill", function(d) {
return heatmapColour(c(d));
Plus you can use the d3.extent function to get the min and max of the dataset in one go.
score:3
Use threshold scales. Here is a quick example:
coffee> d3 = require 'd3'
coffee> color = d3.scale.threshold().domain([5,30,100]).range(["red","orange","green"]);
coffee> color 6
'orange'
coffee> color 3
'red'
coffee> color 33
'green'
score:4
Use a Quantitative Scale plus Color Brewer
// pick any number [3-9]
var numColors = 9;
var heatmapColour = d3.scale.quantize()
.domain(d3.extent(dataset))
.range(colorbrewer.Reds[numColors]);
// use the heatmap to fill in a canvas or whatever you want to do...
canvas.append("svg:rect")
.data(dataset)
.enter()
// snip...
.style("fill", function(d) {return heatmapColour(d);})
Source: stackoverflow.com
Related Query
- D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain
- Create a padding for the first and last data points using a time scale
- how to make sankyplots with no color for source and target node and how to do legend to show the different colors of linkgroup
- Create a scale for bands with different width in D3.js
- D3.js: Set fitExtent or fitSize (or center and scale programmatically) for map with multiple geoJSON files
- d3.js - logarithmic scale with minus and zero in range
- d3v4 checkout chart - making sure the color scheme is correct for the legend and circles
- d3js gantt chart with date/time scale at the top and current day blue line
- Given a range of date intervals, create an array of all 1440 minutes in a day with the frequency each minute appears in the source intervals
- How to display d3 bubbles in different colors for a dataset with one branch and many children?
- How would I create my own color scale based on the id of my data in d3?
- How to scale an integer to date with range and domain
- D3: scale and color for choropleth map
- How to interpolate or get color for the point/values not in specified color table range
- Time in x-axis and data for the y-axis for line chart in d3.js doesn't match/show with what (data) is on JSON
- dc.js with crossfilter and d3, how to load multiple csv files at once, without knowing how many files the user will upload
- How to Maintain d3 Color Scale with Link Severing and Re-joining
- Calendar heatmap: how to define the range of % for each color instead of using quantiles
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
- Draw a d3 trend line with different color and width for different segments
- Use the right color scale for data skewed to right
- How do I change the colour of my d3.js Bar Graph bars individually and make a Scale for them?
- How to create a pie chart visualization in d3.js, typing the input manually and with smooth transitions
- vertical bars not aligned with the x-axis label and last bar rendered outside the x-axis range - D3 js - simple bar with fixed width
- d3.js get selected nodes and apply color for all the selected nodes
- How does the following code get away with not defining initial x and y coordinates for nodes?
- How do I create and write to a new file (csv) and have the browser download it with javascript or D3.js?
- Understanding how to select the Range for a scale
- D3.js color with domain a set of colors and range a subset of colors
- Create color range with radial gradients using d3.js
More Query from same tag
- d3.js performace optimization with selective updates
- d3js set tickValues for time scale axis
- Table based on csv data not updating properly d3js v4
- How to access key values in an object using d3.js?
- Adding a chart legend in D3
- Integrate Datatables plugin with the help of D3.js
- D3 and Leaflet - svg circles not showing
- d3/chrome click/zoom bug?
- How can I attach dynamic objects to an HTML element?
- Vertical sort of nodes in D3 Sankey diagram
- Changing text attribute based upon event handler in d3.js
- Spline path - 3D effect
- Recursive/recurring animation events in D3
- How to place node title to the left or right of the node in d3 Sankey graph?
- force directed graph different cluster for different groups
- How to subset a dataset using d3.js according to change in dropdown
- D3 to CSS color for stacked bar graph
- How to select multiple selectors with selectAll?
- Is there a D3 Gauge allowing multiple series/columns?
- Update/toggle/filter bar graph based on user's <select>
- How can I color ocean with topojson in d3 when I have coordinate info for land?
- D3 path fill is giving weird result
- Format tick values in D3
- Why is d3.histogram merging the last two bins of this chart?
- Are node x and y automatically set in force layout graphs?
- d3.rebind in D3 v4
- Relationship Graph with D3
- Relation between LinkDistance and LinkStrength in D3.js Force layout
- this.setAttribute is not a function when using D3's .attr method
- How to enable qtip2 in Piechart made by Plottable.js/D3.js