question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

passing in custom value to reference line is not working as expected

See original GitHub issue

I have a linear data set going from [1, …6]. Passing in 1 as a value to the SparklinesReferenceLine I would expect the reference line to be at the bottom of my screenshot below, but no matter what value I pass in, it seems to want to just stay around the 4,5,6 range.

const data = [1,2,3,4,5,6];
export default (props) => {
    return (
                <Sparklines data={data}>
                    <SparklinesLine />
                    <SparklinesReferenceLine type='custom' value={1} />
                </Sparklines>
    );
}
screen shot 2018-09-02 at 1 01 01 pm

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

3reactions
junhyuk-prestolabscommented, Feb 19, 2020

Hi, this my team’s work arounds.

extracted from original react-sparklines code.

import React, { memo } from 'react'
import {
  Sparklines,
  SparklinesLine,
  SparklinesReferenceLine,
  SparklinesProps,
  SparklinesLineProps
} from 'react-sparklines'

import { useMemo } from '@src/hooks'

const arrayMin = (data: number[]): number => Math.min.apply(Math, data)
const arrayMax = (data: number[]): number => Math.max.apply(Math, data)

interface FancySparklinesProps extends SparklinesProps {
  data: number[]
  referenceValue?: number
  lineStyle?: SparklinesLineProps
}

interface CalcRefValueProps {
  data: number[]
  height: number
  margin: number
  referenceValue?: number
}

const calcRefValue = ({ data, height, margin, referenceValue }: CalcRefValueProps) => {
  if (referenceValue === undefined) return
  const max = arrayMax(data)
  const min = arrayMin(data)
  const vfactor = (height - margin * 2) / (max - min || 2)

  return (max === min ? 1 : max - referenceValue) * vfactor + margin
}

const FancySparklines = (props: FancySparklinesProps): JSX.Element => {
  const { data, height = 1, margin = 0, referenceValue, lineStyle } = props

  const refValue = useMemo(() => {
    return calcRefValue({ data, height, margin, referenceValue })
  }, [data, height, margin, referenceValue])

  return (
    <Sparklines {...props}>
      <SparklinesLine {...lineStyle} />
      {referenceValue !== undefined && <SparklinesReferenceLine type="custom" value={refValue} />}
    </Sparklines>
  )
}

export default memo(FancySparklines)

the problem is 0 is just 0, not a SVG position.

0reactions
xadamxkcommented, Jun 5, 2019

Ran into the issue today. It seems the reference line defaults to max even when you specify a custom value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reference Lines, Bands, Distributions, and Boxes - Tableau
Tableau lets you add as many reference lines, bands, distributions, and box ... It is also useful when working with a calculation with...
Read more >
Solved: Reference line - JMP User Community
I have 4000 histograms & each one of them have different reference value. ... I've got a similar problem, I have a graph...
Read more >
Tableau Tip: Adding a moving reference line for “today” - VizWiz
Great tips! I'm using a zone graph and was wondering if there's a way to make the zone continue between the actual values...
Read more >
Recharts custom label - Stack Overflow
Expected to see the 'Label' text over of all bars. Update For example, if I have a chart in which multiple lines are...
Read more >
Einstein Analytics and Discovery Insights Specialist - Step 2
"Also, make it possible for the reference line value to filter by ... The problem vas the deploy model name this should follow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found