score:1
dc.js doesn't have anything built-in to display properties, so you'll need to create a div somewhere and populate it with the data you receive on click.
As for coloring the clicked dot, dc.js doesn't have click-selection of dots in a scatterplot, and you wouldn't want it to filter on the one dot.
So you're on the right track implementing it yourself; you just need to change the color of that dot.
indicating selection using color
Since you're in a click handler for the dot, you have the element in this
, and the simplest way is
d3.select(this).attr("fill", "black")
The problem is that the chart doesn't know what you did, and when it redraws it will change the color back. So the better way is to keep track of the selection e.g. a global variable, and then check it in the colorAccessor
and return a different color for the selected dot:
var _selection = null;
.colorAccessor(function(d) {
if(_selection) {
if(d.key[0] === _selection[0] && d.key[1] === _selection[1])
return 5; // or a value that makes sense with your color scale
}
return chart._groupName; // or whatever color logic you use
})
chart.on('renderlet', function(chart) {
chart.selectAll('path.symbol').style('cursor', 'pointer').on('click', function(d) {
_selection = d.key;
chart.redraw();
})
})
indicating selection using size
Unfortunately the dc.js scatter plot does not currently support a size accessor. It sure would be useful!
I tried fiddling with the transform, but I couldn't get that to work because that's also one of the parameters that the scatter plot already controls.
One encoding that is still free is the stroke outline of the dots. By default it's set to stroke: none
in dc.css but I got some decent results by overriding it with .style()
.
With a stroke-width
of 8:
stroke-width
25 (what does this even mean):
chart.on('pretransition', function(chart) {
chart.selectAll('path.symbol').style('cursor', 'pointer')
.style('stroke-width',
d => (_selection && d.key[0] === _selection[0] && d.key[1] === _selection[1]) ?
25 : null)
.style('stroke',
d => (_selection && d.key[0] === _selection[0] && d.key[1] === _selection[1]) ?
'green' : null)
.on('click', function(d) {
_selection = d.key;
chart.redraw();
})
})
hint (but no code, sorry) for implementing in canvas mode
If you're using the new(ish) Canvas support in the scatter plot, there is no DOM for the symbols, so two things won't work here:
- no symbol objects to receive click events.
- no symbol objects to set stroke or stroke-width on.
Sorry this is just an outline but using dc.js I think you'd have to
- listen for clicks on
chart.svg()
(the chart<svg>
element) - use the chart scales to convert click coords to data coords
- use a quadtree to detect if a symbol was hit. or maybe even voronoi/delaunay for approximate hits
- add an SVG overlay and draw a selection marker there
The first three steps will be the same as the example you linked. The fourth step is because there is currently no good way to change the way symbols are drawn, and you can't modify canvas after it's drawn without redrawing the whole thing.
Please try this out and ask another question if you run into trouble. I'm happy to give it a shot but I think this question and answer are played out.
Source: stackoverflow.com
Related Query
- How can I select a dot from a scatter and display data about it?
- How can i bind an json data with a key and Name for display
- how to get data from mongodb and display as a d3 chart using nodejs
- C3.js Scatter plot - how to associate third data (additional value) as part of original column data for callbacks to set radius and display in tooltip
- How to select elements from array field in dc.js / crossfilter / d3 and use each of them as separate data point for chart?
- Using d3.js, how can I display data faster on my chart?
- How to select unique values in d3.js from data
- How to pull data from mysql database and visualize with D3.JS?
- How can I plot positive and negative values for both axes in a scatter plot with d3?
- How can I rotate d3 chart and how can I start pie chart from angle -90?
- How can D3 select multiple shapes with shared attributes from a group?
- How can I efficiently convert data from string to int within a d3 method chain?
- D3.js: How to select data from graph's brushed area?
- How can I get a list of tree-ancestors and tree-descendants from this d3.js layout.tree?
- How can I identify scatter plot data points that are included in a D3 brush?
- How can I apply a time scale and show my data in intervals of "n" years?
- How can I append string to y-axis data with tick and d3.format?
- How can I display tooltips from multiple maps when mousover on one of the maps in D3js
- Changing time scale from days to weeks, how do I update and sum data values?
- how to display data from 1st point on words on y axis for line chart in d3.js
- how can update the name of the legends and tooltip in a scatter plot?
- Using d3 in meteor on windows 8.1, how can I display a world map, zoomed in on the US, with country borders and US state borders?
- How can I specify a d3.js selector using values from a data object?
- How can I scale my map to fit my svg size with d3 and geojson path data
- d3js: How to select only x number of values from data
- How to make xscales and xaxis from this data D3 V4
- How can i made a dot chart from d3.js template 'Reusable Charts'
- How can you draw a collection of lines and points based on categorical data in D3?
- How to select a <tr> from the same data index as a scatterplot <circle>
- Confused about data joins, select and selectAll
More Query from same tag
- How can I wrap text on the each arc in D3.js?
- Why is my tooltip not showing up?
- d3js - Creating Asterplot-like Charts (example included)
- why doesn't d3-geo-zoom pan correctly for more then 1 canvas elements on the page?
- Using d3 to create a density & value heatmap layer for leaflet
- Rails 3 generating json file to use it in javascript library - D3.js
- NVD3.js multichart with fixed x- and y-axis
- Change bar chart to line chart in D3
- Variable is undefined when angular script loads
- Omitting trailing zeroes / using sigfigs with d3.format()
- Updating D3 chart resulting in duplicate charts
- make a tabpanel-like with SVG elements
- D3: remap mousewheel to be panning gesture instead of zoom gesture
- D3.js display Topojson
- make a chart of X vs Y
- d3.js 404 json file not found
- get all nodes that is connected through their links
- why graph is getting blank after dragging it
- Added image to DOM element in d3 but now it won't transition
- d3.json() to load large file
- Show tooltip on the D3 map
- dynamically add input to js d3
- SVG D3 Scrolling with max height
- d3 .data key initially comes back undefined
- d3.js onclick event uses the value of variable at time of click instead of at time of drawing
- D3.js Styling a highlighted Table
- how to load geometryCollection of geojson file in d3
- D3 js BoxPlot with already calculated data
- Displaying some text on States on Map using D3.js and GeoJson
- Can d3.js graph put inside a table?