问题
I am using dc.scatterPlot .. Not able to find how can I bind mouse on click to different symbols (data points) in scatter plot. I think I should first access _symbol and then set attribute, but it seems like there is no way to access _symbol of scatter plot or may be I am getting this completely wrong.
Please suggest.
var individualEventchart = dc.seriesChart("#event-individual-chart");
var symbolScale = d3.scale.ordinal().range(d3.svg.symbolTypes);
var symbolAccessor = function(d) { return symbolScale(d.key[0]); };
var subChart = function(c) {
return dc.scatterPlot(c)
.symbol(symbolAccessor)
.symbolSize(8)
.highlightedSize(10)
};
individualEventchart
.margins({top: 25, right: 30, bottom: 30, left: 40})
.width(882)
.height(250)
.chart(subChart)
.x(d3.scale.linear().domain([0,24]))
.brushOn(false)
.yAxisLabel("Severity")
.xAxisLabel("Time of the day")
.elasticY(true)
.dimension(individualDimension)
.group(individualGroup)
.mouseZoomable(false)
.seriesAccessor(function(d) {return d.key[0];})
.keyAccessor(function(d) {return +d.key[1];})
.valueAccessor(function(d) {return d.value;})
.legend(dc.legend().x(450).y(0).itemHeight(20).gap(5).horizontal(1).legendWidth(540).itemWidth(150));
回答1:
Using dc.js doesn't stop you from using d3, it just sets stuff up for you.
So once you have your chart you can use dc.selectAll to get a d3 selection of svg elements in the chart, and then d3.on to attach event handlers to the selection.
Even better, put it in a renderlet so that the handlers get updated when the chart does.
EDIT: renderlets have been deprecated in favor of events, and the pretransition event is usually better than the renderlet event.
Here's example code:
plot.on('pretransition', function() {
plot.selectAll('path.symbol').on('click', function(d) {
// do something here; d contains the data for the clicked symbol
});
});
回答2:
The other answer is correct, but here is a code example of how to do it, start with your dc chart:
dc.dataGrid(".dc-data-grid").dimension(name).group(function (d) {
... etc ...
Then add:
.renderlet(function (grid) {grid.selectAll(".dc-grid-item").on('click', function (d) { MyPopupFunction(d); });
来源:https://stackoverflow.com/questions/22744802/dc-scatter-plot-binding-onclick-event