react-sparklines icon indicating copy to clipboard operation
react-sparklines copied to clipboard

Tooltip

Open fermuch opened this issue 9 years ago • 28 comments

Is there any way to add a tooltip?

fermuch avatar Sep 28 '15 03:09 fermuch

Since this is a 'sparklines' component, I consider this is out of scope. You can always try adding it yourself, and ask me questions if you need help.

borisyankov avatar Sep 28 '15 04:09 borisyankov

I see. Then, how about passing the prop onMouseMove so we can do something like this:

<Sparklines data={[1, 4, 6, 1, 2, 4, 6]}>
  <text ref="tooltip" x="0" y="0" visibility="hidden">Tooltip</text>
  <SparklinesBars onMouseMove={this.showTooltip}/>
</Sparklines>

And onMouseMove is passed down to every ?

EDIT: wasn't rendering in the comment.

fermuch avatar Sep 28 '15 11:09 fermuch

+1

codemeasandwich avatar Dec 12 '15 16:12 codemeasandwich

+1

RusAlex avatar Dec 16 '15 05:12 RusAlex

+1

Losses avatar Feb 26 '16 09:02 Losses

+1

mmahalwy avatar Feb 28 '16 21:02 mmahalwy

+1

kib357 avatar Mar 01 '16 15:03 kib357

+1

macdja38 avatar Mar 09 '16 01:03 macdja38

Wasn't a huge fan of the feature, but since it is requested so much, I'll add this next.

borisyankov avatar Mar 13 '16 11:03 borisyankov

+1

nichliu avatar Mar 14 '16 02:03 nichliu

+1

amir5000 avatar Mar 28 '16 18:03 amir5000

@fermuch @borisyankov Is there code cooking for this or would you welcome a PR? :)

EDIT: first draft https://github.com/borisyankov/react-sparklines/pull/54

dvdplm avatar Apr 25 '16 20:04 dvdplm

@dvdplm Nothing cooking yet, feel free to do a PR 👍

borisyankov avatar Apr 26 '16 13:04 borisyankov

added mouseover feature in https://github.com/borisyankov/react-sparklines/commit/f1dbc9ea65b7a6c5653da997328ddc2b31e10850

gabrielcsapo avatar Jan 20 '17 19:01 gabrielcsapo

@gabrielcsapo thanks!

demo snip anyone ?

592da avatar Jan 30 '17 14:01 592da

@borisyankov do you mind publishing version with tooltip to https://www.npmjs.com/package/react-sparklines?

anton-fedorov avatar Jun 08 '17 20:06 anton-fedorov

Now i see that SparklinesLine accepts onMouseMove prop, but it never gets called. Do you have a working demo? There is nothing about it in docs

Fxlr8 avatar Aug 23 '17 16:08 Fxlr8

Was this feature added? If so, can you please provide an example of usage or documentation?

ghost avatar Dec 29 '17 14:12 ghost

^ @fermuch @borisyankov @gabrielcsapo ^

ghost avatar Dec 30 '17 02:12 ghost

<Sparklines data={[1, 4, 6, 1, 2, 4, 6]}>
  <text ref="tooltip" x="0" y="0" visibility="hidden">Tooltip</text>
  <SparklinesLine onMouseMove={this.showTooltip}/>
</Sparklines>

should work, what happens when this is run? @1eddy87

gabrielcsapo avatar Dec 31 '17 02:12 gabrielcsapo

tested it out using the code you provided and I don't anything showing up, no errors in the console. Just don't work.

Do I need to import something else?

This is what I'm using at the moment:

import { Sparklines, SparklinesLine, SparklinesSpots, SparklinesReferenceLine } from 'react-sparklines';

@gabrielcsapo

ghost avatar Jan 02 '18 23:01 ghost

<Sparklines data={[1, 4, 6, 1, 2, 4, 6]}>
  <text ref="tooltip" x="0" y="0" visibility="hidden">Tooltip</text>
  <SparklinesLine onMouseMove={this.showTooltip}/>
</Sparklines>

should work, what happens when this is run? @1eddy87

Hi gabrielcsapo, thank you for posting the snippet. May I ask if you have a demo snippet code to show how to define this.showTooltip?

cravo123 avatar Jan 08 '19 15:01 cravo123

FWIW:

  1. Related new ticket #92
  2. Fork that "seems" to be having tooltip https://github.com/vegetableman/react-sparklines (no demo)

rrjanbiah avatar Jan 10 '20 12:01 rrjanbiah

I have found a very hacky solution:

export function SparklineDemo() {
  const [displayTooltip, setDisplayTooltip] = useState(false);
  const [tooltipData, setTooltipData] = useState(null);
  const [eventPosition, setEventPosition] = useState(null);
  const [hoverPosition, setHoverPosition] = useState(null);

  const sparklineData = [0, 1, 2, 3, 4, 5, 6, 7, 8];

  // this is used to calculate a custom reference line point
  const max = Math.max.apply(Math, sparklineData);
  const min = 0;
  const margin = 2;
  const limit = 1;
  const width = 100;
  const height = 20;
  const referencePoint = 5; // draw the reference line a the y value for the datapoint 5
  const vfactor = (height - margin * 2) / (max - min || 2);
  const y = (max - referencePoint) * vfactor;

  return (
    <>
        <div
          onMouseMove={(event) => {
            if (displayTooltip && hoverPosition) {
              const horizontalDisplacement = Math.abs(event.pageY - hoverPosition.y);
              const verticalDisplacement = Math.abs(event.pageX - hoverPosition.x);
              // hide the tooltip if the cursor moved more than 10 px in any direction
              if (horizontalDisplacement > 10 || verticalDisplacement > 10) {
                setDisplayTooltip(false);
                setHoverPosition(null);
              }
            }

            setEventPosition({ x: event.pageX, y: event.pageY });
          }}
          onMouseLeave={() => {
            setDisplayTooltip(false);
            setHoverPosition(null);
          }}
          className="text-center"
        >
          {displayTooltip && (
            <div
              className={`${
                displayTooltip ? 'visible' : 'invisible'
              } absolute z-50 bg-white rounded border-gray-300 shadow-lg p-1`}
              style={{
                top: eventPosition?.y + 10 ?? 0,
                left: eventPosition?.x + 10 ?? 0,
              }}
            >
              {tooltipData.text}
            </div>
          )}
          <Sparklines
            data={sparklineData}
            width={width}
            height={height}
            min={min}
            max={max}
            margin={margin}
          >
            <SparklinesLine
              style={{ stroke: '#4a5568', fill: '#edf2f7', fillOpacity: 1 }}
              onMouseMove={(e, data, point) => {
                setDisplayTooltip(true);
                setHoverPosition(eventPosition);
                setTooltipData({
                  point: point,
                  text: data,
                });
              }}
            />
            <SparklinesReferenceLine type={SparklinesReferenceLineTypes.custom} value={y} />
          </Sparklines>
        </div>
      </div>
    </>
  );
}

The CSS classes are from Tailwindcss so you would neet to adopt some stuff like visible oder invisible. Also this is taken from a Typescript-based project and I didn't test the snippet, so there might be some bugs. The general idea should be clear however.

claaslange avatar Nov 14 '20 12:11 claaslange

Is that tooltip functionality implemented or not? Can't figure it out..

and1son avatar Feb 16 '21 12:02 and1son

Is the tooltip added or still not? If added how can we use it for sparklinebars

jawadakram20 avatar Jun 03 '21 11:06 jawadakram20

+1

khilnani avatar Jun 13 '21 14:06 khilnani

Any update on this request? Would love the feature myself <3

aymather avatar Aug 20 '21 05:08 aymather