Deviation Chart (React.js and D3.js)

Size
13,508 Kb
Views
44,528

How do I make an deviation chart (react.js and d3.js)?

This chart shows the deviation history for 5 assets.. What is a deviation chart (react.js and d3.js)? How do you make a deviation chart (react.js and d3.js)? This script and codes were developed by Rafael Abensur on 12 September 2022, Monday.

Deviation Chart (React.js and D3.js) Previews

Deviation Chart (React.js and D3.js) - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Deviation Chart (React.js and D3.js)</title> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fontastic.s3.amazonaws.com/CYAoZjWzVHHZCBNJqoCxhW/icons.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel='stylesheet prefetch' href='css/9a5ecf2380448bc1a0825a3cb.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="app"></div> <script src='http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js'></script>
<script src='http://s3-us-west-2.amazonaws.com/s.cdpn.io/20221/d3-jetpack.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Deviation Chart (React.js and D3.js) - Script Codes CSS Codes

#app { display: flex; flex-flow: row wrap; min-height: 100vh; min-width: 100vw; background: #eee;
}
.rebalancing { max-width: 50rem; width: 100%; min-width: 28rem; min-height: 20rem; transition: width 0.3s ease, height 0.3s ease; will-change: width, height; margin: auto; background: #fff; display: flex; flex-flow: row wrap; padding: 0.5rem; position: relative; @include material-box-shadow(3); &__player { border: 1px solid $gray-40; width: 100%; } &__chart, &__view, &__timeline, &__controls { width: 100%; } &__chart { height: 18rem; } &__timeline, &__controls { height: 3rem; } &__view { box-sizing: border-box; height: 3rem; display: flex; flex-flow: row wrap; justify-content: space-between; align-items: center; border-top: 1px solid $gray-40; border-bottom: 1px solid $gray-40; padding: 0 1rem; span { flex: 0 auto; color: $gray-90; font-family: $font-medium; font-size: 1.2rem; } input { height: 1.5rem; width: 1.5rem; } input, label { margin: 0; } label { height: 1.5rem; margin-left: 0.5rem; } input, svg { position: relative; } input { top: 1px; } svg { top: 2px; } }
}
.rebalancing-chart { &__guides { shape-rendering: crispEdges; stroke: $gray-60; stroke-width: 1; stroke-dasharray: 5 1; stroke-opacity: 0.5; &[data-index="-0.3"], &[data-index="0.3"] { stroke: $red-80; } &[data-index="-0.2"], &[data-index="0.2"] { stroke: $yellow-80; } } &__past { display: none; &--visible { display: initial; } }
}
.rebalancing-timeline { display: flex; flex-flow: row wrap; align-items: center; padding: 0 1rem; font-family: $font-medium; color: $gray-50; box-sizing: border-box; &__input { flex: 1; height: 100%; margin: 0 1rem; } span { flex: 0 auto; font-size: 1.2rem; }
}
.rebalancing-controls { display: flex; flex-flow: row wrap; justify-content: center; align-items: center; button { flex: 0 auto; height: 100%; border: none; background: #fff; color: $gray-90; text-shadow: 0 1px 1px rgba(#000, 0.55); font-size: 1.5rem; padding: 0 2rem; i:before { line-height: 3rem; } &:active, &:focus { outline: none; } &:hover { color: $blue-70; } &:active { color: $blue-80; } }
}
.loader { height: 100px; left: 50%; position: absolute; top: 50%; transform: translate(-50%, -50%); width: 100px; svg { animation: rotate 2s linear infinite; height: 100px; position: relative; width: 100px; } circle { animation: color 6s ease-in-out infinite, dash 1.5s ease-in-out infinite; stroke-dasharray: 1, 200; stroke-dashoffset: 0; stroke-linecap: round; stroke-miterlimit: 10; }
}
@keyframes rotate { 100% { transform: rotate(360deg); }
}
@keyframes dash { 0% { stroke-dasharray: 1, 200; stroke-dashoffset: 0; } 50% { stroke-dasharray: 89, 200; stroke-dashoffset: -35; } 100% { stroke-dasharray: 89, 200; stroke-dashoffset: -124; }
}
@keyframes color { 100%, 0% { stroke: $red; } 40% { stroke: $blue; } 66% { stroke: $green; } 80%, 90% { stroke: $purple; }
}

Deviation Chart (React.js and D3.js) - Script Codes JS Codes

'use strict';
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; }
console.clear();
var Rebalancing = function (_React$Component) { _inherits(Rebalancing, _React$Component); function Rebalancing() { _classCallCheck(this, Rebalancing); var _this = _possibleConstructorReturn(this, _React$Component.call(this)); _this.state = { index: 0, isPlaying: false, onionSkinView: false }; return _this; } Rebalancing.prototype.componentWillMount = function componentWillMount() { var _this2 = this; d3.json(jsonUrl, function (err, data) { if (err) throw console.log(err); _this2.setState({ data: data, index: data.length - 1, ready: true }); }); }; Rebalancing.prototype._onButtonClick = function _onButtonClick(frames) { var state = this.state, index = +state.index + +frames; if (frames === 'play' || frames === 'pause') { if (state.index === state.data.length - 1) { return this.setState({ index: 0 }, this._onPlayOrPause); } else { return this._onPlayOrPause(); } } if (index < 0) { index = 0; if (state.isPlaying) { this._onPlayOrPause(); } } else if (index > state.data.length - 1) { index = state.data.length - 1; if (state.isPlaying) { this._onPlayOrPause(); } } this.setState({ index: index }); }; Rebalancing.prototype._onRangeClick = function _onRangeClick(newIndex) { this.setState({ index: newIndex }); }; Rebalancing.prototype._toggleOnionSkinView = function _toggleOnionSkinView() { this.setState({ onionSkinView: !this.state.onionSkinView }); }; Rebalancing.prototype._onPlayOrPause = function _onPlayOrPause() { var _this3 = this; var state = this.state; this.setState({ isPlaying: !state.isPlaying }, function () { if (state.isPlaying) { clearInterval(_this3.timer); } else { _this3.timer = setInterval(_this3._onButtonClick.bind(_this3, 1), 50); } }); }; Rebalancing.prototype.render = function render() { var state = this.state, output = {}, cn = "rebalancing__"; output.html = React.createElement( 'div', { className: 'rebalancing' }, React.createElement(Loader, null) ); if (state.ready) { output.html = React.createElement( 'div', { className: 'rebalancing' }, React.createElement( 'div', { className: cn + 'player' }, React.createElement(RebalancingChart, { data: state.data, index: state.index, onionSkinView: state.onionSkinView }), React.createElement( 'div', { className: cn + "view" }, React.createElement( 'span', null, format(date(state.data[state.index].date)) ), React.createElement( 'span', null, React.createElement('input', { id: 'onionSkinView', type: 'checkbox', onChange: this._toggleOnionSkinView.bind(this) }), React.createElement( 'label', { htmlFor: 'onionSkinView' }, React.createElement( 'svg', { width: '15', height: '15' }, React.createElement('circle', { cx: '2.5', cy: '2.5', r: '2.5', fill: '#bbb' }), React.createElement('circle', { cx: '7.5', cy: '7.5', r: '2.5', fill: '#888' }), React.createElement('circle', { cx: '12.5', cy: '12.5', r: '2.5', fill: '#444' }) ) ) ) ), React.createElement(RebalancingTimeline, { data: state.data, index: state.index, onRangeClick: this._onRangeClick.bind(this) }), React.createElement(RebalancingControls, { data: state.data, index: state.index, isPlaying: state.isPlaying, onButtonClick: this._onButtonClick.bind(this) }) ) ); } return output.html; }; return Rebalancing;
}(React.Component);
;
var RebalancingChart = function (_React$Component2) { _inherits(RebalancingChart, _React$Component2); function RebalancingChart() { _classCallCheck(this, RebalancingChart); var _this4 = _possibleConstructorReturn(this, _React$Component2.call(this)); _this4.state = { isDOMReady: false, isDataReady: false, isSetupReady: false, x: d3.scale.ordinal(), y: d3.scale.linear(), r: d3.scale.sqrt().range([3, 15]), guides: [-1, -0.3, -0.2, 0, 0.2, 0.3, 1] }; return _this4; } RebalancingChart.prototype.componentWillMount = function componentWillMount() { this._updateDomains(); }; RebalancingChart.prototype.componentDidMount = function componentDidMount() { var state = this.state, w = +React.findDOMNode(this.refs.container).getBoundingClientRect().width, h = +React.findDOMNode(this.refs.container).getBoundingClientRect().height; state.y.range([h, 0]); state.x.rangeRoundBands([0, w]); window.addEventListener('resize', this._onResize.bind(this)); this.setState({ isDOMReady: true, y: state.y, x: state.x, w: w, h: h }); }; RebalancingChart.prototype.componentWillUnmount = function componentWillUnmount() { window.removeEventListener('resize', this._onResize); }; RebalancingChart.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) { var state = this.state; if (state.isDOMReady && state.isDataReady && !state.isSetupReady) { this._setupChart(); } else if (state.isDOMReady && state.isDataReady && state.isSetupReady) { if (prevProps.index !== this.props.index) { this._updateChart(); } else { d3.select('.rebalancing-chart__past').classed('rebalancing-chart__past--visible', this.props.onionSkinView); } } }; RebalancingChart.prototype._onResize = function _onResize() { var state = this.state, w = +React.findDOMNode(this.refs.container).getBoundingClientRect().width, h = +React.findDOMNode(this.refs.container).getBoundingClientRect().height; state.y.range([h, 0]); state.x.rangeRoundBands([0, w]); this.setState({ y: state.y, x: state.x, w: w, h: h }, this._updateChart); }; RebalancingChart.prototype._updateDomains = function _updateDomains() { var state = this.state, props = this.props, data = props.data[props.index].allocation, yDomain = [], xDomain = Object.keys(data), rDomain = []; yDomain.push(d3.max(d3.entries(data), function (d) { return d3.max(d3.entries(d.value), function (e) { if (e.key === 'deviation') { return Math.abs(e.value); } }); })); yDomain.push(d3.max(d3.entries(data), function (d) { return d3.max(d3.entries(d.value), function (e) { if (e.key === 'deviation') { return e.value; } }); })); rDomain.push(d3.min(d3.entries(data), function (d) { return d3.min(d3.entries(d.value), function (e) { if (e.key === 'wallet') { return e.value; } }); })); rDomain.push(d3.max(d3.entries(data), function (d) { return d3.max(d3.entries(d.value), function (e) { if (e.key === 'wallet') { return e.value; } }); })); state.y.domain([-d3.max([0.35, yDomain[0]]), d3.max([0.35, yDomain[1]])]); state.r.domain(rDomain); state.x.domain(xDomain); this.setState({ isDataReady: true, x: state.x, y: state.y, r: state.r }); }; RebalancingChart.prototype._setupChart = function _setupChart() { var state = this.state, props = this.props, data = props.data[props.index].allocation, svg = d3.select(React.findDOMNode(this.refs.container)); svg.selectAll('.rebalancing-chart__guides').data(state.guides).enter().append('line.rebalancing-chart__guides').attr({ x1: 0, x2: state.w, y1: function y1(d) { return Math.round(state.y(d)); }, y2: function y2(d) { return Math.round(state.y(d)); }, 'data-index': function dataIndex(d) { return d; } }); svg.selectAll('.rebalancing-chart__circles').data(d3.entries(data)).enter().append('circle.rebalancing-chart__circles').translate([state.x.rangeBand() / 2, 0]).attr({ cx: function cx(d) { return state.x(d.key); }, r: function r(d) { return state.r(d.value.wallet); }, cy: function cy(d) { return state.y(d.value.deviation); }, fill: function fill(d) { return investmentClasses[d.key].color; } }); this._setupPastChart(svg); this.setState({ isSetupReady: true }); }; RebalancingChart.prototype._setupPastChart = function _setupPastChart(svg) { var props = this.props, state = this.state; svg = svg.append('g.rebalancing-chart__past'); for (var skin = 1; skin < props.data.length - 1; skin++) { var data = props.data[props.index - skin].allocation; svg.selectAll('.rebalancing-chart__past-circles-' + skin).data(d3.entries(data)).enter().append('circle.rebalancing-chart__past-circles-' + skin).translate([state.x.rangeBand() / 2, 0]).attr({ cx: function cx(d) { return state.x(d.key); }, r: function r(d) { return state.r(d.value.wallet) < 0 ? 0 : state.r(d.value.wallet); }, cy: function cy(d) { return state.y(d.value.deviation); }, fill: function fill(d) { return investmentClasses[d.key].color; }, 'fill-opacity': Math.pow(0.4, Math.log(skin)) }); } }; RebalancingChart.prototype._redraw = function _redraw() { var state = this.state, props = this.props, data = props.data[props.index].allocation, svg = d3.select(React.findDOMNode(this.refs.container)); svg.selectAll('.rebalancing-chart__guides').data(state.guides).attr({ x1: 0, x2: state.w, y1: function y1(d) { return Math.round(state.y(d)); }, y2: function y2(d) { return Math.round(state.y(d)); } }); svg.selectAll('.rebalancing-chart__circles').data(d3.entries(data)).translate([state.x.rangeBand() / 2, 0]).attr({ r: function r(d) { return state.r(d.value.wallet); }, cx: function cx(d) { return state.x(d.key); }, cy: function cy(d) { return state.y(d.value.deviation); } }); for (var skin = 1; skin < props.data.length - 1; skin++) { if (props.index - skin < 0) { break; } else { svg.selectAll('.rebalancing-chart__past-circles-' + skin).data(d3.entries(props.data[props.index - skin].allocation)).translate([state.x.rangeBand() / 2, 0]).attr({ cx: function cx(d) { return state.x(d.key); }, r: function r(d) { return state.r(d.value.wallet) < 0 ? 0 : state.r(d.value.wallet); }, cy: function cy(d) { return state.y(d.value.deviation); } }); } } }; RebalancingChart.prototype._updateChart = function _updateChart() { this._updateDomains(); this._redraw(); }; RebalancingChart.prototype.render = function render() { return React.createElement('svg', { ref: 'container', className: 'rebalancing__chart rebalancing-chart' }); }; return RebalancingChart;
}(React.Component);
var RebalancingTimeline = function (_React$Component3) { _inherits(RebalancingTimeline, _React$Component3); function RebalancingTimeline() { _classCallCheck(this, RebalancingTimeline); return _possibleConstructorReturn(this, _React$Component3.call(this)); } RebalancingTimeline.prototype._update = function _update(event) { this.props.onRangeClick(+event.currentTarget.value); }; RebalancingTimeline.prototype.render = function render() { var props = this.props, output = {}, cn = 'rebalancing-timeline__'; return React.createElement( 'div', { className: 'rebalancing__timeline rebalancing-timeline' }, React.createElement( 'span', null, format(date(props.data[0].date)) ), React.createElement('input', { type: 'range', value: props.index, min: '0', max: props.data.length - 1, className: 'rebalancing-timeline__input', onChange: this._update.bind(this) }), React.createElement( 'span', null, format(date(props.data[props.data.length - 1].date)) ) ); }; return RebalancingTimeline;
}(React.Component);
var RebalancingControls = function (_React$Component4) { _inherits(RebalancingControls, _React$Component4); function RebalancingControls() { _classCallCheck(this, RebalancingControls); return _possibleConstructorReturn(this, _React$Component4.call(this)); } RebalancingControls.prototype._onButtonClick = function _onButtonClick(event) { var frames = event.currentTarget.dataset.frames; this.props.onButtonClick(frames); }; RebalancingControls.prototype.render = function render() { var playOrPause = this.props.isPlaying ? ['pause', 'i-pause'] : ['play', 'i-play']; if (this.props.index === this.props.data.length - 1) { playOrPause = ['play', 'i-reload']; } return React.createElement( 'div', { className: 'rebalancing__controls rebalancing-controls' }, React.createElement( 'button', { onClick: this._onButtonClick.bind(this), 'data-frames': -10 }, React.createElement('i', { className: 'i-fast-backward' }) ), React.createElement( 'button', { onClick: this._onButtonClick.bind(this), 'data-frames': -1 }, React.createElement('i', { className: 'i-backward' }) ), React.createElement( 'button', { onClick: this._onButtonClick.bind(this), 'data-frames': playOrPause[0] }, React.createElement('i', { className: playOrPause[1] }) ), React.createElement( 'button', { onClick: this._onButtonClick.bind(this), 'data-frames': 1 }, React.createElement('i', { className: 'i-forward' }) ), React.createElement( 'button', { onClick: this._onButtonClick.bind(this), 'data-frames': 10 }, React.createElement('i', { className: 'i-fast-forward' }) ) ); }; return RebalancingControls;
}(React.Component);
var Loader = function (_React$Component5) { _inherits(Loader, _React$Component5); function Loader() { _classCallCheck(this, Loader); return _possibleConstructorReturn(this, _React$Component5.call(this)); } Loader.prototype.render = function render() { return React.createElement( 'div', { className: 'loader' }, React.createElement( 'svg', null, React.createElement('circle', { cx: '50', cy: '50', r: '20', fill: 'none', strokeWidth: '2' }) ) ); }; return Loader;
}(React.Component);
var jsonUrl = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/20221/dummy-deviation.json", investmentClasses = { postfixed: { name: 'Juros pós-fixados', color: '#1f9898' }, prefixed: { name: 'Juros prefixados', color: '#135d5d' }, inflation: { name: 'Inflação', color: '#f5a623' }, stock: { name: 'Ações Brasil', color: '#f37077' }, exterior: { name: 'Ações EUA', color: '#5c4561' }
}, format = d3.time.format("%d/%m/%y"), date = d3.time.format("%Y-%m-%d").parse;
React.render(React.createElement(Rebalancing, null), document.getElementById("app"));
Deviation Chart (React.js and D3.js) - Script Codes
Deviation Chart (React.js and D3.js) - Script Codes
Home Page Home
Developer Rafael Abensur
Username abensur
Uploaded September 12, 2022
Rating 3
Size 13,508 Kb
Views 44,528
Do you need developer help for Deviation Chart (React.js and D3.js)?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Rafael Abensur (abensur) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!