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="root"></div>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Open+Sans&display=swap");

#root {
  height: 100vh;
  width: 100vw;
  justify-content: center;
  align-items: center;
  font-family: "Open Sans", sans-serif;
  background-color: #f5f5f5;
}

#viz-container {
  width: 960px;
  height: 600px;
  position: relative;
  margin: auto;
  background-color: #fff;
  padding: 20px 20px 20px 20px;
  box-shadow: 2px 2px 10px;
}
#svg {
  position: absolute;
  top: 5em;
}

.dot {
  opacity: 0.7;
}

#legend {
  display: flex;
  flex-direction: column;
  position: absolute;
  background-color: #f1f2f3;
  z-index: 10;
  font-size: 1rem;
  padding: 10px;
}

#doping-label {
  display: flex;
  flex-direction: row;
  align-self: flex-end;
  padding: 5px;
}

#doping-dot {
  background-color: #e25a42;
  width: 1rem;
  height: 1rem;
  margin: auto;
  border-radius: 50%;
}

#clean-label {
  display: flex;
  flex-direction: row;
  align-self: flex-end;
  padding: 5px;
  border-radius: 50%;
}

#clean-dot {
  background-color: #6bbba1;
  width: 1rem;
  height: 1rem;
  margin: auto;
  border-radius: 50%;
}

#tooltip-container {
  position: absolute;
  overflow: visible;
  background-color: #f1f2f3;
  z-index: 10;
  box-shadow: 2px 2px 2px;
  opacity: 0.9;
  padding: 10px;
  width: 270px;
}

#tooltip {
  display: flex;
  justify-content: space-between;
  padding: 5px;
}

#tooltip-name {
  font-size: 1.2rem;
}

#tooltip-doping {
  font-size: 1.1rem;
  padding: 5px;
}

#title {
  text-align: center;
  font-size: 1.5rem;
}

#subtitle {
  text-align: center;
}

#left-axis-label {
  position: absolute;
  transform: rotate(-90deg);
  left: -30px;
  top: 270px;
  font-size: 1.1rem;
}

#bottom-axis-label {
  position: absolute;
  left: 480px;
  top: 590px;
  font-size: 1.1rem;
}

pre {
  font-size: 1.6rem;
}

              
            
!

JS

              
                const { useState, useEffect, useCallback } = React;

const titleLabel = "Doping in Professional Bicycle Racing";
const subtitleLabel = "35 Fastest times up Alpe d'Huez";

const width = 960;
const height = 500;
const margin = { top: 50, right: 20, bottom: 30, left: 110 };
const innerHeight = height - margin.top - margin.bottom;
const innerWidth = width - margin.right - margin.left;

// App Parent Component
const App = () => {
  data = useData();
  const [hoveredValue, setHoveredValue] = useState(null);
  const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 });

  const handleMouseMove = useCallback(
    (event) => {
      const { clientX, clientY } = event;
      setMousePosition({ x: clientX, y: clientY });
    },
    [setMousePosition]
  );

  if (!data) {
    return <pre>Loading...</pre>;
  }

  const xValue = (d) => d.Year;
  const bottomAxisLabel = "Year";

  const yValue = (d) => d.Time;
  const leftAxisLabel = "Time in minutes";

  const yAxisTickFormat = d3.timeFormat("%M:%S");

  const xScale = d3
    .scaleLinear()
    .domain(d3.extent(data, xValue))
    .range([0, innerWidth])
    .nice();

  const yScale = d3
    .scaleTime()
    .domain(d3.extent(data, yValue))
    .range([0, innerHeight])
    .nice();

  const place = (d) => d.Place;
  const doping = (d) => d.Doping;

  return (
    <div>
      <Tooltip
        hoveredValue={hoveredValue}
        mousePosition={mousePosition}
        yAxisTickFormat={yAxisTickFormat}
      />
      <div id="viz-container">
        <div id="title">{titleLabel}</div>
        <div id="subtitle">{subtitleLabel}</div>
        <div id="left-axis-label">{leftAxisLabel}</div>
        <div id="bottom-axis-label">{bottomAxisLabel}</div>
        <Legend innerWidth={innerWidth} innerHeight={innerHeight} />
        <svg id="svg" width={width} height={height}>
          <g transform={`translate(${margin.left}, ${margin.top})`}>
            <g id="y-axis" className="y-axis">
              <AxisLeft
                yScale={yScale}
                innerWidth={innerWidth}
                yAxisTickFormat={yAxisTickFormat}
                tickOffset={20}
              />
            </g>
            <g id="x-axis" className="x-axis">
              <AxisBottom
                xScale={xScale}
                innerHeight={innerHeight}
                tickOffset={8}
              />
            </g>
            <Marks
              data={data}
              xScale={xScale}
              xValue={xValue}
              yScale={yScale}
              yValue={yValue}
              place={place}
              doping={doping}
              setHoveredValue={setHoveredValue}
              handleMouseMove={handleMouseMove}
              circleRadius={7}
            />
          </g>
        </svg>
      </div>
    </div>
  );
};

// Tooltip Component
const Tooltip = ({ hoveredValue, mousePosition, yAxisTickFormat }) => {
  if (!hoveredValue) {
    return <div id="tooltip-container" style={{ visibility: "hidden" }}></div>;
  } else {
    const xPosition = mousePosition.x;
    const yPosition = mousePosition.y;
    return (
      <div
        id="tooltip-container"
        style={{ left: `${xPosition + 25}px`, top: `${yPosition - 25}px` }}
      >
        <div>
          <div id="tooltip-name">
            {hoveredValue.Name} ({hoveredValue.Nationality})
          </div>
          <hr />
          <div id="tooltip" data-year={hoveredValue.Year}>
            <div>Year: {hoveredValue.Year}</div>
            <div>Place: {hoveredValue.Place}</div>
            <div>Time: {yAxisTickFormat(hoveredValue.Time)} </div>
          </div>
          <div id="tooltip-doping">
            {!hoveredValue.Doping
              ? "No doping allegations"
              : hoveredValue.Doping}
          </div>
          <div></div>
        </div>
      </div>
    );
  }
};

// Marks (Dots) Component
const Marks = ({
  data,
  xScale,
  xValue,
  yScale,
  yValue,
  place,
  doping,
  setHoveredValue,
  handleMouseMove,
  circleRadius
}) =>
  data.map((d, i) => (
    <>
      <circle
        key={place(d)}
        id={"place" + place(d)}
        data-xvalue={xValue(d)}
        data-yvalue={yValue(d)}
        className="dot"
        cx={xScale(xValue(d))}
        cy={yScale(yValue(d))}
        r={circleRadius}
        fill={doping(d) ? "#E25A42" : "#6BBBA1"}
        onMouseEnter={() => setHoveredValue(d)}
        onMouseLeave={() => setHoveredValue(null)}
        onMouseMove={handleMouseMove}
      />
    </>
  ));

// AxisLeft Component
const AxisLeft = ({ yScale, innerWidth, yAxisTickFormat, tickOffset }) =>
  yScale.ticks().map((tickValue) => (
    <g
      key={tickValue}
      className="tick"
      transform={`translate(0,${yScale(tickValue)})`}
    >
      <line x2={innerWidth} stroke="#f1f2f3" />
      <text style={{ textAnchor: "end" }} x={-tickOffset} dy=".32em">
        {yAxisTickFormat(tickValue)}
      </text>
    </g>
  ));

// AxisBottom Component
const AxisBottom = ({ xScale, innerHeight, tickOffset }) =>
  xScale.ticks().map((tickValue) => (
    <g
      key={tickValue}
      className="tick"
      transform={`translate(${xScale(tickValue)},0)`}
    >
      <line y2={innerHeight} stroke="#f1f2f3" />
      <text
        style={{ textAnchor: "middle" }}
        dy=".71em"
        y={innerHeight + tickOffset}
      >
        {tickValue}
      </text>
    </g>
  ));

// Legend Component
const Legend = ({ innerWidth, innerHeight }) => (
  <div
    id="legend"
    style={{ left: `${innerWidth * 0.82}px`, top: `${innerHeight * 0.32}px` }}
  >
    <div id="doping-label">
      <div style={{ paddingRight: "5px" }}>Riders with doping allegations</div>
      <div id="doping-dot"></div>
    </div>
    <div id="clean-label">
      <div style={{ paddingRight: "5px" }}>No doping allegations</div>
      <div id="clean-dot"></div>
    </div>
  </div>
);

// useData custom Hook
const useData = () => {
  const [data, setData] = useState(null);
  const [loaded, setLoaded] = useState(false);

  const url =
    "https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json";

  useEffect(() => {
    d3.json(url).then((json) => {
      setData(json);
    });
  }, []);

  useEffect(() => {
    if (data) {
      data.forEach((d) => {
        let minutes = d.Time.substring(0, 2);
        let seconds = d.Time.substring(3);
        d.Time = new Date(1976, 6, 28, 0, minutes, seconds);
      });
      setLoaded(true);
    }
  }, [data]);

  return data;
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

              
            
!
999px

Console