Fixes variables scopes (2)

This commit is contained in:
Roman Galochkin 2020-02-20 13:33:21 +03:00
parent 8d2602ee77
commit e2d5426616

View File

@ -156,8 +156,8 @@ export class CdfChartD3 {
'translate(' + this.calc.chartLeftMargin + ',' + this.calc.chartTopMargin + ')', 'translate(' + this.calc.chartLeftMargin + ',' + this.calc.chartTopMargin + ')',
); );
this.addDistributionChart(); const distributionChart = this.addDistributionChart();
this.addLollipopsChart(); this.addLollipopsChart(distributionChart);
return this; return this;
} }
@ -172,18 +172,19 @@ export class CdfChartD3 {
const yMax = d3.max(this.attrs.data.primary.ys); const yMax = d3.max(this.attrs.data.primary.ys);
// Scales. // Scales.
let xScale = null;
if (this.attrs.scale === 'linear') { if (this.attrs.scale === 'linear') {
this.xScale = d3.scaleLinear() xScale = d3.scaleLinear()
.domain([xMin, xMax]) .domain([xMin, xMax])
.range([0, this.calc.chartWidth]); .range([0, this.calc.chartWidth]);
} else { } else {
this.xScale = d3.scaleLog() xScale = d3.scaleLog()
.base(this.attrs.logBase) .base(this.attrs.logBase)
.domain([xMin, xMax]) .domain([xMin, xMax])
.range([0, this.calc.chartWidth]); .range([0, this.calc.chartWidth]);
} }
this.yScale = d3.scaleLinear() const yScale = d3.scaleLinear()
.domain([yMin, yMax]) .domain([yMin, yMax])
.range([this.calc.chartHeight, 0]); .range([this.calc.chartHeight, 0]);
@ -206,7 +207,7 @@ export class CdfChartD3 {
.ticks(this.getTimeTicksByStr(unit)) .ticks(this.getTimeTicksByStr(unit))
.tickFormat(this.formatDates); .tickFormat(this.formatDates);
} else { } else {
xAxis = d3.axisBottom(this.xScale) xAxis = d3.axisBottom(xScale)
.ticks(3) .ticks(3)
.tickFormat(d => { .tickFormat(d => {
if (Math.abs(d) < 1) { if (Math.abs(d) < 1) {
@ -221,16 +222,16 @@ export class CdfChartD3 {
}); });
} }
const yAxis = d3.axisRight(this.yScale); const yAxis = d3.axisRight(yScale);
// Objects. // Objects.
const line = d3.line() const line = d3.line()
.x(d => this.xScale(d.x)) .x(d => xScale(d.x))
.y(d => this.yScale(d.y)); .y(d => yScale(d.y));
const area = d3.area() const area = d3.area()
.x(d => this.xScale(d.x)) .x(d => xScale(d.x))
.y1(d => this.yScale(d.y)) .y1(d => yScale(d.y))
.y0(this.calc.chartHeight); .y0(this.calc.chartHeight);
// Add axis. // Add axis.
@ -269,8 +270,8 @@ export class CdfChartD3 {
if (this.attrs.showVerticalLine) { if (this.attrs.showVerticalLine) {
this.chart this.chart
.createObject({ tag: 'line', selector: 'v-line' }) .createObject({ tag: 'line', selector: 'v-line' })
.attr('x1', this.xScale(this.attrs.verticalLine)) .attr('x1', xScale(this.attrs.verticalLine))
.attr('x2', this.xScale(this.attrs.verticalLine)) .attr('x2', xScale(this.attrs.verticalLine))
.attr('y1', 0) .attr('y1', 0)
.attr('y2', this.calc.chartHeight) .attr('y2', this.calc.chartHeight)
.attr('stroke-width', 1.5) .attr('stroke-width', 1.5)
@ -293,8 +294,8 @@ export class CdfChartD3 {
{ {
const context = this; const context = this;
const range = [ const range = [
this.xScale(dataPoints[dataPoints.length - 1][0].x), xScale(dataPoints[dataPoints.length - 1][0].x),
this.xScale( xScale(
dataPoints dataPoints
[dataPoints.length - 1] [dataPoints.length - 1]
[dataPoints[dataPoints.length - 1].length - 1].x, [dataPoints[dataPoints.length - 1].length - 1].x,
@ -305,7 +306,7 @@ export class CdfChartD3 {
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 = mouse[0] > range[0] && mouse[0] < range[1] const xValue = mouse[0] > range[0] && mouse[0] < range[1]
? context.xScale.invert(mouse[0]).toFixed(2) ? xScale.invert(mouse[0]).toFixed(2)
: 0; : 0;
context.attrs.onHover(xValue); context.attrs.onHover(xValue);
} }
@ -324,30 +325,25 @@ export class CdfChartD3 {
.on('mousemove', mouseover) .on('mousemove', mouseover)
.on('mouseout', mouseout); .on('mouseout', mouseout);
} }
return { xScale, yScale };
} }
addLollipopsChart() { addLollipopsChart(distributionChart) {
const data = [ const data = this.getDataPoints('discrete');
{ x: 3, y: 10 }, const ys = data.map(item => item.y);
{ x: 5, y: 30 }, const yMin = d3.min(ys);
{ x: 7.5, y: 50 }, const yMax = d3.max(ys);
];
const xs = [3, 5, 7.5];
// X axis // X axis
const x = d3.scaleBand()
.range([0, this.calc.chartWidth])
.domain(xs)
.padding(1);
this.chart.append("g") this.chart.append("g")
.attr("class", 'lollipops-x-axis') .attr("class", 'lollipops-x-axis')
.attr("transform", "translate(0," + this.calc.chartHeight + ")") .attr("transform", "translate(0," + this.calc.chartHeight + ")")
.call(d3.axisBottom(x)); .call(d3.axisBottom(distributionChart.xScale));
// Y axis // Y axis
const y = d3.scaleLinear() const y = d3.scaleLinear()
.domain([0, 50]) .domain([yMin, yMax])
.range([this.calc.chartHeight, 0]); .range([this.calc.chartHeight, 0]);
this.chart.append("g") this.chart.append("g")
@ -361,8 +357,8 @@ export class CdfChartD3 {
.enter() .enter()
.append("line") .append("line")
.attr("class", 'lollipops-line') .attr("class", 'lollipops-line')
.attr("x1", d => x(d.x)) .attr("x1", d => distributionChart.xScale(d.x))
.attr("x2", d => x(d.x)) .attr("x2", d => distributionChart.xScale(d.x))
.attr("y1", d => y(d.y)) .attr("y1", d => y(d.y))
.attr("y2", y(0)); .attr("y2", y(0));
@ -372,7 +368,7 @@ export class CdfChartD3 {
.enter() .enter()
.append("circle") .append("circle")
.attr("class", 'lollipops-circle') .attr("class", 'lollipops-circle')
.attr("cx", d => x(d.x)) .attr("cx", d => distributionChart.xScale(d.x))
.attr("cy", d => y(d.y)) .attr("cy", d => y(d.y))
.attr("r", "4"); .attr("r", "4");
} }
@ -416,7 +412,8 @@ export class CdfChartD3 {
*/ */
getDataPoints(key) { getDataPoints(key) {
const dt = []; const dt = [];
const data = this.attrs.data[key]; const emptyShape = { xs: [], ys: [] };
const data = _.get(this.attrs.data, key, emptyShape);
const len = data.xs.length; const len = data.xs.length;
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {