Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 176 additions & 45 deletions src/graphs/control-chart/ControlRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,187 @@ export class ControlRenderer extends ScatterplotRenderer {
timeScale = 'logarithmic';
connectDots = false;

constructor(data, avgMovingRangeFunc, chartName, workTicketsURL) {
constructor(data, chartName, workTicketsURL) {
super(data);
this.chartName = chartName;
this.chartType = 'CONTROL';
this.workTicketsURL = workTicketsURL;
this.avgMovingRangeFunc = avgMovingRangeFunc;
this.dotClass = 'control-dot';
this.yAxisLabel = 'Days';
this.limitData = {};
this.processSignalsData = {};
this.visibleLimits = {};
this.activeProcessSignal = null;
}

renderGraph(graphElementSelector) {
this.drawSvg(graphElementSelector);
this.drawAxes();
this.drawArea();
this.computeGraphLimits();
this.drawGraphLimits(this.y);
this.setupMouseLeaveHandler();
setLimitData(limitData) {
this.limitData = {
naturalProcessLimits: limitData?.naturalProcessLimits || null,
twoSigma: limitData?.twoSigma || null,
oneSigma: limitData?.oneSigma || null,
averageCycleTime: limitData?.averageCycleTime || null,
};
this.topLimit = limitData?.naturalProcessLimits?.upper;
this.drawLimits();
}

setProcessSignalsData(signalsData) {
this.processSignalsData = {
largeChange: signalsData?.largeChange || null,
moderateChange: signalsData?.moderateChange || null,
moderateSustainedShift: signalsData?.moderateSustainedShift || null,
smallSustainedShift: signalsData?.smallSustainedShift || null,
};
}

drawGraphLimits(yScale) {
this.drawHorizontalLine(yScale, this.topLimit, 'purple', 'top-pb', `UPL=${this.topLimit}`);
this.drawHorizontalLine(yScale, this.avgLeadTime, 'orange', 'mid-pb', `Avg=${this.avgLeadTime}`);
setVisibleLimits(limitConfig) {
this.visibleLimits = { ...limitConfig };
this.updateLimitVisibility();
}

if (this.bottomLimit > 0) {
this.drawHorizontalLine(yScale, this.bottomLimit, 'purple', 'bottom-pb', `LPL=${this.bottomLimit}`);
} else {
console.warn('The bottom limit is:', this.bottomLimit);
setActiveProcessSignal(signalType) {
this.hideSignals();
this.activeProcessSignal = signalType;
this.showActiveSignal();
}

drawLimits() {
// Remove existing limits first
this.svg.selectAll('[id^="line-"], [id^="text-"]').remove();

// Draw new limits
Object.entries(this.limitData).forEach(([limitType, limitValue]) => {
if (limitValue) {
this.drawLimit(limitType, limitValue);
}
});

this.updateLimitVisibility();
}

drawLimit(limitType, limitValue) {
const limitConfig = {
naturalProcessLimits: { dash: '3 2', text: 'NPL', color: 'orange' },
twoSigma: { dash: '12 8', text: '2s', color: 'orange' },
oneSigma: { dash: '20 10', text: '1s', color: 'orange' },
averageCycleTime: { dash: '7', text: 'Avg', color: 'purple' },
};

const config = limitConfig[limitType];
if (!config) return;

if (typeof limitValue === 'number') {
this.drawHorizontalLine(this.currentYScale, limitValue, config.color, limitType, `${config.text}=${limitValue}`, config.dash);
} else if (limitValue && typeof limitValue === 'object') {
if (limitValue.upper !== undefined) {
this.drawHorizontalLine(
this.currentYScale,
limitValue.upper,
config.color,
`${limitType}-upper`,
`${config.text}U=${limitValue.upper}`,
config.dash
);
}
if (limitValue.lower !== undefined && limitValue.lower > 0) {
this.drawHorizontalLine(
this.currentYScale,
limitValue.lower,
config.color,
`${limitType}-lower`,
`${config.text}L=${limitValue.lower}`,
config.dash
);
}
}
}

updateLimitVisibility() {
Object.entries(this.visibleLimits).forEach(([limitType, isVisible]) => {
const display = isVisible ? 'block' : 'none';
// Handle both single limits and upper/lower pairs
this.svg.select(`#line-${limitType}`).style('display', display);
this.svg.select(`#text-${limitType}`).style('display', display);
this.svg.select(`#line-${limitType}-upper`).style('display', display);
this.svg.select(`#text-${limitType}-upper`).style('display', display);
this.svg.select(`#line-${limitType}-lower`).style('display', display);
this.svg.select(`#text-${limitType}-lower`).style('display', display);
});
}

showActiveSignal() {
if (!this.activeProcessSignal || !this.processSignalsData[this.activeProcessSignal]) {
return;
}

const signals = this.processSignalsData[this.activeProcessSignal];
this.drawSignals(signals);
}

computeGraphLimits() {
this.avgLeadTime = this.getAvgLeadTime();
const avgMovingRange = this.avgMovingRangeFunc(this.baselineStartDate, this.baselineEndDate);
this.topLimit = Math.ceil(this.avgLeadTime + avgMovingRange * 2.66);
hideSignals() {
this.svg.selectAll('.signal-point').classed('signal-point', false).attr('fill', this.color);
}

this.bottomLimit = Math.ceil(this.avgLeadTime - avgMovingRange * 2.66);
const maxY = this.y.domain()[1] > this.topLimit ? this.y.domain()[1] : this.topLimit + 5;
let minY = this.y.domain()[0];
if (this.bottomLimit > 5) {
minY = this.y.domain()[0] < this.bottomLimit ? this.y.domain()[0] : this.bottomLimit - 5;
drawSignals(signals) {
if (signals.upper && signals.lower) {
[...signals.upper, ...signals.lower].forEach((id) => {
this.svg.select(`#control-${id}`).classed('signal-point', true).transition().duration(200).attr('fill', 'orange');
});
}
this.y.domain([minY, maxY]);
}

renderGraph(graphElementSelector) {
this.drawSvg(graphElementSelector);
this.drawAxes();
this.drawArea();
this.drawLimits();
this.showActiveSignal();
}

populateTooltip(event) {
this.tooltip
.style('pointer-events', 'auto')
.style('opacity', 0.9)
.append('div')
.append('a')
.style('text-decoration', 'underline')
.attr('href', `${this.workTicketsURL}/${event.ticketId}`)
.text(event.ticketId)
.attr('target', '_blank')
.on('click', () => {
this.hideTooltip();
this.tooltip.style('pointer-events', 'auto').style('opacity', 0.9);

if (event.overlappingTickets && event.overlappingTickets.length > 1) {
// Add header for multiple tickets
this.tooltip
.append('div')
.style('font-weight', 'bold')
.style('margin-bottom', '8px')
.text(`${event.overlappingTickets.length} tickets at this point:`);

event.overlappingTickets.forEach((ticket) => {
const ticketDiv = this.tooltip.append('div').style('margin-bottom', '4px');

ticketDiv
.append('a')
.style('text-decoration', 'underline')
.attr('href', `${this.workTicketsURL}/${ticket.ticketId}`)
.text(ticket.ticketId)
.attr('target', '_blank')
.on('click', () => {
this.hideTooltip();
});
});

// Optionally add shared information (date, lead time)
if (event.date && event.metrics) {
this.tooltip.append('div').style('margin-top', '8px').style('font-size', '12px').style('color', '#666').html(`
<div><strong>Date:</strong> ${event.date}</div>
<div><strong>Lead Time:</strong> ${event.metrics.leadTime} days</div>
`);
}
} else {
this.tooltip
.append('div')
.append('a')
.style('text-decoration', 'underline')
.attr('href', `${this.workTicketsURL}/${event.ticketId}`)
.text(event.ticketId)
.attr('target', '_blank')
.on('click', () => {
this.hideTooltip();
});
}
}

drawScatterplot(chartArea, data, x, y) {
Expand All @@ -74,7 +198,12 @@ export class ControlRenderer extends ScatterplotRenderer {
.attr('class', this.dotClass)
.attr('id', (d) => `control-${d.ticketId}`)
.attr('data-date', (d) => d.deliveredDate)
.attr('r', 5)
.attr('r', (d) => {
const overlapping = data.filter(
(item) => item.deliveredDate.getTime() === d.deliveredDate.getTime() && item.leadTime === d.leadTime
);
return overlapping.length > 1 ? 7 : 5;
})
.attr('cx', (d) => x(d.deliveredDate))
.attr('cy', (d) => this.applyYScale(y, d.leadTime))
.style('cursor', 'pointer')
Expand All @@ -83,11 +212,6 @@ export class ControlRenderer extends ScatterplotRenderer {
this.connectDots && this.generateLines(chartArea, data, x, y);
}

getAvgLeadTime() {
const filteredData = this.data.filter((d) => d.deliveredDate >= this.baselineStartDate && d.deliveredDate <= this.baselineEndDate);
return Math.ceil(filteredData.reduce((acc, curr) => acc + curr.leadTime, 0) / filteredData.length);
}

generateLines(chartArea, data, x, y) {
// Define the line generator
const line = d3
Expand All @@ -108,7 +232,6 @@ export class ControlRenderer extends ScatterplotRenderer {
}

updateGraph(domain) {
this.computeGraphLimits();
this.updateChartArea(domain);
if (this.connectDots) {
const line = d3
Expand All @@ -117,7 +240,15 @@ export class ControlRenderer extends ScatterplotRenderer {
.y((d) => this.applyYScale(this.currentYScale, d.leadTime));
this.chartArea.selectAll('.dot-line').attr('d', line);
}
this.drawGraphLimits(this.currentYScale);
this.drawLimits();
this.showActiveSignal();
this.displayObservationMarkers(this.observations);
}

cleanup() {
this.limitData = {};
this.processSignalsData = {};
this.visibleLimits = {};
this.activeProcessSignal = null;
}
}
Loading