squiggle/src/components/charts/cdfChartD3.js

361 lines
8.7 KiB
JavaScript
Raw Normal View History

2020-02-19 07:36:11 +00:00
const _ = require('lodash');
2020-02-18 11:29:51 +00:00
const d3 = require('d3');
const moment = require('moment');
2020-02-19 07:36:11 +00:00
export class CdfChartD3 {
2020-02-18 11:29:51 +00:00
constructor() {
this.attrs = {
svgWidth: 400,
svgHeight: 400,
marginTop: 5,
marginBottom: 5,
marginRight: 50,
marginLeft: 5,
2020-02-19 07:36:11 +00:00
container: null,
2020-02-18 11:29:51 +00:00
minX: false,
maxX: false,
scale: 'linear',
2020-02-19 07:36:11 +00:00
timeScale: null,
2020-02-18 11:29:51 +00:00
showDistributionLines: true,
areaColors: ['#E1E5EC', '#E1E5EC'],
logBase: 10,
verticalLine: 110,
showVerticalLine: true,
data: null,
onHover: (e) => {
},
};
this.hoverLine = null;
this.xScale = null;
2020-02-19 07:36:11 +00:00
this.xScaleTime = null;
2020-02-18 11:29:51 +00:00
this.dataPoints = null;
this.mouseover = this.mouseover.bind(this);
this.mouseout = this.mouseout.bind(this);
2020-02-18 11:58:34 +00:00
this.formatDates = this.formatDates.bind(this);
2020-02-18 11:29:51 +00:00
}
svgWidth(svgWidth) {
this.attrs.svgWidth = svgWidth;
return this;
}
svgHeight(height) {
this.attrs.svgHeight = height;
return this;
}
maxX(maxX) {
this.attrs.maxX = maxX;
return this;
}
minX(minX) {
this.attrs.minX = minX;
return this;
}
2020-02-18 12:11:22 +00:00
scale(scale) {
this.attrs.scale = scale;
return this;
}
2020-02-19 07:36:11 +00:00
timeScale(timeScale) {
this.attrs.timeScale = timeScale;
return this;
}
2020-02-18 11:29:51 +00:00
onHover(onHover) {
this.attrs.onHover = onHover;
return this;
}
marginBottom(marginBottom) {
this.attrs.marginBottom = marginBottom;
return this;
}
marginLeft(marginLeft) {
this.attrs.marginLeft = marginLeft;
return this;
}
marginRight(marginRight) {
this.attrs.marginRight = marginRight;
return this;
}
marginTop(marginTop) {
this.attrs.marginTop = marginTop;
return this;
}
showDistributionLines(showDistributionLines) {
this.attrs.showDistributionLines = showDistributionLines;
return this;
}
verticalLine(verticalLine) {
this.attrs.verticalLine = verticalLine;
return this;
}
showVerticalLine(showVerticalLine) {
this.attrs.showVerticalLine = showVerticalLine;
return this;
}
container(container) {
this.attrs.container = container;
return this;
}
data(data) {
this.attrs.data = data;
return this;
}
2020-02-18 11:29:51 +00:00
/**
* @param key
* @returns {[]}
*/
getDatapoints(key) {
const dt = [];
const data = this.attrs.data[key];
const len = data.xs.length;
for (let i = 0; i < len; i++) {
2020-02-19 07:36:11 +00:00
dt.push({ x: data.xs[i], y: data.ys[i] });
}
2020-02-18 11:29:51 +00:00
return dt;
}
render() {
const attrs = this.attrs;
const container = d3.select(attrs.container);
if (container.node() === null) return;
2020-02-19 07:36:11 +00:00
// Sets the width from the DOM element.
2020-02-18 09:31:47 +00:00
const containerRect = container.node().getBoundingClientRect();
if (containerRect.width > 0) {
attrs.svgWidth = containerRect.width;
}
// Calculated properties.
// id for event handlings.
2020-02-18 09:31:47 +00:00
const calc = {};
calc.id = 'ID' + Math.floor(Math.random() * 1000000);
calc.chartLeftMargin = attrs.marginLeft;
calc.chartTopMargin = attrs.marginTop;
calc.chartWidth = attrs.svgWidth - attrs.marginRight - attrs.marginLeft;
calc.chartHeight = attrs.svgHeight - attrs.marginBottom - attrs.marginTop;
2020-02-18 09:31:47 +00:00
const areaColor = d3.scaleOrdinal().range(attrs.areaColors);
2020-02-18 11:29:51 +00:00
this.dataPoints = [this.getDatapoints('primary')];
// Scales.
2020-02-18 09:31:47 +00:00
const xMin = d3.min(attrs.data.primary.xs);
const xMax = d3.max(attrs.data.primary.xs);
if (attrs.scale === 'linear') {
2020-02-18 11:33:32 +00:00
this.xScale = d3.scaleLinear()
2020-02-18 11:58:34 +00:00
.domain([attrs.minX || xMin, attrs.maxX || xMax])
.range([0, calc.chartWidth]);
} else {
2020-02-18 11:33:32 +00:00
this.xScale = d3.scaleLog()
.base(attrs.logBase)
2020-02-18 09:31:47 +00:00
.domain([attrs.minX, attrs.maxX])
.range([0, calc.chartWidth]);
}
2020-02-18 09:31:47 +00:00
const yMin = d3.min(attrs.data.primary.ys);
const yMax = d3.max(attrs.data.primary.ys);
2020-02-18 11:33:32 +00:00
this.yScale = d3.scaleLinear()
2020-02-18 09:31:47 +00:00
.domain([yMin, yMax])
.range([calc.chartHeight, 0]);
// Axis generator.
2020-02-19 07:36:11 +00:00
if (!!this.attrs.timeScale) {
const zero = _.get(this.attrs.timeScale, 'zero', moment());
const unit = _.get(this.attrs.timeScale, 'unit', null);
const length = zero.clone().add('year', 5);
this.xScaleTime = d3.scaleLinear()
.domain([zero.toDate(), length.toDate()])
.range([0, calc.chartWidth]);
2020-02-18 11:58:34 +00:00
this.xAxis = d3.axisBottom()
2020-02-19 07:36:11 +00:00
.scale(this.xScaleTime)
2020-02-18 11:58:34 +00:00
.ticks(5)
.tickFormat(this.formatDates);
} else {
this.xAxis = d3.axisBottom(this.xScale)
.ticks(3)
.tickFormat(d => {
if (Math.abs(d) < 1) {
return d3.format(".2")(d);
} else if (xMin > 1000 && xMax < 3000) {
// Condition which identifies years; 2019, 2020, 2021.
return d3.format(".0")(d);
} else {
const prefix = d3.formatPrefix(".0", d);
return prefix(d).replace("G", "B");
}
});
}
// Line generator.
2020-02-18 09:31:47 +00:00
const line = d3.line()
2020-02-18 11:33:32 +00:00
.x(d => this.xScale(d.x))
.y(d => this.yScale(d.y));
2020-02-18 09:31:47 +00:00
const area = d3.area()
2020-02-18 11:33:32 +00:00
.x(d => this.xScale(d.x))
.y1(d => this.yScale(d.y))
.y0(calc.chartHeight);
// Add svg.
2020-02-18 09:31:47 +00:00
const svg = container
.patternify({ tag: 'svg', selector: 'svg-chart-container' })
.attr('width', "100%")
.attr('height', attrs.svgHeight)
.attr('pointer-events', 'none');
// Add container g element.
2020-02-18 11:29:51 +00:00
this.chart = svg
.patternify({ tag: 'g', selector: 'chart' })
.attr(
'transform',
'translate(' + calc.chartLeftMargin + ',' + calc.chartTopMargin + ')',
);
// Add axis.
2020-02-18 11:29:51 +00:00
this.chart.patternify({ tag: 'g', selector: 'axis' })
.attr('transform', 'translate(' + 0 + ',' + calc.chartHeight + ')')
2020-02-18 11:58:34 +00:00
.call(this.xAxis);
// Draw area.
2020-02-18 11:29:51 +00:00
this.chart
.patternify({
tag: 'path',
selector: 'area-path',
2020-02-18 11:58:34 +00:00
data: this.dataPoints,
})
.attr('d', area)
.attr('fill', (d, i) => areaColor(i))
2020-02-18 09:31:47 +00:00
.attr('opacity', (d, i) => i === 0 ? 0.7 : 0.5);
// Draw line.
if (attrs.showDistributionLines) {
2020-02-18 11:29:51 +00:00
this.chart
.patternify({
tag: 'path',
selector: 'line-path',
2020-02-18 11:58:34 +00:00
data: this.dataPoints,
})
.attr('d', line)
.attr('id', (d, i) => 'line-' + (i + 1))
2020-02-18 11:58:34 +00:00
.attr('opacity', (d, i) => i === 0 ? 0.7 : 1)
.attr('fill', 'none');
}
if (attrs.showVerticalLine) {
2020-02-18 11:29:51 +00:00
this.chart
.patternify({ tag: 'line', selector: 'v-line' })
2020-02-18 11:58:34 +00:00
.attr('x1', this.xScale(attrs.verticalLine))
.attr('x2', this.xScale(attrs.verticalLine))
.attr('y1', 0)
.attr('y2', calc.chartHeight)
.attr('stroke-width', 1.5)
.attr('stroke-dasharray', '6 6')
.attr('stroke', 'steelblue');
}
2020-02-18 11:29:51 +00:00
this.hoverLine = this.chart
.patternify({ tag: 'line', selector: 'hover-line' })
.attr('x1', 0)
.attr('x2', 0)
.attr('y1', 0)
.attr('y2', calc.chartHeight)
.attr('opacity', 0)
.attr('stroke-width', 1.5)
.attr('stroke-dasharray', '6 6')
.attr('stroke', '#22313F');
// Add drawing rectangle.
2020-02-18 11:29:51 +00:00
const thi$ = this;
this.chart
.patternify({ tag: 'rect', selector: 'mouse-rect' })
.attr('width', calc.chartWidth)
.attr('height', calc.chartHeight)
.attr('fill', 'transparent')
.attr('pointer-events', 'all')
2020-02-18 11:29:51 +00:00
.on('mouseover', function () {
thi$.mouseover(this);
})
.on('mousemove', function () {
thi$.mouseover(this);
})
.on('mouseout', this.mouseout);
2020-02-18 11:29:51 +00:00
return this;
}
2020-02-18 11:29:51 +00:00
mouseover(constructor) {
const mouse = d3.mouse(constructor);
this.hoverLine.attr('opacity', 1)
.attr('x1', mouse[0])
.attr('x2', mouse[0]);
const xValue = this.xScale.invert(mouse[0]).toFixed(2);
const range = [
this.xScale(this.dataPoints[this.dataPoints.length - 1][0].x),
this.xScale(
this.dataPoints
[this.dataPoints.length - 1]
[this.dataPoints[this.dataPoints.length - 1].length - 1].x,
),
];
if (mouse[0] > range[0] && mouse[0] < range[1]) {
this.attrs.onHover(xValue);
} else {
this.attrs.onHover(0.0);
}
2020-02-18 09:31:47 +00:00
}
2020-02-18 11:29:51 +00:00
mouseout() {
this.hoverLine.attr('opacity', 0)
}
2020-02-18 11:58:34 +00:00
formatDates(ts) {
return moment(ts).format("dddd, MMMM Do YYYY, h:mm:ss a");
}
2020-02-18 11:29:51 +00:00
}
2020-02-19 05:20:54 +00:00
d3.selection.prototype.patternify = function patternify(params) {
const selector = params.selector;
const elementTag = params.tag;
const data = params.data || [selector];
2020-02-19 07:36:11 +00:00
const selection = this.selectAll('.' + selector)
.data(data, (d, i) => {
if (typeof d === 'object' && d.id) return d.id;
return i;
});
2020-02-19 05:20:54 +00:00
selection.exit().remove();
return selection
.enter()
.append(elementTag)
.merge(selection)
.attr('class', selector);
};