Skip to content

Commit

Permalink
Merge branch '3-uses-react-component-for-popups'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sankho Mallik committed Jul 25, 2016
2 parents 033cb87 + ca3bf45 commit c4d3781
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
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';

const MAP_CLEARING_PROPS = [
'setProjection', 'scope', 'height', 'width'
Expand Down Expand Up @@ -69,7 +69,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
43 changes: 43 additions & 0 deletions src/extended_datamaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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() {
hoverObj
.style('display', 'none')
.html('');
});
}

}

0 comments on commit c4d3781

Please sign in to comment.