score:1
Accepted answer
I did some refactoring for your code. And put it on jsfiddle - https://jsfiddle.net/kdj9xq98/ It's works:
var svg = d3.select('#root').append('svg')
.attr('width', 400)
.attr('height', 400);
var circ = svg.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 20);
function fillCircle(red) {
circ.style("fill", d3.rgb(red, 0, 0));
}
// Initial values on page load
fillCircle(100);
var sliderScale = d3.scaleLinear()
.domain([0, 255]) // Red component goes from 0 to 255
.range([0, 200]) // Width of slider is 200 px
.clamp(true);
var slider = svg.append("g")
.attr("class", "slider")
.attr("transform", "translate(" + 13 + "," + 200 + ")");
slider.append("line")
.attr("class", "track")
.attr("x1", sliderScale.range()[0])
.attr("x2", sliderScale.range()[1])
.select(function() {
return this.parentNode;
})
.append("line")
.attr("x1", sliderScale.range()[0])
.attr("x2", sliderScale.range()[1])
.attr("class", "track-inset")
.select(function() {
return this.parentNode;
})
.append("line")
.attr("x1", sliderScale.range()[0])
.attr("x2", sliderScale.range()[1])
.attr("class", "track-overlay")
.call(d3.drag()
.on("start.interrupt", function() {
slider.interrupt();
})
.on("start drag", function() {
changeRed(sliderScale.invert(d3.event.x));
}));
var handle = slider.insert("circle", ".track-overlay")
.attr("class", "handle")
.attr("r", 9);
function changeRed(h) {
handle.attr("cx", sliderScale(h));
fillCircle(h);
}
Source: stackoverflow.com
Related Query
- Using D3 slider to change attribute
- d3.js - using slider to dynamically change graph
- Using a range slider to change the radius of an SVG circle
- D3: Using node attribute for links instead of index in array
- Invalid value for <circle> attribute cx=“NaN” using D3.js
- D3 color change on mouseover using classed("active",true)
- Invalid value for <circle> attribute cx="NaN" using NVD3.js
- d3 change attribute of all elements
- Change a shadow opacity partially in svg using filters and d3.js
- Change object attribute following transition D3
- mpld3: How to change the location of the toolbar using a plugin?
- D3: Select SVG attribute and change it
- How to change alignment of nodes in a Sankey diagram using D3?
- Set the percentage of animation in a transition of an element (forward, backward) using a slider
- How to select and deselect a svg line and change its color on selection and deselection using d3.js and javascript , jquery?
- How to change color of donut chart created using d3.js?
- How to change point size using geojson file in D3
- How to change color of line in line chart using dimple.js?
- Change button text using D3js
- Change the Class attribute
- <g> attribute transform="translate(NaN,NaN)" when using Interactive Guideline
- Change jQuery slider to SVG Slider
- Change attribute back to before calculated attribute in d3?
- Changing attribute of a DOM element using D3 data that is bound to it
- d3 chart update automatically when data change by using vue
- Time slider to move automatically using setInterval()
- How to change the circles style using d3.js
- How to concatenate a return function into a SVG transform attribute using D3.js
- How to change json file in d3.js and update chart using select options
- R shiny: Change "height" attribute of rectangle in gvisTimeline
More Query from same tag
- How to make an area chart if a condition is true otherwise make a line chart?
- d3.js table tooltip not disappearing
- What do sub-transitions inherit?
- d3-drag - rescale y-axis with drag in d3v5
- stacked dynamic line graph d3.js
- Use an IIFE approach and pass a variable from one file to another
- How so create complex SVG shapes using D3JS?
- Converting a flat JSON Object to a Tree Data Structure
- Adding a nvd3 sparkline chart inside a dynamic table
- TypeError: Cannot read property 'createElementNS' of undefined
- How to pass a value to line generation function in D3
- loop d3 transition a finite number of times
- D3.js - Date ticks don't align with data path - offset to the right
- What is the equivalent of `link` function in Angular to access DOM elements
- d3 prevent firing of mouseout on contextmenu
- How to output PHP result in plottable format
- D3.js ~ How to filter data values containing certain values/words?
- I am trying to extract an element from the DOM, how is this done?
- labels wont show up on d3 force diagram
- unique id , id name for generated rectangles in D3
- Static D3 Force-Directed Graph performance
- d3 - draw smooth line if two circles or points are close to each other
- Is there a way to wrap rects (or other objects) using d3
- append a circle in middle of all my path (sunburst)
- Appended bars wont draw on svg element
- how to draw rectangles for each column in a row in d3.js
- Dc.js no update between charts
- D3 line chart legend not working with v3 but working with v2
- How can I determine if a point is hidden on a projection?
- Set unique bubble color in D3 Bubble Chart