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.

MouseOver event for entire stacked bar, not each small rectangle in the StackedBarChart

See original GitHub issue

I need to be able to trigger a mouse event from the entire vertical bar of a Stacked Bar Chart.

Currently, if you add onMouseEnter on on <Bar ... it triggers when you mouse over the corresponding colour on each bar (which makes sense), but isn’t what I need.

When you add a <Tooltip ... it is able to trigger when on a single bar, which is what I need, but i don’t need the Recharts tooltip. I need to be able to call a function that triggers state within my React component. I’ve played around with trying to manipulate the Tooltip to do what I want, but I can’t send down the active state (and the label which i was planning on using to differentiate between which bar was being hovered over) unless I sent it through the Tooltips’s content prop as a new component. This doesn’t work because you end up leaving the original component and therefor don’t have access to its state.

My code:

        <BarChart
          width={205}
          height={275}
          barCategoryGap="25%"
          data={graphData}
          margin={{ top: 20, right: 15, left: 0, bottom: 5 }}
        >
          <XAxis
            dataKey="name"
            tickLine={false}
            tick={<XAxisTick />}
            stroke={color.grey400}
          />
          <Bar
            dataKey="project"
            stackId="a"
            fill={color.blueMedium}
            isAnimationActive={false}
            label={<BarLabel currency={currencySymbol} />}
            onMouseEnter={this.handleShowDivA}
            onMouseLeave={this.handleHideDivA}
          />
          <Bar
            dataKey="reccuring"
            stackId="a"
            fill={color.blueDark}
            isAnimationActive={false}
            onMouseEnter={this.handleShowDivB}
            onMouseLeave={this.handleHideDivB}
          />
        </BarChart>

What currently happens when using onMouseEnter etc:

current mouse events

What I need to happen:

desired mouse events

Does anyone know how to do this? Is it even possible?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Haraldsoncommented, Jan 21, 2021

The solution looks like this:

import React, { useState } from 'react'
import { BarChart, Bar, Rectangle } from 'recharts'

const BarChartContainer = ({ data, labels, colors, ...whatnot }) => {
    const [activeBarLabel, setActiveBarLabel] = useState(null)

    return (
        <BarChart data={data}>
            {labels.map((label, key, index) => (
                <Bar
                    dataKey={label}
                    stackId="x"
                    fill={colors[index]}
                    shape={props => {
                        const active = props.label === activeBarLabel

                        props = {
                            ...props,
                            width: active ? 50 : 40
                        }

                        return <Rectangle {...props} />
                    }}
                    onMouseEnter={({ label }) => setActiveBarLabel(label)}
                    onMouseLeave={() => setActiveBarLabel(null)}
                    key={label} />
            ))}
        </BarChart>
    )
}

export default BarChartContainer
4reactions
erosenbergcommented, Feb 26, 2019

Is there still no way to access a stack as a single element or component? Is there a way to get all of the data one would need from the props by each stack? That would be incredibly helpful if BarChart’s props for example contained an array of stacks with corresponding data/sums/colors/values, etc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

X Y Co-ordinates of Stacked Bar
I'm here creating a new rectangle on hover. I want the to be created on right side top of the hovered rectangle of...
Read more >
A Complete Guide to Stacked Bar Charts | Tutorial by Chartio
Stacked bar charts extend the standard bar chart by dividing each bar into multiple subcategories. Learn how to best use this chart type...
Read more >
Bar Charts | Google Developers
A stacked bar chart is a bar chart that places related values atop one another. If there are any negative values, they are...
Read more >
Solved: Stacked column chart hover on one column and show
Stacked column chart hover on one column and show all data in tooltip. Reply. Topic Options ... I just want to see date...
Read more >
Bar Chart / Bar Graph: Examples, Excel Steps & Stacked Graphs
2. Stacked Bar Chart · Want to show the total size of groups. · Are interested in showing how the proportions between groups...
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