/    Sign up×
Community /Pin to ProfileBookmark

I am new to javascript and I’m a little stuck

Hi everyone– I am new to javascript and I am stuck. Basically, I have created a map with a bunch of dots on it, each dot representing a person. I want to be able to click on the dot and it show the information as

Name: _______
Gender: _
__
___
Age: _
__
_____

at the moment, its like this: Name: _______ , Gender: ______, Age: ________

And I also want to have it that when I click the next dot, the previous dot of information disappears. At the moment they all show up like this:[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-04-21/1524270541-638043-screen-shot-2018-04-20-at-72852-pm.png]

Here is the code :

//draw dots for shootings
g.selectAll(‘.shots’)
.data(shootings)
.enter()
.append(‘circle’)
.attr(‘cx’, function(d){return proj([d.Longitude,d.Latitude])[0]})
.attr(‘cy’, function(d){return proj([d.Longitude,d.Latitude])[1]})
.attr(‘r’,1.5)
.attr(‘opacity’,.7)
.attr(‘class’,’shots’)
.attr(‘fill’,function(d){
//change dot color depending on dataset
if(d.signs_of_mental_illness == “True”){
return ‘#e0ffff’
}
else{
return ‘#90ee90’
}
})

//returns information
.on(‘click’, function(d){

g.append(‘text’)
.attr(‘x’,
proj([d.Longitude,d.Latitude])[0]
)
.attr(‘y’, proj([d.Longitude,d.Latitude])[1]).text(“name: “+ d.name + “, gender: ” + d.gender + “, age: ” + d.age )
.attr(‘fill’, ‘white’)
.attr(‘font-family’, “Lato”)
.attr(‘font-size’, 16)

//when the user clicks one of the dots, change the data and the previous disappears.

var topic = “total”;
d3.select(“#total”).style(‘background-color’, ‘#A147AD’);

//when the user clicks one of the switches, change the data
d3.select(‘body’)
.selectAll(‘.shots’)
.on(‘click’, function(d){
//reset all buttons
d3.selectAll(‘.shots’).style(‘background-color’, ‘#333’)

//make the background of selected button a different color
d3.select(this).style(‘background-color’, ‘#A147AD’)

//change topic variable to hold the data that the user wants to see
topic = this.id;

//run the update routine
update(this.id);
})


})
})
})
})

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumApr 21.2018 — Obviously you are using d3.js, you should have mentioned this. Inserting a line break in a text element seems to be not as easy as in HTML. A possible solution is described here:

https://stackoverflow.com/questions/13049050/can-you-insert-a-line-break-in-text-when-using-d3-js

And I also want to have it that when I click the next dot, the previous dot of information disappears.[/quote]
Apply a class to the text element and when another dot is clicked select all elements having this class and remove them like this:

https://stackoverflow.com/questions/20477224/removing-svg-text-elements-with-d3-js
Copy linkTweet thisAlerts:
@rootApr 21.2018 — and note... JQUERY is not JavaScript..! Its a "JavaScript Framework" as it is written in the JavaScript language but by itself, will not run, it needs that Library (Framework) to support it otherwise the script does nothing.
×

Success!

Help @d12 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...