react-radial
Version:
a radial component built with react and resonance
152 lines (136 loc) • 5.72 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React, { Component } from 'react';
import { NodeGroup } from 'resonance';
import * as d3 from 'd3';
import { renderObject } from './renderObject';
var Radial = function (_Component) {
_inherits(Radial, _Component);
function Radial() {
_classCallCheck(this, Radial);
return _possibleConstructorReturn(this, _Component.apply(this, arguments));
}
Radial.prototype.componentDidMount = function componentDidMount() {
this.props.updateData();
};
Radial.prototype.componentDidUpdate = function componentDidUpdate() {
// this needs to be called due to a bug in the resonance v0.9.5, it should otherwise live in the render object
var data = this.props.data;
data.forEach(function (d) {
var buttonFunctions = d.buttonFunctions,
text = d.text;
d3.select('#arc' + text).on('mouseover', function () {
// these are minor faux-dom operations, styling only
d3.select('#arc' + text).style('fill-opacity', 1);
// this id matches the svg group id specified above
document.body.style.cursor = "pointer";
}).on('mouseout', function () {
// these are minor faux-dom operations, styling only
d3.select('#arc' + text).style('fill-opacity', .6);
// this id matches the svg group id specified above
document.body.style.cursor = "default";
}).on('click', function (event) {
d3.event.stopPropagation();
buttonFunctions();
});
});
};
Radial.prototype.render = function render() {
var _this = this;
var _props = this.props,
innerRadius = _props.innerRadius,
outerRadius = _props.outerRadius,
stroke = _props.stroke,
strokeWidth = _props.strokeWidth,
duration = _props.duration,
delay = _props.delay,
cx = _props.cx,
cy = _props.cy,
data = _props.data,
fill = _props.fill;
var totalRadius = innerRadius + outerRadius;
var size = totalRadius * 2 + strokeWidth * 2;
return React.createElement(
'div',
{ style: {
position: 'fixed',
zIndex: 20000,
width: size + 'px',
height: size + 'px',
transform: 'translate(' + (cx - totalRadius) + 'px,' + (cy - totalRadius) + 'px)'
} },
React.createElement(
'svg',
{
viewBox: strokeWidth + ' ' + strokeWidth + ' ' + size + ' ' + size,
preserveAspectRatio: 'none',
style: {
position: 'absolute',
left: 0, top: 0, width: '100%', height: '100%'
}
},
React.createElement(
NodeGroup,
{
data: data,
keyAccessor: function keyAccessor(d) {
return d.key;
},
start: function start(dat, i) {
return _extends({}, renderObject(dat, i));
},
update: function update(dat, i) {
return _extends({}, renderObject(dat, i), {
timing: {
duration: duration,
delay: i * delay,
ease: d3.easeCircleInOut
}
});
}
},
function (nodes) {
return React.createElement(
'g',
null,
nodes.map(function (_ref) {
var key = _ref.key,
data = _ref.data,
state = _ref.state;
// state here comes from our render object
return React.createElement(
'g',
state.g,
React.createElement('path', _extends({}, state.region, { style: _extends({}, state.region.style, { stroke: stroke, fill: fill }) })),
React.createElement('path', state.arc),
React.createElement(
'text',
state.text,
React.createElement(
'textPath',
_extends({}, state.textPath, { fill: stroke }),
' ',
data.text
)
)
);
})
);
}
),
React.createElement('circle', {
cx: innerRadius + outerRadius + strokeWidth,
cy: innerRadius + outerRadius + strokeWidth,
r: 3,
fill: 'none',
stroke: stroke,
opacity: .5
})
)
);
};
return Radial;
}(Component);
export default Radial;