Code Monkey home page Code Monkey logo

Comments (5)

joshwcomeau avatar joshwcomeau commented on August 15, 2024 2

Gonna close this, as I don't foresee it being resolved any time soon.

As Sami suggests, the user-land solution is pretty simple:

data.length > 0
  ? <Trend data={data} />
  : <SomeFallbackComponent />

from react-trend.

samijaber avatar samijaber commented on August 15, 2024 1

Yeah, a flat line would indeed be inaccurate. The correct behaviour as far as I see it is to not render a graph at all if there's less than 2 points, without crashing/erroring. A fallback could be nice, although part of me feels that it's more the end-user's concern.

from react-trend.

markpinero avatar markpinero commented on August 15, 2024

Is this an intended use case for react-trend? I know it's typically used for line graphs. This is the source code of Trend.js on line 118-121 indicating why it won't render:

    // We need at least 2 points to draw a graph.
    if (!data || data.length < 2) {
      return null;
    }

from react-trend.

wopian avatar wopian commented on August 15, 2024

Is this an intended use case for react-trend?

Don't see why it wouldn't be, since I just need a spark line to show up/down trends. Would also be an issue depending on how logging is done for uptime of new servers/activity of new users.

It's more that it has a fairly destructive exit if length < 2


Work around is to just duplicate single values:

return <Trend data={props.data.length > 1 ? props.data : props.data.concat(props.data)}/>

from react-trend.

joshwcomeau avatar joshwcomeau commented on August 15, 2024

Ah, right. Yeah, this was an oversight; from a purely mathematical perspective, you can't have a line with only 1 point, so I assumed any attempt to draw a line with a single data point would be a mistake. Never considered data that resets at the start of a period.

That said, I almost feel like a flat line is a little misleading... It gives the impression that the data is flat, when in actuality the trend might be increasing or decreasing from the previous period.

At any rate, I certainly don't think it should crash; my proposed solution would be to just render nothing. We could even have a prop that takes an element to render when not enough data is available, so you could render a fallback:

<Trend
  data={...}
  fallback={<h1>🚫</h1>}
/>

@samijaber thoughts?

from react-trend.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.