-
Notifications
You must be signed in to change notification settings - Fork 60
[IMP] chart: add zoom interactivity #6046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
132 changes: 132 additions & 0 deletions
132
src/components/figures/chart/chartJs/zoomable_chart/zoomable_chart_store.ts
This file contains hidden or 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,132 @@ | ||
import { Command, UID } from "../../../../.."; | ||
import { | ||
MOVING_AVERAGE_TREND_LINE_XAXIS_ID, | ||
TREND_LINE_XAXIS_ID, | ||
} from "../../../../../helpers/figures/charts/chart_common"; | ||
import { SpreadsheetStore } from "../../../../../stores"; | ||
|
||
const TREND_LINE_AXES_IDS = [TREND_LINE_XAXIS_ID, MOVING_AVERAGE_TREND_LINE_XAXIS_ID] as const; | ||
const ZOOMABLE_AXIS_IDS = ["x", ...TREND_LINE_AXES_IDS] as const; | ||
export type AxisId = (typeof ZOOMABLE_AXIS_IDS)[number]; | ||
export type AxesLimits = { | ||
[chartId: UID]: { [axisId in AxisId]?: { min: number; max: number } | undefined }; | ||
}; | ||
export class ZoomableChartStore extends SpreadsheetStore { | ||
mutators = [ | ||
"resetAxisLimits", | ||
"updateAxisLimits", | ||
"updateTrendLineConfiguration", | ||
"clearAxisLimits", | ||
] as const; | ||
|
||
originalAxisLimits: AxesLimits = {}; | ||
currentAxesLimits: AxesLimits = {}; | ||
private idConversion: Record<UID, Set<UID>> = {}; | ||
|
||
handle(cmd: Command) { | ||
switch (cmd.type) { | ||
case "DELETE_FIGURE": | ||
if (cmd.figureId && this.idConversion[cmd.figureId]) { | ||
for (const chartId of this.idConversion[cmd.figureId]) { | ||
delete this.originalAxisLimits[chartId]; | ||
delete this.currentAxesLimits[chartId]; | ||
} | ||
} | ||
break; | ||
case "UPDATE_CHART": | ||
const type = cmd.definition.type; | ||
const chartId = `${type}-${cmd.figureId}`; | ||
if (!this.idConversion[cmd.figureId]) { | ||
this.idConversion[cmd.figureId] = new Set<UID>(); | ||
} | ||
this.idConversion[cmd.figureId].add(chartId); | ||
if (!("zoomable" in cmd.definition && cmd.definition.zoomable)) { | ||
this.clearAxisLimits(chartId); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
clearAxisLimits(chartId: UID) { | ||
delete this.originalAxisLimits[chartId]; | ||
delete this.currentAxesLimits[chartId]; | ||
return "noStateChange"; | ||
} | ||
|
||
resetAxisLimits( | ||
chartId: UID, | ||
limits: { [key: string]: { min: number; max: number } | undefined } | undefined | ||
) { | ||
for (const axisId of ZOOMABLE_AXIS_IDS) { | ||
if (limits?.[axisId]) { | ||
if (!this.originalAxisLimits[chartId]?.[axisId]) { | ||
this.originalAxisLimits[chartId] = { | ||
...this.originalAxisLimits[chartId], | ||
[axisId]: {}, | ||
}; | ||
} | ||
this.originalAxisLimits[chartId][axisId]!["min"] = limits[axisId].min; | ||
this.originalAxisLimits[chartId][axisId]!["max"] = limits[axisId].max; | ||
} else { | ||
if (this.originalAxisLimits[chartId]?.[axisId]) { | ||
delete this.originalAxisLimits[chartId][axisId]; | ||
} | ||
} | ||
} | ||
return "noStateChange"; | ||
} | ||
|
||
updateAxisLimits(chartId: UID, limits?: { min: number; max: number } | undefined) { | ||
if (limits === undefined) { | ||
delete this.currentAxesLimits[chartId]; | ||
return "noStateChange"; | ||
} | ||
let { min, max } = limits; | ||
if (min > max) { | ||
[min, max] = [max, min]; | ||
} | ||
this.currentAxesLimits[chartId] = { x: { min, max } }; | ||
return "noStateChange"; | ||
} | ||
|
||
/* Update the trend line axis configuration based on the current axis limits. | ||
* This function calculates the new limits for the trend line axes based on the current x-axis | ||
* limits and the original limits of the trend line axes. | ||
* It assumes that the origininal trend line axes are linear transformations of the original x-axis | ||
* limits and applies the same transformation to the current x-axis limits to get the new limits | ||
* for the current trend line axes. | ||
*/ | ||
updateTrendLineConfiguration(chartId: UID) { | ||
LucasLefevre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!this.originalAxisLimits[chartId]) { | ||
return "noStateChange"; | ||
} | ||
const chartLimits = this.originalAxisLimits[chartId].x; | ||
if (chartLimits === undefined) { | ||
return "noStateChange"; | ||
} | ||
for (const axisId of TREND_LINE_AXES_IDS) { | ||
if (!this.originalAxisLimits[chartId][axisId]) { | ||
continue; | ||
} | ||
if (!this.currentAxesLimits[chartId]?.[axisId]) { | ||
this.currentAxesLimits[chartId] = { | ||
...this.currentAxesLimits[chartId], | ||
[axisId]: {}, | ||
}; | ||
} | ||
if (this.currentAxesLimits[chartId]?.x === undefined) { | ||
return "noStateChange"; | ||
} | ||
const realRange = chartLimits.max - chartLimits.min; | ||
const trendingLimits = this.originalAxisLimits[chartId][axisId]; | ||
const trendingRange = trendingLimits.max! - trendingLimits.min!; | ||
const slope = trendingRange / realRange; | ||
const intercept = trendingLimits.min! - chartLimits.min * slope; | ||
const newXMin = this.currentAxesLimits[chartId].x.min; | ||
const newXMax = this.currentAxesLimits[chartId].x.max; | ||
this.currentAxesLimits[chartId][axisId]!.min = newXMin * slope + intercept; | ||
this.currentAxesLimits[chartId][axisId]!.max = newXMax * slope + intercept; | ||
} | ||
return "noStateChange"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.