-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat WIP: display lcms as a line spectrum
- Loading branch information
Lan Le
committed
Aug 20, 2024
1 parent
c978a2f
commit 9d316f7
Showing
7 changed files
with
295 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
/* eslint-disable prefer-object-spread, no-mixed-operators */ | ||
import { | ||
InitScale, InitAxisCall, InitTip, | ||
} from '../../helpers/init'; | ||
import { | ||
MountPath, MountGrid, MountAxis, MountAxisLabelX, MountAxisLabelY, | ||
MountClip, MountMainFrame, MountTags, | ||
} from '../../helpers/mount'; | ||
import MountBrush from '../../helpers/brush'; | ||
import { TfRescale, MountCompass } from '../../helpers/compass'; | ||
import { LIST_LAYOUT } from '../../constants/list_layout'; | ||
|
||
const d3 = require('d3'); | ||
|
||
class LineFocus { | ||
constructor(props) { | ||
const { | ||
W, H, clickUiTargetAct, selectUiSweepAct, scrollUiWheelAct, | ||
} = props; | ||
|
||
this.jcampIdx = 0; | ||
this.rootKlass = '.d3Line'; | ||
this.margin = { | ||
t: 5, | ||
b: 40, | ||
l: 60, | ||
r: 5, | ||
}; | ||
this.w = W - this.margin.l - this.margin.r; | ||
this.h = H - this.margin.t - this.margin.b; | ||
this.clickUiTargetAct = clickUiTargetAct; | ||
this.selectUiSweepAct = selectUiSweepAct; | ||
this.scrollUiWheelAct = scrollUiWheelAct; | ||
this.brush = d3.brush(); | ||
this.brushX = d3.brushX(); | ||
|
||
this.axis = null; | ||
this.path = null; | ||
this.grid = null; | ||
this.tags = null; | ||
this.data = []; | ||
this.tTrEndPts = null; | ||
this.root = null; | ||
this.svg = null; | ||
this.axisCall = InitAxisCall(5); | ||
this.pathCall = null; | ||
this.tip = null; | ||
this.factor = 0.125; | ||
this.currentExtent = null; | ||
this.shouldUpdate = {}; | ||
this.layout = LIST_LAYOUT.H1; | ||
|
||
this.getShouldUpdate = this.getShouldUpdate.bind(this); | ||
this.resetShouldUpdate = this.resetShouldUpdate.bind(this); | ||
this.setTip = this.setTip.bind(this); | ||
this.setDataParams = this.setDataParams.bind(this); | ||
this.create = this.create.bind(this); | ||
this.update = this.update.bind(this); | ||
this.setConfig = this.setConfig.bind(this); | ||
this.drawLine = this.drawLine.bind(this); | ||
this.drawGrid = this.drawGrid.bind(this); | ||
this.onClickTarget = this.onClickTarget.bind(this); | ||
this.isFirefox = typeof InstallTrigger !== 'undefined'; | ||
} | ||
|
||
getShouldUpdate() { | ||
const { | ||
prevXt, prevYt, prevLySt, | ||
prevTePt, prevData, | ||
} = this.shouldUpdate; | ||
const { xt, yt } = TfRescale(this); | ||
const sameXY = xt(1.1) === prevXt && prevYt === yt(1.1); | ||
const sameLySt = prevLySt === this.layout; | ||
const sameTePt = prevTePt === this.tTrEndPts.length; | ||
const sameData = prevData === this.data.length; | ||
this.shouldUpdate = Object.assign( | ||
{}, | ||
this.shouldUpdate, | ||
{ | ||
sameXY, sameLySt, // eslint-disable-line | ||
sameTePt, sameData, // eslint-disable-line | ||
}, | ||
); | ||
} | ||
|
||
resetShouldUpdate() { | ||
const { xt, yt } = TfRescale(this); | ||
const prevXt = xt(1.1); | ||
const prevYt = yt(1.1); | ||
const prevTePt = this.tTrEndPts.length; | ||
const prevData = this.data.length; | ||
const prevLySt = this.layout; | ||
this.shouldUpdate = Object.assign( | ||
{}, | ||
this.shouldUpdate, | ||
{ | ||
prevXt, prevYt, prevLySt, // eslint-disable-line | ||
prevTePt, prevData, // eslint-disable-line | ||
}, | ||
); | ||
} | ||
|
||
setTip() { | ||
this.tip = InitTip(); | ||
this.root.call(this.tip); | ||
} | ||
|
||
setDataParams(data, tTrEndPts, layout) { | ||
this.data = [...data]; | ||
this.tTrEndPts = tTrEndPts; | ||
this.layout = layout; | ||
} | ||
|
||
updatePathCall(xt, yt) { | ||
this.pathCall = d3.line() | ||
.x((d) => xt(d.x)) | ||
.y((d) => yt(d.y)); | ||
} | ||
|
||
setConfig(sweepExtentSt) { | ||
// Domain Calculate | ||
let { xExtent, yExtent } = sweepExtentSt || { xExtent: false, yExtent: false }; | ||
|
||
if (!xExtent || !yExtent) { | ||
const xes = d3.extent(this.data, (d) => d.x).sort((a, b) => a - b); | ||
xExtent = { xL: xes[0], xU: xes[1] }; | ||
const btm = d3.min(this.data, (d) => d.y); | ||
const top = d3.max(this.data, (d) => d.y); | ||
const height = top - btm; | ||
yExtent = { | ||
yL: (btm - this.factor * height), | ||
yU: (top + this.factor * height), | ||
}; | ||
} | ||
|
||
this.scales.x.domain([xExtent.xL, xExtent.xU]); | ||
this.scales.y.domain([yExtent.yL, yExtent.yU]); | ||
|
||
// rescale for zoom | ||
const { xt, yt } = TfRescale(this); | ||
|
||
// Axis Call | ||
this.axisCall.x.scale(xt); | ||
this.axisCall.y.scale(yt); | ||
|
||
this.currentExtent = { xExtent, yExtent }; | ||
} | ||
|
||
drawLine() { | ||
const { sameXY } = this.shouldUpdate; | ||
if (sameXY) return; | ||
|
||
const { xt, yt } = TfRescale(this); | ||
this.updatePathCall(xt, yt); | ||
this.path.attr('d', this.pathCall(this.data)); | ||
} | ||
|
||
drawGrid() { | ||
const { sameXY } = this.shouldUpdate; | ||
if (sameXY) return; | ||
|
||
this.grid.x.call(this.axisCall.x | ||
.tickSize(-this.h, 0, 0)) | ||
.selectAll('line') | ||
.attr('stroke', '#ddd') | ||
.attr('stroke-opacity', 0.6) | ||
.attr('fill', 'none'); | ||
this.grid.y.call(this.axisCall.y | ||
.tickSize(-this.w, 0, 0)) | ||
.selectAll('line') | ||
.attr('stroke', '#ddd') | ||
.attr('stroke-opacity', 0.6) | ||
.attr('fill', 'none'); | ||
} | ||
|
||
onClickTarget(event, data) { | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
const onPeak = true; | ||
this.clickUiTargetAct(data, onPeak); | ||
} | ||
|
||
create({ | ||
filterSeed, tTrEndPts, | ||
layoutSt, | ||
sweepExtentSt, isUiAddIntgSt, isUiNoBrushSt, | ||
}) { | ||
this.svg = d3.select('.d3Svg'); | ||
MountMainFrame(this, 'focus'); | ||
MountClip(this); | ||
|
||
this.root = d3.select(this.rootKlass).selectAll('.focus-main'); | ||
this.scales = InitScale(this, false); | ||
this.setTip(); | ||
this.setDataParams(filterSeed, tTrEndPts, layoutSt); | ||
MountCompass(this); | ||
|
||
this.axis = MountAxis(this); | ||
this.path = MountPath(this, 'steelblue'); | ||
this.grid = MountGrid(this); | ||
this.tags = MountTags(this); | ||
MountAxisLabelX(this); | ||
MountAxisLabelY(this); | ||
|
||
if (this.data && this.data.length > 0) { | ||
this.setConfig(sweepExtentSt); | ||
this.drawLine(); | ||
this.drawGrid(); | ||
} | ||
MountBrush(this, isUiAddIntgSt, isUiNoBrushSt); | ||
this.resetShouldUpdate(); | ||
} | ||
|
||
update({ | ||
filterSeed, tTrEndPts, | ||
layoutSt, | ||
sweepExtentSt, isUiAddIntgSt, isUiNoBrushSt, | ||
}) { | ||
this.root = d3.select(this.rootKlass).selectAll('.focus-main'); | ||
this.scales = InitScale(this, false); | ||
this.setDataParams(filterSeed, tTrEndPts, layoutSt); | ||
|
||
if (this.data && this.data.length > 0) { | ||
this.setConfig(sweepExtentSt); | ||
this.getShouldUpdate(); | ||
this.drawLine(); | ||
this.drawGrid(); | ||
} | ||
MountBrush(this, isUiAddIntgSt, isUiNoBrushSt); | ||
this.resetShouldUpdate(); | ||
} | ||
} | ||
|
||
export default LineFocus; |
Oops, something went wrong.