css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              #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;
  }
}

            
          
!
            
              console.clear();

class Rebalancing extends React.Component {

		constructor () {
			super();
			this.state = {
        index: 0,
        isPlaying: false,
        onionSkinView: false
			}
		}
  
    componentWillMount () {
      d3.json(jsonUrl, (err, data) => {
        if (err) throw console.log(err);
        this.setState({data, index: data.length - 1, ready: true});
      });
    }
  
    _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});
    }
    
    _onRangeClick (newIndex) {
      this.setState({ index: newIndex });
    }
    
    _toggleOnionSkinView () {
        this.setState({ onionSkinView: !this.state.onionSkinView });
    }
  
    _onPlayOrPause () {
      var state = this.state;
      this.setState({isPlaying: !state.isPlaying}, () => {

        if (state.isPlaying) {
          clearInterval(this.timer);        
        }

        else {
          this.timer = setInterval(this._onButtonClick.bind(this, 1), 50);        
        }
      });
    }
	
    render () {
        var state = this.state,
            output = {},
            cn = "rebalancing__";
      
        output.html = <div className="rebalancing"><Loader /></div>;
        
        if (state.ready) {
          output.html = (
            <div className="rebalancing">
                <div className={cn + 'player'}>
                  <RebalancingChart
                    data={state.data}
                    index={state.index}
                    onionSkinView={state.onionSkinView} />
                  <div className={cn + "view"}>
                    <span>{format(date(state.data[state.index].date))}</span>
                    <span>
                      <input id="onionSkinView" type="checkbox" onChange={this._toggleOnionSkinView.bind(this)} />
                      <label htmlFor="onionSkinView">
                        <svg width="15" height="15">
                          <circle cx="2.5" cy="2.5" r="2.5" fill="#bbb" />
                          <circle cx="7.5" cy="7.5" r="2.5" fill="#888" />
                          <circle cx="12.5" cy="12.5" r="2.5" fill="#444" />
                        </svg>
                      </label>
                    </span>
                  </div>
                  <RebalancingTimeline
                    data={state.data}
                    index={state.index}
                    onRangeClick={this._onRangeClick.bind(this)} />
                  <RebalancingControls
                    data={state.data}
                    index={state.index}
                    isPlaying={state.isPlaying}
                    onButtonClick={this._onButtonClick.bind(this)} />
                </div>
            </div>);
        }
        return output.html;
    }
};

class RebalancingChart extends React.Component {
  constructor () {
    super();
    this.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]
    }
  }
  
  componentWillMount () {
    this._updateDomains();
  }
  
  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, h
    });
  }
  
  componentWillUnmount () {
        window.removeEventListener('resize', this._onResize);
  }
  
  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);
      }
    }
  }
  
  _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, h
    }, this._updateChart);
  }
  
  _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),
        d => d3.max(d3.entries(d.value),
            e => {
              if (e.key === 'deviation') {
                return Math.abs(e.value);
            }
        })
    ));
    
    
    yDomain.push(d3.max(d3.entries(data),
        d => d3.max(d3.entries(d.value),
            e => {
              if (e.key === 'deviation') {
                return e.value;
            }
        })
    ));

    rDomain.push(d3.min(d3.entries(data),
        d => d3.min(d3.entries(d.value),
            e => {
              if (e.key === 'wallet') {
                return e.value;
            }
        })
    ));
    
    rDomain.push(d3.max(d3.entries(data),
        d => d3.max(d3.entries(d.value),
            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
    });

  }
  
  _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: d => Math.round(state.y(d)),
        y2: d => Math.round(state.y(d)),
        'data-index': d => d
      });

    svg.selectAll('.rebalancing-chart__circles')
      .data(d3.entries(data))
      .enter()
      .append('circle.rebalancing-chart__circles')
      .translate([state.x.rangeBand() / 2, 0])
      .attr({
        cx: d => state.x(d.key),
        r: d => state.r(d.value.wallet),
        cy: d => state.y(d.value.deviation),
        fill: d => investmentClasses[d.key].color
      });
    
    this._setupPastChart(svg);
    
    this.setState({isSetupReady: true});

  }
  
  _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: d => state.x(d.key),
          r: d => state.r(d.value.wallet) < 0 ? 0 : state.r(d.value.wallet),
          cy: d => state.y(d.value.deviation),
          fill: d => investmentClasses[d.key].color,
          'fill-opacity': Math.pow(0.4, Math.log(skin))
        });
    }
  }
  
  _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: d => Math.round(state.y(d)),
        y2: d => Math.round(state.y(d))
      });

    svg.selectAll('.rebalancing-chart__circles')
      .data(d3.entries(data))
      .translate([state.x.rangeBand() / 2, 0])
      .attr({
        r: d => state.r(d.value.wallet),
        cx: d => state.x(d.key),
        cy: d => 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: d => state.x(d.key),
            r: d => state.r(d.value.wallet) < 0 ? 0 : state.r(d.value.wallet),
            cy: d => state.y(d.value.deviation)
          });
      }
    }
  }
  
  _updateChart () {
    this._updateDomains();
    this._redraw();
  }
  
  render () {
    return <svg
      ref="container"
      className="rebalancing__chart rebalancing-chart" />;
  }
}

class RebalancingTimeline extends React.Component {
  
  constructor () {
    super();
  }

  _update (event) {
    this.props.onRangeClick(+event.currentTarget.value);
  }

  render () {
    var props = this.props,
        output = {},
        cn = 'rebalancing-timeline__';

    return (
      <div className="rebalancing__timeline rebalancing-timeline">
        <span>{format(date(props.data[0].date))}</span>
        <input
            type="range"
            value={props.index}
            min="0"
            max={props.data.length - 1}
            className="rebalancing-timeline__input"
            onChange={this._update.bind(this)} />
        <span>{format(date(props.data[props.data.length - 1].date))}</span>
      </div>
    );
  }
}

class RebalancingControls extends React.Component {
  constructor () {
    super();
  }
  
  _onButtonClick (event) {
    let frames = event.currentTarget.dataset.frames;
    this.props.onButtonClick(frames);
  }
  
  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 (
      <div className="rebalancing__controls rebalancing-controls">
        <button onClick={this._onButtonClick.bind(this)} data-frames={-10}><i className="i-fast-backward" /></button>
        <button onClick={this._onButtonClick.bind(this)} data-frames={-1}><i className="i-backward" /></button>
        <button onClick={this._onButtonClick.bind(this)} data-frames={playOrPause[0]}><i className={playOrPause[1]} />
      </button>
        <button onClick={this._onButtonClick.bind(this)} data-frames={1}><i className="i-forward" /></button>
        <button onClick={this._onButtonClick.bind(this)} data-frames={10}><i className="i-fast-forward" /></button>
      </div>
    );
  }
}

class Loader extends React.Component {
  constructor () {
    super();
  }
  render() {
    return (<div className="loader"><svg><circle cx="50" cy="50" r="20" fill="none" strokeWidth="2" /></svg></div>);
  }
}

const 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(<Rebalancing />, document.getElementById("app"));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console