ChartingAllPuts = {
let puts = OptionChain.map(x => [...x.puts]).reduce(function(arr, value) {
return (arr = [...value, ...arr]);
}, []);
let options = puts.map(x => x[PROPERTY]);
let strikes = puts.map(x => x.strike);
if (options.length !== strikes.length)
throw 'diff num of options and strikes';
let x = d3
.scaleLinear()
.domain(d3.extent(strikes))
.range([
width / 2 + DIM.CHART_LEFT_PADDING,
width / 2 + DIM.CHART_LEFT_PADDING + CHART_WIDTH
]);
let y = d3
.scaleLinear()
.domain(d3.extent(options))
.nice()
.range([
height - DIM.BOTTOM_MARGIN - DIM.CHART_BOTTOM_PADDING,
DIM.TOP_MARGIN + DIM.CHART_TOP_PADDING
])
.clamp(true)
.unknown(height - DIM.BOTTOM_MARGIN - box[1]);
let xAxis = g =>
g
.attr(
"transform",
`translate(${0},
${DIM.TOP_MARGIN + DIM.CHART_TOP_PADDING})`
)
.call(
d3
.axisBottom(x)
.tickSize(CHART_HEIGHT)
.tickPadding(5)
.tickValues(strikes)
.tickFormat(function(d, i) {
return strikes.includes(d) ? d3.format('3,.4')(d) : "";
})
);
let yAxis = g =>
g
.attr(
"transform",
`translate(${width / 2 + DIM.CHART_LEFT_PADDING},
${0})`
)
.call(d3.axisLeft(y).tickSize(-CHART_WIDTH))
.call(g => g.select(".domain").remove())
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(d => "y-axis units")
);
d3.select(Puts)
.selectAll('.xAxis')
.data(['_'], d => d)
.join('g')
.classed('xAxis', true)
.call(xAxis);
d3.select(Puts)
.selectAll('.yAxis')
.data(['_'], d => d)
.join('g')
.classed('yAxis', true)
.call(yAxis);
d3.select(Puts)
.selectAll('.put-box')
.data(strikes, (d, i) => i)
.join('rect')
.attr('class', 'put-box')
.attr('x', d => x(d) - box[0] / 2)
.attr('width', box[0])
.attr('y', (d, i) => {
let value = puts[i][PROPERTY] ? puts[i][PROPERTY] : 'null';
return y(value) - box[1] / 2;
})
.attr('height', box[1])
.attr('stroke', 'white');
}