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 stream = fc.randomFinancial().stream();
//const data = stream.take(500);

var taken = stream.take(50);
var xZoom = 10;
var xPan = 0;
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

const data = {
  series: taken,
  crosshair: []
};

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

// secondary scales for the purposes of computing the zoom
var x2 = d3.scaleTime();
var y2 = d3.scaleLinear();

const xExtent = fc.extentDate()
  .accessors([
    () => new Date(taken[0].date.addDays(-xZoom + xPan)), // Left pad
    d => d.date,
    () => new Date(taken[taken.length - 1].date.addDays(xZoom + xPan)) // Right pad
  ])
  //.pad([xZoom, xZoom]) // This worked only the first time for some reason


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


let lastY = 0;
let yOffset = 0;

const zoom = d3.zoom()
  .on('zoom', () => {   
    const t = d3.event.transform;
    //x.domain(t.rescaleX(x2).domain()); // Don't zoom X any longer
    y.domain(t.rescaleY(y2).domain());
    
    //console.log("new t.x:", t.x); // This changes even when Y zooming!
    
    const zoomFactor = Math.sign(d3.event.sourceEvent.wheelDelta) * -3; // Read from mouse wheel event directly
    if (zoomFactor) xZoom += zoomFactor;
    if (xZoom < 0) xZoom = 0; // Inverting zoom fix
    
    console.log("xz", xZoom)
    
    const panFactor = Math.sign(d3.event.sourceEvent.movementX) * -3; // Read from mouse wheel event directly
    if (panFactor) xPan += panFactor;
    
    chart.xDomain(xExtent(data.series)); // Recalculate series extent
    
    render();
  });

/*const yAxisZoom = d3.zoom()
  .on('zoom', () => {
    y.domain(d3.event.transform.rescaleY(y2).domain());
    render();
  });*/

const yAxisDrag = d3.drag()
  .on('drag', () => {
    const factor = Math.pow(2, -d3.event.dy * 0.01);
    d3.select('#zoom-chart .plot-area').call(zoom.scaleBy, factor); // No need to call yAxizZoom any longer
  }); 
 
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 = fc.chartCanvasCartesian(x, y)
  .plotArea(multichart)
  .yDecorate((selection, d) => {
     d3.select('#zoom-chart')
      .select('.y-axis')
      .call(yAxisDrag)
  })
  .decorate((sel) => {
    sel.enter()
      .select('.plot-area')
      .on('measure.range', () => {
        x2.range([0, d3.event.detail.width]);
        y2.range([d3.event.detail.height, 0]);
      })
     .call(zoom); 
  });

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

function updateData() {
  chart
    .xDomain(xExtent(data.series))
    .yDomain(yExtent(data.series));

  x2.domain(chart.xDomain());
  y2.domain(chart.yDomain());

  render();
}

updateData(data.series)
              
            
!
999px

Console