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>
            
          
!
            
              body {
  background-image: url(http://c2064.in-berlin.de/fcc/leaderboard-bg.jpg);
  background-size: 100%;
  font-family: "Anonymous Pro", "Courier New", sans-serif;
}

div.world-map {
  position: relative;
  width: 1280px;
  height: 800px;
  margin: 0 auto;
  
  .country {
    fill: #ccc;
    stroke: #888;
  }
  
  .country.selected {
    fill: #99c;
  }
  
  .meteorites {
    fill: rgba(100, 0, 0, 0.2);
    stroke: rgba(100, 0, 0, 0.5);
    strokeWeight: 0.5
  }
  
  .display {
    position: absolute;
    width: 40vh;
    background-color: rgba(120,90,90,0.4);
    border-radius: 3vh;
    text-align: center;
    padding: 1vh;
    font-size: 2vh;
    
    .name {
      font-size: 3vh;
    }
    
    .label {
      font-weight: bold;
    }
  }
  
  
}
            
          
!
            
              // React/Redux Boilerplate with Reducers and Actions

// React /////////////////////////////////////////////////////////
const Component = React.Component;

// Redux /////////////////////////////////////////////////////////
const Provider = ReactRedux.Provider;
const connect = ReactRedux.connect;
const createStore = Redux.createStore;
const combineReducers = Redux.combineReducers;
const bindActionCreators = Redux.bindActionCreators;

// Display //////////////////////////////////////////////////////////////
const Display = ({pos, mass, name, recclass, year, id}) => {
  if( !mass ) {
    console.log("No display.");
    return <div></div>
  } else {
    console.log(pos, name);
    return (
      <div className="display" style={{"top": pos[1], "left": pos[0]+30}}>
        <span className="name"><strong>{name}</strong></span>
        <br />
        <span className="label">Mass: </span><span className="info">{mass/1000} kg</span>
        <br />
        <span className="label">Recclass: </span><span className="info">{recclass}</span>
        <br />
        <span className="label">Year: </span><span className="year">{year.substr(0,4)}</span>
      </div>
    );    
  }
}

// Container / ReduxContainer /////////////////////////////////////////
class WorldMapClass extends Component {
  constructor(props) {
    super(props);
    
    this.state = {
      margin: { top: 50, left: 50, right: 50, bottom: 50 },
      size: null
    };
    
    // Load Meteorite data
    axios.get("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/meteorite-strike-data.json")
      .then(res => {
        this.props.dataLoaded("meteorites", res);
    });

    // Load world map
    axios.get("https://unpkg.com/world-atlas@1/world/110m.json")
      .then(res => {
        this.props.dataLoaded("world", res);
    });
  }
  
  componentDidMount() {
    this.initMap();    
  }

  componentDidUpdate() {
    this.drawMap();
  }
  
  initMap() {
    // Get width, height and margin
    let {width, height} = d3.select(".world-map").node().getBoundingClientRect(),
        margin = this.state.margin;
    
    // Define a projection (earth is a globe, we need screen coordinates)
    let projection = d3.geoMercator()
      .translate([ width/2, height/2 ])
      .scale(200);

    // Define a "magic" path, that uses the projection to translate all globe lat, long positions into x and y
    let path = d3.geoPath()
      .projection(projection);

    // Then the world map, first with out data
    let worldMap = d3.select(".world-map").append("svg")
            .attr("height", height )
            .attr("width", width )
          .append("g")
            .attr("transform", "translate("+ margin.left +","+ margin.top +")");    

    // Subtract margin from width and height
    width -= margin.left + margin.right;
    height -= margin.top + margin.bottom;

    // Remember this
    this.setState({ width, height, projection, path, worldMap }) 
  }
  
  drawMap() {
    let { worldMap, path, projection } = this.state;

    // No map or no data ... return
    if( !worldMap || !this.props.data.world ) return;
    
    let countries = topojson.feature(this.props.data.world, this.props.data.world.objects.countries).features
    
    worldMap.selectAll(".country")
      .data(countries)
      .enter().append("path")
        .attr("class", "country")
        .attr("d", path);

    worldMap.selectAll(".meteorites")
      .data(this.props.data.meteorites.features)
      .enter().append("circle")
        .attr("class", "meteorites")
        .attr("r", (d) => Math.pow( d.properties.mass, 0.5 ) / 100)
        .attr("cx", (d) => {
          return projection([d.properties.reclong, d.properties.reclat])[0];
        })
        .attr("cy", (d) => {
          return projection([d.properties.reclong, d.properties.reclat])[1];
        })
        .on("mouseover", (d) => {
          d.properties.pos = d3.mouse(this.wrapper);
          this.props.hover(d.properties, true);
        })
        .on("mouseout",  (d) => {
          d.properties.pos = d3.mouse(this.wrapper);
          this.props.hover(d.properties, false)
        });
  }
  
  render() {
    return (
      <div
        className="world-map"
        ref={ wrapper => this.wrapper = wrapper }
      >
        <Display
          className="display"
          pos={this.props.data.selection.pos}
          mass={this.props.data.selection.mass}
          name={this.props.data.selection.name}
          recclass={this.props.data.selection.recclass}
          year={this.props.data.selection.year}
          id={this.props.data.selection.id}
        />
      </div>
    )
  }
}

const WorldMap = connect(
  ({data}) => { return {data}; },
  (dispatch) => bindActionCreators({hover, dataLoaded}, dispatch)
)(WorldMapClass);


// Redux Reducers /////////////////////////////////////////////////////////
const WorldMapReducer = function(state, action) {
  
  if( !state ) {
    var state = {
      meteorites: null,
      world: null,
      selection: { mass: null }
    }
    
    return state;
  }
  
  switch( action.type ) {
  case DATALOADED_ACTION:
      
      let data = action.payload.data.data;
      if( action.payload.type === "meteorites" ) {
        data.features = data.features.sort((a, b) => parseInt(b.properties.mass) - parseInt(a.properties.mass))
        console.log(data);
      }
      
      let state = { ...state };
      state[action.payload.type] = data;
      return state;
  case HOVER_ACTION:
      if( action.payload ) return { ...state, selection: action.payload };
      else                 return { ...state, selection: { mass: null } };
  }
  
  return state;
}

const rootReducer = combineReducers({
  data: WorldMapReducer  
});

// Redux Actions ////////////////////////////////////////////////////////////
const DATALOADED_ACTION = "DATALOADED_ACTION";
const HOVER_ACTION = "HOVER_ACTION";

function dataLoaded(type, data) {
  return {
    type: DATALOADED_ACTION,
    payload: { type, data }
  }
}

function hover(properties, set) {
  return {
    type: HOVER_ACTION,
    payload: set? properties : null
  }
}


// App ///////////////////////////////////////////////////////////////////////
class App extends Component {
  constructor(props) {
    super(props);
    
    this.state = {
    }        
  }

  render() {
    return (
      <div>
        <WorldMap />
      </div>
    )
  }
}

ReactDOM.render(<Provider store={createStore(rootReducer)}><App /></Provider>, 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