diff --git a/examples/right_click.html b/examples/right_click.html new file mode 100644 index 0000000..692d69a --- /dev/null +++ b/examples/right_click.html @@ -0,0 +1,33 @@ + + + + + + + + +
+ + + diff --git a/package.json b/package.json index b029f67..e993309 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@chartwerk/line-pod", - "version": "0.7.6", + "version": "0.7.7", "description": "Chartwerk line chart", "main": "dist/index.js", "files": [ diff --git a/src/index.ts b/src/index.ts index f6d187f..5b209d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ class LinePod extends ChartwerkPod { this.clearAllMetrics(); this.updateCrosshair(); + this.updateEvents(); if(!this.series.isSeriesAvailable) { this.renderNoDataPointsMessage(); @@ -59,6 +60,16 @@ class LinePod extends ChartwerkPod { this._markersLayer?.clear(); } + protected updateEvents(): void { + // overlay - core component that is used to handle mouse events + if(!this.overlay) { + return; + } + if(this.options._options.events?.contextMenu === undefined) { + return; + } + this.overlay.on('contextmenu', this.onContextMenu.bind(this)); + } getRenderGenerator(renderArea: AreaType, yOrientation: yAxisOrientation): Generator { const yScale = yOrientation === yAxisOrientation.LEFT ? this.state.yScale : this.state.y1Scale; @@ -453,8 +464,19 @@ class LinePod extends ChartwerkPod { } } + protected onContextMenu(): void { + d3.event.preventDefault(); // do not open browser's context menu. + const eventX = d3.mouse(this.chartContainer.node())[0]; + const eventY = d3.mouse(this.chartContainer.node())[1]; + + this.options._options.events.contextMenu({ + x: this.state.xScale.invert(eventX), + y: this.state.yScale.invert(eventY), + }); + } + // override parent updateData method to provide markers and segments -protected updateLineData( + protected updateLineData( series?: LineTimeSerie[], options?: LineOptions, markersConf?: MarkersConf, diff --git a/src/types.ts b/src/types.ts index a67e1d4..d5493a3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -23,6 +23,10 @@ export type LineOptions = Options & { x: number; y: number; }, range: AxisRange[]) => void; + contextMenu?: (position: { + x: number; + y: number; + }) => void; } }