Skip to content
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

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

Open
austinyearlykim opened this issue Sep 2, 2018 · 6 comments

Comments

@austinyearlykim
Copy link

austinyearlykim commented Sep 2, 2018

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

@andeemarks
Copy link

I'm seeing similar problems with this. From inspecting the generated <line...> code, it looks like the "value" for a custom SparklinesReferenceLine is being interpreted relative to the pixel dimensions of the containing svg (from top-left corner) rather than relative to the data being passed in via the data attribute

@Gajesh-LH
Copy link

+1

@ironwren
Copy link

I just ran into this today. Please Fix!

@joakimbengtson
Copy link

+1

@xadamxk
Copy link

xadamxk commented Jun 5, 2019

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

@junhyuk-prestolabs
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants