Skip to content

Commit

Permalink
FSR-1294: Add logic to wrap text for the threshold label on the stati…
Browse files Browse the repository at this point in the history
…on chart
  • Loading branch information
Keyurx11 committed Sep 20, 2024
1 parent cc50515 commit f6a0006
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions server/src/js/components/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,18 +776,24 @@ function LineChart (containerId, stationId, data, options = {}) {
.attr('class', 'threshold__line')
.attr('aria-hidden', true)
.attr('x2', xScale(xExtent[1])).attr('y2', 0)
const label = thresholdContainer.append('g')
.attr('class', 'threshold-label')
// Construct label text and split into lines of up to 35 characters
const thresholdLabel = `${threshold.level}m ${threshold.name}`
const labelSegments = thresholdLabel.match(/.{1,35}(\s|$)/g).map(line => line.trim())
const label = thresholdContainer.append('g').attr('class', 'threshold-label')
const path = label.append('path')
.attr('aria-hidden', true)
.attr('class', 'threshold-label__bg')
const text = label.append('text')
.attr('class', 'threshold-label__text')
text.append('tspan').attr('font-size', 0).text('Threshold: ')
text.append('tspan').attr('x', 10).attr('y', 22).text(`${threshold.level}m ${threshold.name}`)
// Add each line of the split text (up to 35 characters per line) as separate tspans
labelSegments.forEach((line, i) => {
text.append('tspan').attr('x', 10).attr('y', (i + 1) * 22).text(line.trim())
})
const textWidth = Math.round(text.node().getBBox().width)
path.attr('d', `m-0.5,-0.5 l${textWidth + 20},0 l0,36 l-${((textWidth + 20) / 2) - 7.5},0 l-7.5,7.5 l-7.5,-7.5 l-${((textWidth + 20) / 2) - 7.5},0 l0,-36 l0,0`)
label.attr('transform', `translate(${Math.round(width / 2 - ((textWidth + 20) / 2))}, -46)`)
const textHeight = Math.round(text.node().getBBox().height)
path.attr('d', `m-0.5,-0.5 l${textWidth + 20},0 l0,${19 + textHeight} l-${((textWidth + 20) / 2) - 7.5},0 l-7.5,7.5 l-7.5,-7.5 l-${((textWidth + 20) / 2) - 7.5},0 l0,-${19 + textHeight} l0,0`)
label.attr('transform', `translate(${Math.round(width / 2 - ((textWidth + 20) / 2))}, -${29 + textHeight})`)
const remove = thresholdContainer.append('a')
.attr('role', 'button')
.attr('class', 'threshold__remove')
Expand Down

0 comments on commit f6a0006

Please sign in to comment.