Giter Club home page Giter Club logo

Comments (4)

NTC-THSTO avatar NTC-THSTO commented on June 13, 2024 1

I posted my answer also on github but will also post it here for future users looking for a similar impl:

Maybe there is an alternate way but this method seems to work fine for what I need, it allows you to customise the tooltip fully and map individual element props easily without having impact on what is rendered within xaxis, but its a lot more code than what I was initially intending and had to reapply initial css styling

<XAxis textAnchor="start" interval={0} dataKey="label" tick={<CustomizedAxisTick/>} />
<Tooltip wrapperStyle={{ zIndex: 1000 }} content={<CustomTooltip />}/>
import React from 'react';
import { StyledCustomTooltip, StyledTooltipPayload } from './styles';

export type CustomTooltipProps = {
  active?: any;
  payload?: any;
  label?: any;
};

export const CustomTooltip = ({ active, payload, label }: CustomTooltipProps) => {
  if (active && payload && payload.length) {
    const firstPayload = payload[0]; // Assuming payload is an array of objects
    const name = firstPayload?.payload?.name;

    return (
      <StyledCustomTooltip>
        <div>
          <p>{label}</p>
          {name && <div>Name: {name}</div>}
          {payload.map((pld, index) => (
            <StyledTooltipPayload key={index}>
              <div style={{ color: pld.fill }}>
                {pld.dataKey}: {pld.value}
              </div>
            </StyledTooltipPayload>
          ))}
        </div>
      </StyledCustomTooltip>
    );
  }

  return null;
};

image

from recharts.

ckifer avatar ckifer commented on June 13, 2024

Set name on your graphical item rather than label on Tooltip or name on XAxis

      <Tooltip />
      <Legend />
      <Bar dataKey="pv" barSize={20} fill="#8884d8" name="My Bar Chart" />

You can do this without a custom tooltip.

Result:
image

from recharts.

ckifer avatar ckifer commented on June 13, 2024

Its hard to tell if the above is what you actually mean or not:

If its not could you place your current code into a sandbox so its easier to help with?
You can use this to start
Edit tooltip-with-customized-content (forked)

from recharts.

ckifer avatar ckifer commented on June 13, 2024

Yep - ideally this could be done in less code but it is the way it is for now. Closing this

from recharts.

Related Issues (20)

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.