score:-1
Accepted answer
I change your code structure.
It seems you don't understand d3 update pattern.
all working example is here https://scrimba.com/c/cv8wD7H2
var dataSet = [
{hour: "0.1",yval: "0.2",foo: "0", goo:"0", bar:"1", boo:"0"},
{hour: "14",yval: "0.4",foo: "0", goo:"1", bar:"0", boo:"0"},
{hour: "15",yval: "0.3",foo: "0", goo:"0", bar:"0", boo:"1"},
{hour: "19",yval: "241",foo: "0", goo:"0", bar:"0", boo:"0"},
{hour: "22",yval: "0.2",foo: "0", goo:"0", bar:"1", boo:"1"},
{hour: "08",yval: "118",foo: "1", goo:"0", bar:"1", boo:"0"},
{hour: "22",yval: "48",foo: "0", goo:"1", bar:"0", boo:"1"},
{hour: "21",yval: "31",foo: "1", goo:"0", bar:"1", boo:"1"},
{hour: "12",yval: "38",foo: "0", goo:"1", bar:"0", boo:"0"},
{hour: "16",yval: "138",foo: "1", goo:"1", bar:"0", boo:"0"},
{hour: "05",yval: "344",foo: "0", goo:"1", bar:"1", boo:"1"},
{hour: "08",yval: "218",foo: "0", goo:"0", bar:"1", boo:"0"},
{hour: "03",yval: "18",foo: "1", goo:"0", bar:"0", boo:"0"},
{hour: "18",yval: "98",foo: "1", goo:"0", bar:"0", boo:"1"},
{hour: "18",yval: "98",foo: "0", goo:"1", bar:"1", boo:"0"},
];
const COLOR_SET = {
"goo": {
"0:0": "blue",
"1:1": "green",
"1:0": "red",
"0:1": "violet"
},
"bar": {
"0:0": "green",
"1:1": "red",
"1:0": "violet",
"0:1": "blue"
},
"boo": {
"0:0": "red",
"1:1": "violet",
"1:0": "blue",
"0:1": "green"
}
}
function getColorByColorSet(colorSetName) {
return function(data) {
try {
return COLOR_SET[colorSetName][ data.foo +':'+data.goo ]
}catch(e) {
console.error(colorSetName, data)
throw e;
}
}
}
function MyScatterPlot(options={}) {
var {w=500,h=500,top_pad=20,left_pad=100,target='body',dataSet=[],colorSetName='goo'} = options;
this.dataSet = dataSet;
this.colorSetName = colorSetName;
// prepare
this.xScale = d3.scale.linear()
.domain([-1, 23])
.range([left_pad, w - left_pad])
this.yScale = d3.scale.linear()
.domain([0, 350])
.range([h - top_pad, top_pad]);
this.svg = d3.select("body")
.append("svg:svg")
.attr("width", w + left_pad)
.attr("height", h + top_pad);
this.symbolTypes = {
"circle": d3.svg.symbol().type("circle")
};
var fooColors = d3.scale.linear().domain([0, 50]).range(["#87CEFF", "#0000FF"]);
var xAxis = d3.svg.axis().scale(this.xScale).orient("bottom");
var yAxis = d3.svg.axis().scale(this.yScale).orient("left");
this.svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0, " + (h - top_pad) + ")")
.call(xAxis);
this.svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + (left_pad) + ", 0)")
.call(yAxis);
// end of prepare
}
MyScatterPlot.prototype.changeColorSet = function(selectedColorsetName) {
this.colorSetName = selectedColorsetName;
this.svg.selectAll("path.dot")
.style("fill", getColorByColorSet(this.colorSetName))
}
MyScatterPlot.prototype.setData = function(dataSet) {
this.dataSet = dataSet
let allDots = this.svg.selectAll("path.dot").data(this.dataSet)
allDots.exit().remove();
allDots.enter().append("path")
.attr("class", "dot")
.attr("d", (d,i)=>{return this.symbolTypes.circle();})
allDots.attr("transform", (d) => {
return "translate(" + (this.xScale(d.hour) + (Math.random() * 12 - 6)) + "," + this.yScale(d.yval) + ")";
}).style("fill", getColorByColorSet(this.colorSetName))
}
var scatterChart = new MyScatterPlot()
scatterChart.setData(dataSet)
let myUpdateFunction = function(clickedButtonName) {
console.log('clickedButtonName : ' + clickedButtonName)
scatterChart.changeColorSet(clickedButtonName)
}
=== here is another answer for your comment. you need only on colorset. change color set like this
const COLOR_SET = {
"0:0": "blue",
"1:1": "green",
"1:0": "red",
"0:1": "violet"
}
and then change getColorByColorSet() function as you want.
colorSetName
parameter actually has clicked button name. so can not be removed just change parameter name to clickedButton
function getColorByColorSet(clickedButton) {
return function(data) {
var finalColor;
try {
if(clickedButton === 'goo') {
finalColor = COLOR_SET[ data['bar']+':'+data['boo']]
}else if(clickedButton === 'bar'){
finalColor = COLOR_SET[ data['goo']+':'+data['boo']]
}else if(clickedButton === 'boo'){
finalColor = COLOR_SET[ data['goo']+':'+data['bar']]
}
return finalColor
}catch(e) {
console.error(colorSetName, data)
throw e;
}
}
Source: stackoverflow.com
Related Query
- binding a function to a button d3.js - cannot make buttons switch colors in scatterplot
- How to make data() key function to work with TypeScript?
- d3: make the d3.csv function syncronous
- How to make the transform:translate-drag function of d3 more smooth?
- 2 arrays one of values one of colors make D3 bar chart have color at same index of value
- I am trying to use multiple 2 arrays for binding data for svg circles but cannot figure out how
- Binding configuration to a function using a closure
- dc.js - reduce function selected from radio button
- Cannot make labels work on zoomable d3 sunburst
- Javascript button onclick event to call function inside function in module
- How to create elements (links) with 2 or more colors with CSS to make it look like a rainbow?
- How D3 key function in data binding works
- d3.js d3.json is not working - how to make this function work?
- D3.js: Cannot make Y axis scrollable and X axis fixed
- D3js button onclick function to zoom the graph for specific timeframe
- How to make a general function to create crossfilter.js dimensions
- How to switch csv dataset of d3.js graph to another csv by clicking on button
- d3js: How to pass button id to onclick callback function
- How to call javascript function from D3 button
- cannot understand why function keep getting called on d3.js
- Using angular function binding inside .html() in d3
- How to make bars have different colors in d3?
- How to make one function instead of 3 or more for a visualization in d3 to then add highlight when hovering over nodes
- Cannot output legend for D3 quantile function
- D3.js: Pause button not working (in a callback function used for animation)
- how to make ticks function work in d3.js?
- Anonymous function and data binding mechanism explanation on example of D3.js implementation
- Using a function to return a series of colors for stroke
- How do I make sure a function will be called before another in a deeply modularized Backbone, Require.js and D3 app?
- how to make sankyplots with no color for source and target node and how to do legend to show the different colors of linkgroup
More Query from same tag
- D3 bar chart left aligned x-axis
- How to add legend to an aster plot d3 js?
- d3 zoom behaviour after target has been translated
- Fade in bars one by one with D3
- How to visualize multiple graphs with different input files in D3?
- How to draw this D3 chart?
- d3js v5 + topojson v3 Access to the object's properties in the background
- Is there a way to set a default color for the un-selected boxes in heatmap in dc.js when crossfilter is applied?
- Convert an SVG element to a g element using d3.js
- Select top k records for each date
- d3.layout.stack() with 'wiggle' offset issue
- How to restrict number of nodes initially in d3 Force directed graph
- Plot a Network with specified coordinates in D3.js
- calling a function as data in D3.js
- AngularJS, D3 and JQuery in the same page. D3 can't read the correct DOM dimensions
- 'd3.nest()' cannot read property of undefined
- How do display array of Objects in D3.js
- d3 - draw lines n number of times
- d3's tree layout with d3.nest() result
- variables scope in javascript D3
- d3 5+ - Appending circles about a semi-circle or arc
- What is the exact meaning of this phrase in D3.js Path Transitions
- D3 v4 - Pie Chart with connectors outside the pie and with dots.
- Need to understand how node.js has to be configured to return a javascript library
- Problem to show dynamic text on mouseover in D3.js and json file?
- How to make axis label of stacked bar chart in the middle of ticks chart in D3?
- d3.js Throbbing marker points on a map
- Using an array to render the scatterplot graph in place of d3.csv method
- Why aren't my axes aligned with the viewbox in d3?
- Getting some weird bar which is not part of my data