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="zoom-chart" class="chart large">

</div>

              
            
!

CSS

              
                .chart-label {
  font-size: 1.5em;
  line-height: normal;
  color: black;
}

.chart {
  margin: 10px 0;
  width: 100%;
  height: 250px;
}

.chart svg {
  overflow: visible !important;
}

.chart.large {
  height: 400px;
}

.plot-area {
  overflow: visible !important;
}

.y-axis-label {
  white-space: nowrap;
}
#container {
	position: relative;
}
.tooltip {
  position: absolute;
  top: 10px;
  left: 10px;
}
              
            
!

JS

              
                
const interactiveChart = (xScale, yScale) => {
  const zoom = d3.zoom();
  const xZoom = d3.zoom();
  const yZoom = d3.zoom();

  const chart = fc.chartCartesian(xScale, yScale).decorate(sel => {
    const plotAreaNode = sel.select(".plot-area").node();
    const xAxisNode = sel.select(".x-axis").node();
    const yAxisNode = sel.select(".y-axis").node();

    const applyTransform = () => {
      // apply the zoom transform from the x-scale
      xScale.domain(
        d3
          .zoomTransform(xAxisNode)
          .rescaleX(xScaleOriginal)
          .domain()
      );
      // apply the zoom transform from the y-scale
      yScale.domain(
        d3
          .zoomTransform(yAxisNode)
          .rescaleY(yScaleOriginal)
          .domain()
      );
      sel.node().requestRedraw();
    };

    zoom.on("zoom", () => {
      // compute how much the user has zoomed since the last event
      const factor = (plotAreaNode.__zoom.k - plotAreaNode.__zoomOld.k) / plotAreaNode.__zoomOld.k;
      plotAreaNode.__zoomOld = plotAreaNode.__zoom;

      // apply scale to the x & y axis, maintaining their aspect ratio
      xAxisNode.__zoom.k = xAxisNode.__zoom.k * (1 + factor);
      yAxisNode.__zoom.k = yAxisNode.__zoom.k * (1 + factor);

      // apply transform
      xAxisNode.__zoom.x = d3.zoomTransform(plotAreaNode).x;
      yAxisNode.__zoom.y = d3.zoomTransform(plotAreaNode).y;

      applyTransform();
    });

    xZoom.on("zoom", () => {
      plotAreaNode.__zoom.x = d3.zoomTransform(xAxisNode).x;
      applyTransform();
    });

    yZoom.on("zoom", () => {
      plotAreaNode.__zoom.y = d3.zoomTransform(yAxisNode).y;
      applyTransform();
    });

    sel
      .enter()
      .select(".plot-area")
      .on("measure.range", () => {
        xScaleOriginal.range([0, d3.event.detail.width]);
        yScaleOriginal.range([d3.event.detail.height, 0]);
      })
      .call(zoom);
    
    plotAreaNode.__zoomOld = plotAreaNode.__zoom;
    
    // cannot use enter selection as this pulls data through
    sel.selectAll(".y-axis").call(yZoom);
    sel.selectAll(".x-axis").call(xZoom);

    decorate(sel);
  });

  let xScaleOriginal = xScale.copy(),
    yScaleOriginal = yScale.copy();

  let decorate = () => {};

  const instance = selection => chart(selection);

  instance.yDomain = (...args) => {
    if (!args.length) {
      return chart.yDomain();
    }
    yScaleOriginal.domain(...args);
    chart.yDomain(...args);
    return instance;
  };

  instance.xDomain = (...args) => {
    if (!args.length) {
      return chart.xDomain();
    }
    xScaleOriginal.domain(...args);
    chart.xDomain(...args);
    return instance;
  };

  instance.decorate = (...args) => {
    if (!args.length) {
      return decorate;
    }
    decorate = args[0];
    return instance;
  };

  fc.rebindAll(instance, chart, fc.exclude("xDomain", "yDomain", "decorate"));

  return instance;
};



const stream = fc.randomFinancial().stream();
//const data = stream.take(500);
const data = {
  series: stream.take(50),
  crosshair: []
};

var x = d3.scaleTime();
var y = d3.scaleLinear();

const xExtent = fc.extentDate()
  .accessors([d => d.date])

const yExtent = fc.extentLinear()
  .accessors([d => d.high, d => d.low])
  .pad([0.1, 0.1])

const candlesticks = fc.autoBandwidth(fc.seriesCanvasCandlestick())
			.widthFraction(0.75)
			.crossValue(d => d.date)
			.openValue(d => d.open)
			.highValue(d => d.high)
			.lowValue(d => d.low)
			.closeValue(d => d.close)


const multichart = fc.seriesCanvasMulti()
			.series([candlesticks])
      .mapping((data, index, series) => {
      switch(series[index]) {
        case candlesticks:
          return data.series;
        case crosshair:
          return data.crosshair;
      }
    });


const chart = interactiveChart(x, y)
  .canvasPlotArea(multichart);

function render() {
  d3.select('#zoom-chart')
    .datum(data)
    .call(chart);  
}

function updateData() {

  chart
    .xDomain(xExtent(data.series))
    .yDomain(yExtent(data.series));

  render();
}

updateData()
              
            
!
999px

Console