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

Replace chart types with optional elements

This commit is contained in:
Jose Farias 2024-04-20 12:26:59 -06:00
parent bdd7e82bae
commit f5c33c051e
3 changed files with 47 additions and 70 deletions

View file

@ -2,13 +2,16 @@ import { Controller } from "@hotwired/stimulus"
import tailwindColors from "@maybe/tailwindcolors" import tailwindColors from "@maybe/tailwindcolors"
import * as d3 from "d3" import * as d3 from "d3"
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,
useLabels: Boolean,
useTooltip: Boolean,
strokeWidth: { type: Number, default: 2 }
}
#_d3MainSvg = null #_d3MainSvg = null
#_d3FullChartGroup = null #_d3MainGroup = null
#d3Tooltip = null #d3Tooltip = null
#dataPoints = [] #dataPoints = []
#d3InitialContainerWidth = 0 #d3InitialContainerWidth = 0
@ -31,7 +34,7 @@ export default class extends Controller {
#teardown() { #teardown() {
this.#_d3MainSvg = null this.#_d3MainSvg = null
this.#_d3FullChartGroup = null this.#_d3MainGroup = null
this.#d3Tooltip = null this.#d3Tooltip = null
this.#dataPoints = [] this.#dataPoints = []
@ -64,10 +67,8 @@ export default class extends Controller {
#draw() { #draw() {
if (this.#dataPoints.length < 2) { if (this.#dataPoints.length < 2) {
this.#drawEmpty() this.#drawEmpty()
} else if (this.#isFullChart) {
this.#drawFullChart()
} else { } else {
this.#drawTrendline() this.#drawChart()
} }
} }
@ -101,16 +102,23 @@ export default class extends Controller {
} }
#drawFullChart() { #drawChart() {
this.#drawXAxisLabels()
this.#drawTrendline() this.#drawTrendline()
if (this.useLabelsValue) {
this.#drawXAxisLabels()
}
if (this.useTooltipValue) {
this.#drawTooltip() this.#drawTooltip()
this.#trackMouseForShowingTooltip() this.#trackMouseForShowingTooltip()
} }
}
#drawXAxisLabels() { #drawXAxisLabels() {
// Add ticks // Add ticks
this.#d3FullChartGroup.append("g") this.#d3MainGroup
.append("g")
.attr("transform", `translate(0,${this.#d3ContainerHeight})`) .attr("transform", `translate(0,${this.#d3ContainerHeight})`)
.call( .call(
d3 d3
@ -123,7 +131,7 @@ export default class extends Controller {
.remove() .remove()
// Style ticks // Style ticks
this.#d3FullChartGroup.selectAll(".tick text") this.#d3MainGroup.selectAll(".tick text")
.style("fill", tailwindColors.gray[500]) .style("fill", tailwindColors.gray[500])
.style("font-size", "12px") .style("font-size", "12px")
.style("font-weight", "500") .style("font-weight", "500")
@ -136,31 +144,15 @@ export default class extends Controller {
} }
#drawTrendline() { #drawTrendline() {
let container this.#d3MainGroup
if (this.#isFullChart) {
container = this.#d3FullChartGroup
} else {
container = this.#d3MainSvg
}
const line = container
.append("path") .append("path")
.datum(this.#dataPoints) .datum(this.#dataPoints)
.attr("fill", "none") .attr("fill", "none")
.attr("stroke", this.#trendColor) .attr("stroke", this.#trendColor)
.attr("d", this.#d3Line) .attr("d", this.#d3Line)
if (this.#isFullChart) {
line
.attr("stroke-linejoin", "round") .attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round") .attr("stroke-linecap", "round")
.attr("stroke-width", 1.5) .attr("stroke-width", this.strokeWidthValue)
.attr("class", "line-chart-path")
} else {
line
.attr("stroke-width", 2)
}
} }
#drawTooltip() { #drawTooltip() {
@ -180,7 +172,7 @@ export default class extends Controller {
#trackMouseForShowingTooltip() { #trackMouseForShowingTooltip() {
const bisectDate = d3.bisector(d => d.date).left const bisectDate = d3.bisector(d => d.date).left
this.#d3FullChartGroup.append("rect") this.#d3MainGroup.append("rect")
.attr("width", this.#d3ContainerWidth) .attr("width", this.#d3ContainerWidth)
.attr("height", this.#d3ContainerHeight) .attr("height", this.#d3ContainerHeight)
.attr("fill", "none") .attr("fill", "none")
@ -199,11 +191,11 @@ export default class extends Controller {
const d = xPos - this.#d3XScale(d0.date) > this.#d3XScale(d1.date) - xPos ? d1 : d0 const d = xPos - this.#d3XScale(d0.date) > this.#d3XScale(d1.date) - xPos ? d1 : d0
// Reset // Reset
this.#d3FullChartGroup.selectAll(".data-point-circle").remove() this.#d3MainGroup.selectAll(".data-point-circle").remove()
this.#d3FullChartGroup.selectAll(".guideline").remove() this.#d3MainGroup.selectAll(".guideline").remove()
// Big circle // Big circle
this.#d3FullChartGroup this.#d3MainGroup
.append("circle") .append("circle")
.attr("class", "data-point-circle") .attr("class", "data-point-circle")
.attr("cx", this.#d3XScale(d.date)) .attr("cx", this.#d3XScale(d.date))
@ -214,7 +206,7 @@ export default class extends Controller {
.attr("pointer-events", "none") .attr("pointer-events", "none")
// Small circle // Small circle
this.#d3FullChartGroup this.#d3MainGroup
.append("circle") .append("circle")
.attr("class", "data-point-circle") .attr("class", "data-point-circle")
.attr("cx", this.#d3XScale(d.date)) .attr("cx", this.#d3XScale(d.date))
@ -224,7 +216,7 @@ export default class extends Controller {
.attr("pointer-events", "none") .attr("pointer-events", "none")
// Guideline // Guideline
this.#d3FullChartGroup this.#d3MainGroup
.append("line") .append("line")
.attr("class", "guideline") .attr("class", "guideline")
.attr("x1", this.#d3XScale(d.date)) .attr("x1", this.#d3XScale(d.date))
@ -242,8 +234,8 @@ export default class extends Controller {
.style("top", event.pageY - 10 + "px") .style("top", event.pageY - 10 + "px")
}) })
.on("mouseout", () => { .on("mouseout", () => {
this.#d3FullChartGroup.selectAll(".guideline").remove() this.#d3MainGroup.selectAll(".guideline").remove()
this.#d3FullChartGroup.selectAll(".data-point-circle").remove() this.#d3MainGroup.selectAll(".data-point-circle").remove()
this.#d3Tooltip.style("opacity", 0) this.#d3Tooltip.style("opacity", 0)
}) })
} }
@ -331,28 +323,16 @@ export default class extends Controller {
} }
} }
get #d3FullChartGroup() { get #d3MainGroup() {
if (this.#_d3FullChartGroup) { if (this.#_d3MainGroup) {
return this.#_d3FullChartGroup return this.#_d3MainGroup
} else { } else {
return this.#_d3FullChartGroup = this.#createFullChartGroup() return this.#_d3MainGroup = this.#createFullChartGroup()
}
}
get #isFullChart() {
return this.#chartType === "full"
}
get #chartType() {
if (CHART_TYPES.includes(this.chartTypeValue)) {
return this.chartTypeValue
} else {
return "mini"
} }
} }
get #margin() { get #margin() {
if (this.#isFullChart) { if (this.useLabelsValue) {
return { top: 20, right: 1, bottom: 30, left: 1 } return { top: 20, right: 1, bottom: 30, left: 1 }
} else { } else {
return { top: 0, right: 0, bottom: 0, left: 0 } return { top: 0, right: 0, bottom: 0, left: 0 }
@ -404,17 +384,10 @@ export default class extends Controller {
} }
get #d3YScale() { get #d3YScale() {
let percentPadding const reductionPercent = this.useLabelsValue ? 0.15 : 0.05
if (this.#isFullChart) {
percentPadding = 0.15
} else {
percentPadding = 0.05
}
const dataMin = d3.min(this.#dataPoints, d => d.value) const dataMin = d3.min(this.#dataPoints, d => d.value)
const dataMax = d3.max(this.#dataPoints, d => d.value) const dataMax = d3.max(this.#dataPoints, d => d.value)
const padding = (dataMax - dataMin) * percentPadding const padding = (dataMax - dataMin) * reductionPercent
return d3 return d3
.scaleLinear() .scaleLinear()

View file

@ -5,7 +5,9 @@
class="w-full h-full" class="w-full h-full"
data-controller="time-series-chart" data-controller="time-series-chart"
data-time-series-chart-series-value="<%= series.to_json %>" data-time-series-chart-series-value="<%= series.to_json %>"
data-time-series-chart-chart-type-value="full"></div> data-time-series-chart-use-labels-value="true"
data-time-series-chart-use-tooltip-value="true"
data-time-series-chart-stroke-width-value="1.5"></div>
<% else %> <% else %>
<div class="w-full h-full flex items-center justify-center"> <div class="w-full h-full flex items-center justify-center">
<p class="text-gray-500">No data available for the selected period.</p> <p class="text-gray-500">No data available for the selected period.</p>

View file

@ -5,7 +5,9 @@
class="w-full h-full" class="w-full h-full"
data-controller="time-series-chart" data-controller="time-series-chart"
data-time-series-chart-series-value="<%= series.to_json %>" data-time-series-chart-series-value="<%= series.to_json %>"
data-time-series-chart-chart-type-value="full"></div> data-time-series-chart-use-labels-value="true"
data-time-series-chart-use-tooltip-value="true"
data-time-series-chart-stroke-width-value="1.5"></div>
<% else %> <% else %>
<div class="w-full h-full flex items-center justify-center"> <div class="w-full h-full flex items-center justify-center">
<p class="text-gray-500">No data available for the selected period.</p> <p class="text-gray-500">No data available for the selected period.</p>