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


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


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.


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.


                <div id="wrapper"></div>  


                body {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2em;
  letter-spacing: -0.011em;
  font-family: sans-serif;
  font-size: 16px;
  color: #34495e;
  background: #f8f9fa;

svg {
  fill: #34495e;

.axis path,
.axis line {
  stroke: #1347a6;

.axis text {
  fill: #1347a6;

.freezing {
    fill: #e0f3f3;

.line {
    fill: none;
    stroke: #af9358;
    stroke-width: 2;
.x-axis-label {
    fill: black;
    font-size: 1.4em;
    text-transform: capitalize;



                async function drawLineChart() {
  // 1. Access data
  let dataset = await d3.json("");

  // 2. Create chart dimensions

  const yAccessor = d => d.temperatureMax;
  const dateParser = d3.timeParse("%Y-%m-%d");
  const xAccessor = d => dateParser(;
  dataset = dataset.sort((a, b) => xAccessor(a) - xAccessor(b)).slice(0, 100);

  let dimensions = {
    width: window.innerWidth * 0.9,
    height: 400,
    margin: {
      top: 15,
      right: 15,
      bottom: 40,
      left: 60
  dimensions.boundedWidth =
    dimensions.width - dimensions.margin.left - dimensions.margin.right;
  dimensions.boundedHeight =
    dimensions.height - - dimensions.margin.bottom;

  // 3. Draw canvas

  const wrapper = d3
    .attr("width", dimensions.width)
    .attr("height", dimensions.height);

  const bounds = wrapper
      `translate(${dimensions.margin.left}px, ${}px)`

    .attr("id", "bounds-clip-path")
    .attr("width", dimensions.boundedWidth)
    .attr("height", dimensions.boundedHeight);

  // init static elements
  bounds.append("rect").attr("class", "freezing");
  const clip = bounds.append("g").attr("clip-path", "url(#bounds-clip-path)");

  clip.append("path").attr("class", "line");

    .attr("class", "x-axis")
    .style("transform", `translateY(${dimensions.boundedHeight}px)`);
  bounds.append("g").attr("class", "y-axis");

  const drawLine = dataset => {
    // 4. Create scales

    const yScale = d3
      .domain(d3.extent(dataset, yAccessor))
      .range([dimensions.boundedHeight, 0]);

    const freezingTemperaturePlacement = yScale(32);
    const freezingTemperatures = bounds
      .attr("x", 0)
      .attr("width", dimensions.boundedWidth)
      .attr("y", freezingTemperaturePlacement)
      .attr("height", dimensions.boundedHeight - freezingTemperaturePlacement);

    const xScale = d3
      .domain(d3.extent(dataset, xAccessor))
      .range([0, dimensions.boundedWidth]);

    // 5. Draw data

    const lineGenerator = d3
      .x(d => xScale(xAccessor(d)))
      .y(d => yScale(yAccessor(d)));

    const lastTwoPoints = dataset.slice(-2);
    const pixelsBetweenLastPoints =
      xScale(xAccessor(lastTwoPoints[1])) - xScale(xAccessor(lastTwoPoints[0]));

    const line = bounds
      .attr("d", lineGenerator(dataset))
      .style("transform", `translate(${pixelsBetweenLastPoints}px)`)
      .style("transform", "none");

    // 6. Draw peripherals

    const yAxisGenerator = d3.axisLeft().scale(yScale);

    const yAxis =".y-axis").call(yAxisGenerator);

    const xAxisGenerator = d3.axisBottom().scale(xScale);

    const xAxis = bounds

  // update the line every 1.5 seconds
  setInterval(addNewDay, 1500);
  function addNewDay() {
    dataset = [...dataset.slice(1), generateNewDataPoint(dataset)];

  function generateNewDataPoint(dataset) {
    const lastDataPoint = dataset[dataset.length - 1];
    const nextDay = d3.timeDay.offset(xAccessor(lastDataPoint), 1);

    return {
      date: d3.timeFormat("%Y-%m-%d")(nextDay),
      temperatureMax: yAccessor(lastDataPoint) + (Math.random() * 6 - 3)