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.

            
              <div id="container"></div>
            
          
!
            
              h1 {
  text-align: center;
}

svg {
  display: block;
  margin: auto;
}

.axis path,
.axis line {
  fill: none;
  stroke: black;
  shape-rendering: crispEdges;
}

.axis text {
  font-family: sans-serif;
  font-size: 11px;
}

.controls button {
  display: block;
  margin: 4em auto 4em auto;
  background-color: 'aquawhite';
}
            
          
!
            
              const settings = {
  width: 500,
  height: 300,
  padding: 30,
  numDataPoints: 50,
  maxRange: () => Math.random() * 1000
};

class Axis extends React.Component {
  componentDidMount() {
    this.renderAxis();  
  }
  
  componentDidUpdate() {
    this.renderAxis();
  }
  
  renderAxis() {
    const node = React.findDOMNode(this.refs.axisContainer);
    const axis = d3.svg.axis()
      .orient(this.props.orient)
      .ticks(5)
      .scale(this.props.scale);
    
    d3.select(node).call(axis);
  }
  
  render() {
    return <g className="axis" ref="axisContainer" transform={this.props.translate} />
  }
}

class XYAxis extends React.Component {
  render() {
    return (
      <g className="xy-axis">
        <Axis
          translate={`translate(0, ${this.props.height - this.props.padding})`}
          scale={this.props.xScale}
          orient="bottom"
        />
        <Axis
          translate={`translate(${this.props.padding}, 0)`}
          scale={this.props.xScale}
          orient="left"
        />
      </g>
    );
  }
}

class DataCircles extends React.Component {
  renderCircle(coords) {
    return (
      <circle
       cx={this.props.xScale(coords[0])}
       cy={this.props.yScale(coords[1])}
       r={2}
       key={Math.random() * 1}
      />
    );
  }
  
  render() {
    return <g>{this.props.data.map(this.renderCircle.bind(this))}</g>
  }
}

class ScatterPlot extends React.Component {
  getXScale() {
    const xMax = d3.max(this.props.data, (d) => d[0]);
    
    return d3.scale.linear()
      .domain([0, xMax])
      .range([this.props.padding, (this.props.width - this.props.padding * 2)]);
  }
  
  getYScale() {
    const yMax = d3.max(this.props.data, (d) => d[1]);
    
    return d3.scale.linear()
      .domain([0, yMax])
      .range([this.props.height - this.props.padding, this.props.padding]);
  }
  
  render() {
    const xScale = this.getXScale();
    const yScale = this.getYScale();
    
    return (
      <svg width={this.props.width} height={this.props.height}>
        <DataCircles xScale={xScale} yScale={yScale} {...this.props} />
        <XYAxis xScale={xScale} yScale={yScale} {...this.props} />
      </svg>
    );
  }
}

class App extends React.Component {
  componentWillMount() {
    this.randomizeData();
  }
  
  randomizeData() {    
    const randomData = d3.range(settings.numDataPoints).map(() => {
      return [Math.floor(Math.random() * settings.maxRange()),  Math.floor(Math.random() * settings.maxRange())];
    });
    this.setState({ data: randomData });
  }
  
  render() {
    return (
      <div>
        <h1>React and D3 are Friends</h1>
        <ScatterPlot data={this.state.data} {...settings} />
        <div className="controls">
          <button className="btn randomize" onClick={this.randomizeData.bind(this)}>Randomize Data</button>
        </div>
      </div>
    );
  }
}

React.render(<App />, document.getElementById('container'));
            
          
!
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