Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="container"></div>
              
            
!

CSS

              
                body {
  font-family: Arial;
}

rect {
  fill: #0776A0;
}

.chart {
  margin: 1em 0;
}

.button {
  font-size: 16px;
  padding: 0.5em;
  border-radius: 3px;
  background-color: #FFA340;
  color: white;
  margin: 0.5em;
  cursor: pointer;
  transition: all 0.3s;
  
  &:hover {
    background-color: #A65600;
  }
}
              
            
!

JS

              
                class Chart extends React.Component {
  static defaultProps = {
    w: 600,
    h: 250
  };
  
  constructor() {
    super();
    this.state = {
      dataset: [{ key: 0, value: 5 },
                { key: 1, value: 10 },
                { key: 2, value: 13 },
                { key: 3, value: 19 },
                { key: 4, value: 21 },
                { key: 5, value: 25 },
                { key: 6, value: 22 },
                { key: 7, value: 18 },
                { key: 8, value: 15 },
                { key: 9, value: 13 },
                { key: 10, value: 11 },
                { key: 11, value: 12 },
                { key: 12, value: 15 },
                { key: 13, value: 20 },
                { key: 14, value: 18 },
                { key: 15, value: 17 },
                { key: 16, value: 16 },
                { key: 17, value: 18 },
                { key: 18, value: 23 },
                { key: 19, value: 25 } ]
    };
  }
  
  componentDidMount() {
    this.svg = d3.select(this.refs.chart)
      .append('svg')
      .attr('width', this.props.w)
      .attr('height', this.props.h);
    
    this._renderChart();
  }

  componentDidUpdate() {
    this._renderChart();
  }
  
  _renderChart() {
    const { w, h } = this.props;
    const dataset = this.state.dataset;
    
    let xScale = d3.scale.ordinal()
      .domain(d3.range(dataset.length))
      .rangeRoundBands([0, w], 0.05);
    let yScale = d3.scale.linear()
      .domain([0, d3.max(dataset, d => d.value)])
      .range([h, 0]);

    // Data binding with key
    let bars = this.svg.selectAll('rect').data(dataset, d => d.key);

    // Enter
    bars.enter()
      .append('rect')
      .classed("bar", true)
      .attr('x', w) // Enter from the right side
      .attr('width', xScale.rangeBand())
      .attr('y', d => yScale(d.value))
      .attr('height', d => h - yScale(d.value));

    // Update
    bars.transition()
      .duration(500)
      .attr('x', (d, i) => xScale(i))
      .attr('width', xScale.rangeBand())
      .attr('y', d => yScale(d.value))
      .attr('height', d => h - yScale(d.value));

    // Exit
    bars.exit()
      .transition()
      .duration(500)
      .attr("x", -xScale.rangeBand()) // Exit to the left side
      .remove();
  }

  handleAdd() {
    const dataset = this.state.dataset;
    const lastKey = dataset[dataset.length-1].key;
    this.setState({
      dataset: [
        ...dataset,
        {
          key: lastKey + 1,
          value: Math.round(Math.random()*25)
        }
      ]
    });
  }

  handleRemove() {
    this.setState({
      dataset: this.state.dataset.slice(1)
    })
  }
  
  render() {
    return (
      <div>
        <div ref="chart" className="chart"></div>
        <span className="button" onClick={e => this.handleAdd()}>Add</span>
        <span className="button" onClick={e => this.handleRemove()}>Remove</span>
      </div>
    )
  }
}

ReactDOM.render(<Chart />, document.querySelector('#container'));
              
            
!
999px

Console