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

Totals for stacked bars #951

Closed
timothyarmes opened this issue May 11, 2020 · 16 comments
Closed

Totals for stacked bars #951

timothyarmes opened this issue May 11, 2020 · 16 comments
Labels

Comments

@timothyarmes
Copy link

It would be really useful to show the total at the top of each bar so that we can show a sales breakdown by category along with total earnings.

@alexiastandish
Copy link

@timothyarmes did you ever figure out a workaround for this? Currently I'm using a custom bar component but with that it no longer utilizes the built in tooltip functionality

@Joelelmiller
Copy link

Looking for a work around for this too. Would be a great addition.

@jeffodle
Copy link

Anyone find a solution to this?

@alexiastandish
Copy link

alexiastandish commented Aug 30, 2020

@Joelelmiller @jeffodle I did but it's pretty hacky...

layers={['grid', 'axes', 'bars', Line, 'markers', 'legends']}

I added a custom Line component and then I used the bar data that is passed to the custom component through nivo to render a custom bar with a element that shows the total of the bar. Custom component below...

const Line = ({ bars }) => {
  if (bars.length / 3 <= 14) {
    const barsList = [];
    bars.map((currBar) => {
      const key = currBar.key.slice(currBar.key.indexOf(".") + 1);
      const keyCheck = barsList.find((element) => element.key === key);
      const keyCheckIndex = barsList.findIndex(
        (element) => element.key === key
      );
      if (keyCheck) {
        barsList[keyCheckIndex]["bars"].push(currBar.data.id);
      } else {
        barsList.push({
          key: key,
          bars: [currBar.data.id],
        });
      }
    });
    return (
      <Fragment>
        {bars.map((bar) => {
          const barListKey = barsList.findIndex(
            (element) => element.key === bar.key.slice(bar.key.indexOf(".") + 1)
          );
          const lastBarInGroup =
            barsList[barListKey].bars[barsList[barListKey].bars.length - 1];
          if (bar.data.id === lastBarInGroup) {
            return (
              <svg
                key={bar.key}
                style={{
                  overflow: "visible",
                }}
              >
                <g>
                  {" "}
                  <rect
                    x={bar.x}
                    y={bar.y - 20}
                    width={bar.width}
                    height="14px"
                    fill="transparent"
                  />{" "}
                  <text
                    x={bar.x + bar.width / 2}
                    y={bar.y + 25 / 2 - 20}
                    style={{
                      fontSize: "12px",
                      textAlign: "center",
                    }}
                    dominantBaseline="middle"
                    textAnchor="middle"
                  >
                    {useNumberFormat(
                      bar.data.data.deeplink +
                        bar.data.data.direct +
                        bar.data.data.push
                    )}
                  </text>
                </g>
              </svg>
            );
          }
        })}
      </Fragment>
    );
  } else {
    return <div />;
  }
};

Like I said it's not a great and would love to see a cleaner solution. There's also some niche, random config in their specific to the graph I was building that can be ignored (ex: if bars.length / 3 <= 14)

@seppu
Copy link

seppu commented Sep 7, 2020

Hi @alexiastandish,

Have you tried to do this in typescript? I am getting an error while adding the following line
layers={['grid', 'axes', 'bars', Line, 'markers', 'legends']}

I tried to follow this codepen example

@stale
Copy link

stale bot commented Dec 6, 2020

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Dec 6, 2020
@stale
Copy link

stale bot commented Dec 13, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

@stale stale bot closed this as completed Dec 13, 2020
@Mulli
Copy link

Mulli commented Apr 19, 2022

This issue is not closed and much needed!

@ghost ghost mentioned this issue May 27, 2022
@Kryptesa
Copy link

Bump 🥺

@iampueroo
Copy link

bump!

@damianr13
Copy link

Bump

@ChuckJonas
Copy link

can this be reopened?

@harsh3401
Copy link

Bump !

@jnsoler12012
Copy link

jnsoler12012 commented Jan 22, 2024

This same question was asked recently too and they gave a pretty good solution

#2020

@elijahnikov
Copy link

For anyone trying to do this for <ResponsiveBarCanvas/> this is what helped me
#2020

@plouc
Copy link
Owner

plouc commented Mar 19, 2024

Implemented in #2525 thanks to @joaopedromatias.

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

No branches or pull requests