1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-27 09:09:41 +02:00

Group similar time series chart functionality

This commit is contained in:
Jose Farias 2024-04-19 18:30:25 -06:00
parent f52b042253
commit 23e71f82c5

View file

@ -7,12 +7,12 @@ const CHART_TYPES = [ "mini", "full" ]
export default class extends Controller { export default class extends Controller {
static values = { series: Object, chartType: String } static values = { series: Object, chartType: String }
#_dataPoints = [] #_d3MainSvg = null
#_d3Svg = null
#_d3FullChartGroup = null #_d3FullChartGroup = null
#_d3Tooltip = null #d3Tooltip = null
#_d3InitialContainerWidth = 0 #dataPoints = []
#_d3InitialContainerHeight = 0 #d3InitialContainerWidth = 0
#d3InitialContainerHeight = 0
connect() { connect() {
this.#install() this.#install()
@ -23,14 +23,18 @@ export default class extends Controller {
document.removeEventListener("turbo:load", this.#reinstall) document.removeEventListener("turbo:load", this.#reinstall)
} }
#reinstall = () => { #reinstall = () => {
this.#teardown() this.#teardown()
this.#install() this.#install()
} }
#teardown() { #teardown() {
this.#_d3Svg = null this.#_d3MainSvg = null
this.#_dataPoints = [] this.#_d3FullChartGroup = null
this.#d3Tooltip = null
this.#dataPoints = []
this.#d3Container.selectAll("*").remove() this.#d3Container.selectAll("*").remove()
} }
@ -40,6 +44,7 @@ export default class extends Controller {
this.#draw() this.#draw()
} }
#normalizeDataPoints() { #normalizeDataPoints() {
this.#dataPoints = (this.seriesValue.values || []).map((d) => ({ this.#dataPoints = (this.seriesValue.values || []).map((d) => ({
...d, ...d,
@ -49,31 +54,34 @@ export default class extends Controller {
})) }))
} }
#rememberInitialContainerSize() { #rememberInitialContainerSize() {
this.#d3InitialContainerWidth = this.#d3Container.node().clientWidth this.#d3InitialContainerWidth = this.#d3Container.node().clientWidth
this.#d3InitialContainerHeight = this.#d3Container.node().clientHeight this.#d3InitialContainerHeight = this.#d3Container.node().clientHeight
} }
#draw() { #draw() {
if (this.#dataPoints.length < 2) { if (this.#dataPoints.length < 2) {
this.#drawEmpty() this.#drawEmpty()
} else if (this.#isFullChart) { } else if (this.#isFullChart) {
this.#drawFullChart() this.#drawFullChart()
} else { } else {
this.#drawLine() this.#drawTrendline()
} }
} }
#drawEmpty() {
this.#d3Svg.selectAll(".tick").remove()
this.#d3Svg.selectAll(".domain").remove()
this.#drawDashedLine() #drawEmpty() {
this.#drawCenteredCircle() this.#d3MainSvg.selectAll(".tick").remove()
this.#d3MainSvg.selectAll(".domain").remove()
this.#drawDashedLineEmptyState()
this.#drawCenteredCircleEmptyState()
} }
#drawDashedLine() { #drawDashedLineEmptyState() {
this.#d3Svg this.#d3MainSvg
.append("line") .append("line")
.attr("x1", this.#d3InitialContainerWidth / 2) .attr("x1", this.#d3InitialContainerWidth / 2)
.attr("y1", 0) .attr("y1", 0)
@ -83,8 +91,8 @@ export default class extends Controller {
.attr("stroke-dasharray", "4, 4") .attr("stroke-dasharray", "4, 4")
} }
#drawCenteredCircle() { #drawCenteredCircleEmptyState() {
this.#d3Svg this.#d3MainSvg
.append("circle") .append("circle")
.attr("cx", this.#d3InitialContainerWidth / 2) .attr("cx", this.#d3InitialContainerWidth / 2)
.attr("cy", this.d3InitialContainerHeight / 2) .attr("cy", this.d3InitialContainerHeight / 2)
@ -92,9 +100,10 @@ export default class extends Controller {
.style("fill", tailwindColors.gray[400]) .style("fill", tailwindColors.gray[400])
} }
#drawFullChart() { #drawFullChart() {
this.#drawXAxisLabels() this.#drawXAxisLabels()
this.#drawLine() this.#drawTrendline()
this.#drawTooltip() this.#drawTooltip()
this.#trackMouseForShowingTooltip() this.#trackMouseForShowingTooltip()
} }
@ -126,36 +135,13 @@ export default class extends Controller {
.attr("dy", "0em") .attr("dy", "0em")
} }
#dataTrendColor(data) { #drawTrendline() {
return {
up: tailwindColors.success,
down: tailwindColors.error,
flat: tailwindColors.gray[500],
}[data.trend.direction]
}
#currencyValue(data) {
return Intl.NumberFormat(undefined, {
style: "currency",
currency: data.currency || "USD",
}).format(data.value)
}
#currencyChange(data) {
return Intl.NumberFormat(undefined, {
style: "currency",
currency: data.currency || "USD",
signDisplay: "always",
}).format(data.trend.value.amount)
}
#drawLine() {
let container let container
if (this.#isFullChart) { if (this.#isFullChart) {
container = this.#d3FullChartGroup container = this.#d3FullChartGroup
} else { } else {
container = this.#d3Svg container = this.#d3MainSvg
} }
const line = container const line = container
@ -259,19 +245,6 @@ export default class extends Controller {
}) })
} }
#createD3Svg() {
return this.#d3Container
.append("svg")
.attr("width", this.#d3InitialContainerWidth)
.attr("height", this.#d3InitialContainerHeight)
.attr("viewBox", [ 0, 0, this.#d3InitialContainerWidth, this.#d3InitialContainerHeight ])
}
#createD3FullChartGroup() {
return this.#d3Svg
.append("g")
.attr("transform", `translate(${this.#margin.left},${this.#margin.top})`)
}
#tooltipTemplate(data) { #tooltipTemplate(data) {
return(` return(`
@ -287,24 +260,63 @@ export default class extends Controller {
`) `)
} }
get #isFullChart() { #dataTrendColor(data) {
return this.#chartType === "full" return {
up: tailwindColors.success,
down: tailwindColors.error,
flat: tailwindColors.gray[500],
}[data.trend.direction]
} }
get #margin() { #currencyValue(data) {
if (this.#isFullChart) { return Intl.NumberFormat(undefined, {
return { top: 20, right: 1, bottom: 30, left: 1 } style: "currency",
currency: data.currency || "USD",
}).format(data.value)
}
#currencyChange(data) {
return Intl.NumberFormat(undefined, {
style: "currency",
currency: data.currency || "USD",
signDisplay: "always",
}).format(data.trend.value.amount)
}
#createMainSvg() {
return this.#d3Container
.append("svg")
.attr("width", this.#d3InitialContainerWidth)
.attr("height", this.#d3InitialContainerHeight)
.attr("viewBox", [ 0, 0, this.#d3InitialContainerWidth, this.#d3InitialContainerHeight ])
}
#createFullChartGroup() {
return this.#d3MainSvg
.append("g")
.attr("transform", `translate(${this.#margin.left},${this.#margin.top})`)
}
get #d3MainSvg() {
if (this.#_d3MainSvg) {
return this.#_d3MainSvg
} else { } else {
return { top: 0, right: 0, bottom: 0, left: 0 } return this.#_d3MainSvg = this.#createMainSvg()
} }
} }
get #dataPoints() { get #d3FullChartGroup() {
return this.#_dataPoints if (this.#_d3FullChartGroup) {
return this.#_d3FullChartGroup
} else {
return this.#_d3FullChartGroup = this.#createFullChartGroup()
}
} }
set #dataPoints(dataPoints) { get #isFullChart() {
this.#_dataPoints = dataPoints return this.#chartType === "full"
} }
get #chartType() { get #chartType() {
@ -315,52 +327,20 @@ export default class extends Controller {
} }
} }
get #d3Svg() { get #margin() {
if (this.#_d3Svg) { if (this.#isFullChart) {
return this.#_d3Svg return { top: 20, right: 1, bottom: 30, left: 1 }
} else { } else {
return this.#_d3Svg = this.#createD3Svg() return { top: 0, right: 0, bottom: 0, left: 0 }
} }
} }
get #d3FullChartGroup() {
if (this.#_d3FullChartGroup) {
return this.#_d3FullChartGroup
} else {
return this.#_d3FullChartGroup = this.#createD3FullChartGroup()
}
}
get #d3InitialContainerWidth() {
return this.#_d3InitialContainerWidth
}
set #d3InitialContainerWidth(value) {
this.#_d3InitialContainerWidth = value
}
get #d3InitialContainerHeight() {
return this.#_d3InitialContainerHeight
}
set #d3InitialContainerHeight(value) {
this.#_d3InitialContainerHeight = value
}
get #d3Tooltip() {
return this.#_d3Tooltip
}
set #d3Tooltip(value) {
this.#_d3Tooltip = value
}
get #d3ContainerWidth() { get #d3ContainerWidth() {
return this.#d3InitialContainerWidth - this.#margin.left - this.#margin.right return this.#d3InitialContainerWidth - this.#margin.left - this.#margin.right
} }
get #d3ContainerHeight() { get #d3ContainerHeight() {
return this.#_d3InitialContainerHeight - this.#margin.top - this.#margin.bottom return this.#d3InitialContainerHeight - this.#margin.top - this.#margin.bottom
} }
get #d3Container() { get #d3Container() {