Fixes the code styles and the construction of the code

This commit is contained in:
Roman Galochkin 2020-02-26 10:12:13 +03:00
parent 6a54d24550
commit ca7cf60f5c

View File

@ -129,8 +129,14 @@ export class CdfChartD3 {
data(data) { data(data) {
this.attrs.data = data; this.attrs.data = data;
this.attrs.data.continuous = data.continuous || {xs: [], ys: []}; this.attrs.data.continuous = data.continuous || {
this.attrs.data.discrete = data.discrete || {xs: [], ys: []}; xs: [],
ys: [],
};
this.attrs.data.discrete = data.discrete || {
xs: [],
ys: [],
};
return this; return this;
} }
@ -149,8 +155,12 @@ export class CdfChartD3 {
// Calculated properties. // Calculated properties.
this.calc.chartLeftMargin = this.attrs.marginLeft; this.calc.chartLeftMargin = this.attrs.marginLeft;
this.calc.chartTopMargin = this.attrs.marginTop; this.calc.chartTopMargin = this.attrs.marginTop;
this.calc.chartWidth = this.attrs.svgWidth - this.attrs.marginRight - this.attrs.marginLeft; this.calc.chartWidth = this.attrs.svgWidth
this.calc.chartHeight = this.attrs.svgHeight - this.attrs.marginBottom - this.attrs.marginTop; - this.attrs.marginRight
- this.attrs.marginLeft;
this.calc.chartHeight = this.attrs.svgHeight
- this.attrs.marginBottom
- this.attrs.marginTop;
// Add svg. // Add svg.
this.svg = this._container this.svg = this._container
@ -159,17 +169,17 @@ export class CdfChartD3 {
.attr('height', this.attrs.svgHeight) .attr('height', this.attrs.svgHeight)
.attr('pointer-events', 'none'); .attr('pointer-events', 'none');
// Add container g element. // Add container "g" (empty) element.
this.chart = this.svg this.chart = this.svg
.createObject({ tag: 'g', selector: 'chart' }) .createObject({ tag: 'g', selector: 'chart' })
.attr( .attr(
'transform', 'transform',
'translate(' + this.calc.chartLeftMargin + ',' + this.calc.chartTopMargin + ')', `translate(${this.calc.chartLeftMargin}, ${this.calc.chartTopMargin})`,
); );
if(this.hasDate('continuous')){ if (this.hasDate('continuous')) {
const distributionChart = this.addDistributionChart(); const distributionChart = this.addDistributionChart();
if(this.hasDate('discrete')) { if (this.hasDate('discrete')) {
this.addLollipopsChart(distributionChart); this.addLollipopsChart(distributionChart);
} }
} }
@ -186,32 +196,36 @@ export class CdfChartD3 {
const yMin = d3.min(this.attrs.data.continuous.ys); const yMin = d3.min(this.attrs.data.continuous.ys);
const yMax = d3.max(this.attrs.data.continuous.ys); const yMax = d3.max(this.attrs.data.continuous.ys);
// Scales. // X-domains.
let xScale = null; const xMinDomain = xMin;
if (this.attrs.scale === 'linear') { const xMaxDomain = xMax;
xScale = d3.scaleLinear()
.domain([xMin, xMax])
.range([0, this.calc.chartWidth]);
} else {
xScale = d3.scaleLog()
.base(this.attrs.logBase)
.domain([xMin, xMax])
.range([0, this.calc.chartWidth]);
}
// X-scale.
let xScale = this.attrs.scale === 'linear'
? d3.scaleLinear()
.domain([xMinDomain, xMaxDomain])
.range([0, this.calc.chartWidth])
: d3.scaleLog()
.base(this.attrs.logBase)
.domain([xMinDomain, xMaxDomain])
.range([0, this.calc.chartWidth]);
// Y-scale.
const yScale = d3.scaleLinear() const yScale = d3.scaleLinear()
.domain([yMin, yMax]) .domain([yMin, yMax])
.range([this.calc.chartHeight, 0]); .range([this.calc.chartHeight, 0]);
// Axis generator. // X-axis.
let xAxis = null; let xAxis = null;
if (!!this.attrs.timeScale) { if (!!this.attrs.timeScale) {
const zero = _.get(this.attrs.timeScale, 'zero', moment()); // Calculates the projection on X-axis.
const unit = _.get(this.attrs.timeScale, 'unit', 'years'); const zero = _.get(this.attrs, 'timeScale.zero', moment());
const unit = _.get(this.attrs, 'timeScale.unit', 'years');
const diff = Math.abs(xMax - xMin); const diff = Math.abs(xMax - xMin);
const left = zero.clone().add(xMin, unit); const left = zero.clone().add(xMin, unit);
const right = left.clone().add(diff, unit); const right = left.clone().add(diff, unit);
// X-time-scale.
const xScaleTime = d3.scaleTime() const xScaleTime = d3.scaleTime()
.domain([left.toDate(), right.toDate()]) .domain([left.toDate(), right.toDate()])
.nice() .nice()
@ -237,6 +251,7 @@ export class CdfChartD3 {
}); });
} }
// Y-axis.
const yAxis = d3.axisRight(yScale); const yAxis = d3.axisRight(yScale);
// Objects. // Objects.
@ -250,12 +265,14 @@ export class CdfChartD3 {
.y0(this.calc.chartHeight); .y0(this.calc.chartHeight);
// Add axis. // Add axis.
this.chart.createObject({ tag: 'g', selector: 'x-axis' }) this.chart
.attr('transform', 'translate(0,' + this.calc.chartHeight + ')') .createObject({ tag: 'g', selector: 'x-axis' })
.attr('transform', `translate(0, ${this.calc.chartHeight})`)
.call(xAxis); .call(xAxis);
if (this.attrs.showDistributionYAxis) { if (this.attrs.showDistributionYAxis) {
this.chart.createObject({ tag: 'g', selector: 'y-axis' }) this.chart
.createObject({ tag: 'g', selector: 'y-axis' })
.call(yAxis); .call(yAxis);
} }
@ -313,15 +330,16 @@ export class CdfChartD3 {
function mouseover() { function mouseover() {
const mouse = d3.mouse(this); const mouse = d3.mouse(this);
hoverLine.attr('opacity', 1).attr('x1', mouse[0]).attr('x2', mouse[0]); hoverLine
.attr('opacity', 1)
.attr('x1', mouse[0])
.attr('x2', mouse[0]);
const xValue = xScale.invert(mouse[0]); const xValue = xScale.invert(mouse[0]);
// This used to be here, but doesn't seem important
// const xValue = (mouse[0] > range[0] && mouse[0] < range[1]) ? : 0;
context.attrs.onHover(xValue); context.attrs.onHover(xValue);
} }
function mouseout() { function mouseout() {
hoverLine.attr('opacity', 0) hoverLine.attr('opacity', 0);
} }
this.chart this.chart
@ -338,49 +356,64 @@ export class CdfChartD3 {
return { xScale, yScale }; return { xScale, yScale };
} }
/**
* @param {object} distributionChart
* @param {object} distributionChart.xScale
* @param {object} distributionChart.yScale
*/
addLollipopsChart(distributionChart) { addLollipopsChart(distributionChart) {
const data = this.getDataPoints('discrete'); const data = this.getDataPoints('discrete');
const ys = data.map(item => item.y);
const yMax = d3.max(ys);
// X axis const _yMin = d3.min(this.attrs.data.discrete.ys);
this.chart.append("g") const yMax = d3.max(this.attrs.data.discrete.ys);
.attr("class", 'lollipops-x-axis')
.attr("transform", "translate(0," + this.calc.chartHeight + ")") // X axis.
this.chart.append('g')
.attr('class', 'lollipops-x-axis')
.attr('transform', `translate(0, ${this.calc.chartHeight})`)
.call(d3.axisBottom(distributionChart.xScale)); .call(d3.axisBottom(distributionChart.xScale));
// Y axis // Y-domain.
const yMinDomain = 0;
const yMaxDomain = yMax * 2;
// Y-scale.
const yScale = d3.scaleLinear() const yScale = d3.scaleLinear()
.domain([0, yMax]) .domain([yMinDomain, yMaxDomain])
.range([this.calc.chartHeight, 0]); .range([this.calc.chartHeight, 0]);
this.chart.append("g") // Adds "g" for an y-axis.
.attr("class", 'lollipops-y-axis') this.chart.append('g')
.attr("transform", "translate(" + this.calc.chartWidth + ",0)") .attr('class', 'lollipops-y-axis')
.attr('transform', `translate(${this.calc.chartWidth}, 0)`)
.call(d3.axisLeft(yScale)); .call(d3.axisLeft(yScale));
// Lines // Lines.
this.chart.selectAll("lollipops-line") this.chart.selectAll('lollipops-line')
.data(data) .data(data)
.enter() .enter()
.append("line") .append('line')
.attr("class", 'lollipops-line') .attr('class', 'lollipops-line')
.attr("x1", d => distributionChart.xScale(d.x)) .attr('x1', d => distributionChart.xScale(d.x))
.attr("x2", d => distributionChart.xScale(d.x)) .attr('x2', d => distributionChart.xScale(d.x))
.attr("y1", d => yScale(d.y)) .attr('y1', d => yScale(d.y))
.attr("y2", yScale(0)); .attr('y2', yScale(0));
// Circles // Circles.
this.chart.selectAll("lollipops-circle") this.chart.selectAll('lollipops-circle')
.data(data) .data(data)
.enter() .enter()
.append("circle") .append('circle')
.attr("class", 'lollipops-circle') .attr('class', 'lollipops-circle')
.attr("cx", d => distributionChart.xScale(d.x)) .attr('cx', d => distributionChart.xScale(d.x))
.attr("cy", d => yScale(d.y)) .attr('cy', d => yScale(d.y))
.attr("r", "4"); .attr('r', '4');
} }
/**
* @param ts
* @returns {string}
*/
formatDates(ts) { formatDates(ts) {
return moment(ts).format("MMMM Do YYYY"); return moment(ts).format("MMMM Do YYYY");
} }
@ -436,8 +469,8 @@ export class CdfChartD3 {
* @returns {boolean} * @returns {boolean}
*/ */
hasDate(key) { hasDate(key) {
const data = _.get(this.attrs.data, key); const xs = _.get(this.attrs, ['data', key, 'xs']);
return !!data; return !!_.size(xs);
} }
} }