score:0
Seems like the temps
array has numbers with decimal points. If the temps
array values lies between the numRange
, you can just round the temp value to fix this issue.
.attr("fill", function(d, i) {
return color(Math.round(d));
});
var temps = [25.2, 1.45, 240.25, 3.1],
coors = [{
x: 10,
y: 20
}, {
x: 50,
y: 60
}, {
x: 50,
y: 80
}, {
x: 90,
y: 100
}];
var color_chart = d3.select("body")
.append("svg")
.attr("width", 1000)
.attr("height", 500);
var numRange = [1, 1.5, 2, 2.5, 3, 4, 5, 7, 10, 15, 25, 45, 70, 100, 150, 240],
colorRange = ["#0500FF", "#0100FF", "#0022FF", "#0064FF",
"#00A4FF", "#00E4FF", "#00FF83", "#17FF00",
"#B0FF00", "#FFF000", "#FFC800", "#FFA000",
"#FF7800", "#FF5000", "#FF2800", "#FF0000"
],
gradientRange = [];
var defs = color_chart.append("defs");
for (var i = 0; i < colorRange.length; i++) {
var gradient = defs.append("radialGradient")
.attr("id", "radial-gradient" + i);
gradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", colorRange[i])
.attr("stop-opacity", 1);
gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", colorRange[i])
.attr("stop-opacity", 0);
gradientRange.push("url(#radial-gradient" + i + ")");
};
console.log(numRange);
console.log(gradientRange);
var color = d3.scale.linear()
.domain(numRange)
.range(gradientRange);
color_chart.selectAll("circle")
.data(temps)
.enter()
.append("circle")
.attr("cx", function(d, i) {
return coors[i].x
})
.attr("cy", function(d, i) {
return coors[i].y
})
.attr("r", 10)
.attr("fill", function(d, i) {
return color(Math.round(d));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- Create color range with radial gradients using d3.js
- D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain
- Using d3.js to create a heatmap, having issues with color
- Create an interactive chart (zoom/pan) with lines using D3
- d3: Color Range with an array with more than 2 colors
- How do I create a multiline chart using d3js with json formatted for nvd3?
- Using scaleQuantile with "viridis" color scheme
- How to create a % difference arrow line with value in a bar chart using D3.js
- 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 color SVG image based on value from color range using d3?
- How to create SVG with grid lines using D3JS
- Creating a radial chart with a different color for each axis label
- D3 heatmap color range updating with slider
- Create rect with blurred stroke in svg using d3.js
- Displaying values associated with each color gradient mapped from range in D3 legend
- Create d3 linear color legend using d3 colors
- Calendar heatmap: how to define the range of % for each color instead of using quantiles
- Create scatter plot with unequal intervals on x-axis using d3.js
- How to create a heatmap with numbers using Leaflet map?
- Problems making radial gradient in arcs using javascript, d3 with data from json file
- Using nest and rollup to create a line chart with a mean in d3 v4
- Using Jquery Mobile to create charts using d3js with fixed width
- Using datamaps, I am trying to create a map with not 100% opacity
- Using datamaps, I am trying to create a map with not 100% opacity
- D3.js color the node circles with a function using spectrum.js
- create a radial chart with d3.js
- trying to create a donut chart with labels inside the curve using d3.js
- Create a multiple line chart with Text data in x-axis using d3.js
- border each node in a network created using networkD3::forceNetwork() with a specific color
- Return individual value from a D3 color range to create legend
More Query from same tag
- Use D3.js single line graph to display same date but different temperatures
- Traversing JSON to buid a parent child output with Javascript
- d3.json request successfully but got an error message
- D3 v5 area chart not being generated
- Filter multiple rows of a CSV in D3.js and update filter on keyboard input
- Zoomable sunburst visualization comes out messed up
- How can I give this radial cluster straight lines instead of curved lines?
- Y axis some text cut of in d3js bar chart
- d3js create a force layout with fixed nodes
- Merging arrays that hold CSV data into one object array
- How to display the total value of a stacked bar chart in d3js
- Multiline svg text axis ticks in d3
- How to remove a paticular legend in D3
- Add event listener per nodes level on d3 chart
- Converting CSV to JSON - jQuery
- D3.js Multi-Series Chart with Y value tracking
- D3 Scale doesn't match graph
- Repeat an object with different input data
- Using D3 to retrieve a TopoJSON U.S. county from coordinates
- D3 Adding arrowheads to edges, directed graph
- Bar chart with d3.js and an associative array
- Uncaught TypeError: Cannot read property 'startAngle'
- Once I put a function into defer() in D3, the data structure inside console.log changed in other function
- Beeswarm plot, split "swarm" y value based on boolean
- Function create graph in C3js don't show inside a event Meteor.js
- d3 chart + jQuery DataTables: trouble reading nested array
- d3v4 scatterplot - fixing the domain adding animations
- Generate a shadow on circle click with D3.js
- D3js pack-layout visualization of generated json file does not work
- Global array not updating with d3.json