Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extends datamaps JS class to override updatePopup method #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/datamap.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import Datamaps from 'datamaps';
import ExtendedDatamaps from './extended_datamaps';

export default class Datamap extends React.Component {

Expand Down Expand Up @@ -64,7 +64,7 @@ export default class Datamap extends React.Component {
let map = this.map;

if (!map) {
map = this.map = new Datamaps({
map = this.map = new ExtendedDatamaps({
...props,
data,
element: this.refs.container
Expand Down
45 changes: 45 additions & 0 deletions src/extended_datamaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Datamaps from 'datamaps';
import d3 from 'd3';

export default class ExtendedDatamaps extends Datamaps {

updatePopup(element, d, options) {
const self = this,
data = JSON.parse(element.attr('data-info')),
reactObj = options.popupTemplate(d, data);

if (!React.isValidElement(reactObj)) {
return super.updatePopup(element, d, options);
}

const hoverObj = d3.select(self.svg[0][0].parentNode).select('.datamaps-hoverover');

element.on('mousemove', null);
element.on('mouseenter', null);
element.on('mouseleave', null);

element.on('mouseenter', function() {
hoverObj.style('display', 'block');
ReactDOM.render(reactObj, hoverObj[0][0]);
});

element.on('mousemove', function() {
let position = d3.mouse(self.options.element);

hoverObj
.style('top', (position[1] + 30) + "px")
.style('left', position[0] + "px");
});

element.on('mouseleave', function() {
ReactDOM.unmountComponentAtNode(hoverObj[0][0])

hoverObj
.style('display', 'none')
.html('');
});
}

}