Skip to content

Commit 17edf42

Browse files
committed
adding plotly_click event
1 parent 62c9d65 commit 17edf42

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

FAQ.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44
## How to disable plotly events ?
55

66
It's possible disable some plotly events returning `false` in its event. Please see issue: https://github.com/plotly/angular-plotly.js/issues/52#issuecomment-476018478
7+
8+
9+
10+
## Why using `(plotly_click)` instead of `(click)` ?
11+
12+
Angular uses the `(click)` directive to itself. If we use the `click` name as event alias to `plotly_click` we might get unexpected behaviour from both angular and plotly.js. We believe it is simpler to just avoid using the same name is better.
13+
Please see issue: https://github.com/plotly/angular-plotly.js/issues/63
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div>
2-
<plotly-plot [data]="graph.data" [layout]="graph.layout" [revision]="0" [debug]="true" [useResizeHandler]="true"></plotly-plot>
2+
<plotly-plot (plotly_click)="onClick($event)" [data]="graph.data" [layout]="graph.layout" [revision]="0" [debug]="true" [useResizeHandler]="true"></plotly-plot>
33
</div>

src/app/demo/fancyplot/fancyplot.component.ts

+4
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ export class FancyplotComponent implements OnInit {
2020
ngOnInit() {
2121
}
2222

23+
onClick(event: any) {
24+
console.log('click!');
25+
}
26+
2327
}

src/app/shared/plot/plot.component.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class PlotComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
6363
@Output() beforeExport = new EventEmitter();
6464
@Output() buttonClicked = new EventEmitter();
6565
@Output() click = new EventEmitter();
66+
@Output() plotly_click = new EventEmitter();
6667
@Output() clickAnnotation = new EventEmitter();
6768
@Output() deselect = new EventEmitter();
6869
@Output() doubleClick = new EventEmitter();
@@ -83,7 +84,7 @@ export class PlotComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
8384
@Output() unhover = new EventEmitter();
8485

8586
public eventNames = ['afterExport', 'afterPlot', 'animated', 'animatingFrame', 'animationInterrupted', 'autoSize',
86-
'beforeExport', 'buttonClicked', 'click', 'clickAnnotation', 'deselect', 'doubleClick', 'framework', 'hover',
87+
'beforeExport', 'buttonClicked', 'clickAnnotation', 'deselect', 'doubleClick', 'framework', 'hover',
8788
'legendClick', 'legendDoubleClick', 'relayout', 'restyle', 'redraw', 'selected', 'selecting', 'sliderChange',
8889
'sliderEnd', 'sliderStart', 'transitioning', 'transitionInterrupted', 'unhover'];
8990

@@ -98,6 +99,13 @@ export class PlotComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
9899
const figure = this.createFigure();
99100
this.initialized.emit(figure);
100101
});
102+
103+
104+
if (this.plotly.debug && this.click.observers.length > 0) {
105+
const msg = 'DEPRECATED: Reconsider using `(plotly_click)` instead of `(click)` to avoid event conflict. '
106+
+ 'Please check https://github.com/plotly/angular-plotly.js#FAQ';
107+
console.error(msg);
108+
}
101109
}
102110

103111
ngOnDestroy() {
@@ -188,6 +196,11 @@ export class PlotComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
188196
plotlyInstance.on(eventName, (data: any) => (this[name] as EventEmitter<void>).emit(data));
189197
});
190198

199+
plotlyInstance.on('plotly_click', (data: any) => {
200+
this.click.emit(data);
201+
this.plotly_click.emit(data);
202+
});
203+
191204
this.updateWindowResizeHandler();
192205
}, err => {
193206
console.error('Error while plotting:', err);

src/app/shared/plotly.service.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { Plotly } from './plotly.interface';
3+
import { environment } from '../../environments/environment';
34

45
type PlotlyName = 'Plotly' | 'ViaCDN' | 'ViaWindow';
56

@@ -40,6 +41,10 @@ export class PlotlyService {
4041
}
4142
}
4243

44+
public get debug(): boolean {
45+
return environment.production === false;
46+
}
47+
4348
public getInstanceByDivId(id: string): Plotly.PlotlyHTMLElement | undefined {
4449
for (const instance of PlotlyService.instances) {
4550
if (instance && instance.id === id) {

0 commit comments

Comments
 (0)