Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
React VictoryPie - How to scale slice on mouse hover

I'm trying to scale a slice of this Victory Pie chart on mouse hover but I'm having a difficult time getting it to render correctly. Has anyone had any luck with this? I've tried adding things like transform: scale(1.2) to the mutation style but it moves the slice rather than scales (I believe this is because scale scales based on the point of (0,0)). I've also tried playing around with translate or matrix but I can't get it to work correctly. Here's my code:

        <div style={{ width: '150px' }}>
          <VictoryPie
            data={[
              { x: 'Cats', y: 35, label: 'Cats \n 35%' },
              { x: 'Dogs', y: 40, label: 'Dogs \n 40%' },
              { x: 'Birds', y: 55, label: 'Birds \n 55%' }
            ]}
            labelComponent={
              <VictoryTooltip
                cornerRadius={10}
                pointerWidth={30}
                pointerLength={20}
                flyoutPadding={{ top: 10, bottom: 10, left: 25, right: 25 }}
                style={{ fontSize: '36px', fill: '#FFF' }}
                flyoutStyle={{ fill: '#414B5F' }}
                orientation="right"
              />
            }
            colorScale={['tomato', 'orange', 'gold', 'cyan', 'navy']}
            innerRadius={100}
            events={[
              {
                target: 'data',
                eventHandlers: {
                  onMouseOver: () => {
                    return [
                      {
                        target: 'data',
                        mutation: () => ({ style: { fill: 'red', width: 30 } })
                      },
                      {
                        target: 'labels',
                        mutation: () => ({ active: true })
                      }
                    ]
                  },
                  onMouseOut: () => {
                    return [
                      {
                        target: 'data',
                        mutation: () => {}
                      },
                      {
                        target: 'labels',
                        mutation: () => ({ active: false })
                      }
                    ]
                  }
                }
              }
            ]}
          />
        </div>

I added a simple mutation that changes the slice to red on hover and that works great. I'm just having a difficult time finding the best way to make the slice scale up slightly without causing problems.

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use a custom component to render slices

const CustomSlice = (props) => {
  const [scale, setScale] = useState(1);

  // modified transformation from here
  // https://github.com/FormidableLabs/victory/blob/844109cfe4e40b23a4dcb565e551a5a98015d0c0/packages/victory-pie/src/slice.js#L74
  const transform = `translate(${props.origin.x}, ${props.origin.y}) scale(${scale})`;

  return (
    <Slice
      {...props}
      style={{ ...props.style }}
      events={{
        onMouseOver: (e) => {
          if (props.events.onMouseOver) {
            props.events.onMouseOver(e);
          }
          setScale((c) => c * 1.2);
        },
        onMouseOut: (e) => {
          if (props.events.onMouseOut) {
            props.events.onMouseOut(e);
          }
          setScale(1);
        }
      }}
      transform={transform}
    />
  );
};
<div style={{ width: "150px" }}>
  <VictoryPie
    dataComponent={<CustomSlice />}
    data={[
      { x: "Cats", y: 35, label: "Cats \n 35%" },
      { x: "Dogs", y: 40, label: "Dogs \n 40%" },
      { x: "Birds", y: 55, label: "Birds \n 55%" }
    ]}
    labelComponent={
      <VictoryTooltip
        cornerRadius={10}
        pointerWidth={30}
        pointerLength={20}
        flyoutPadding={{ top: 10, bottom: 10, left: 25, right: 25 }}
        style={{ fontSize: "36px", fill: "#FFF" }}
        flyoutStyle={{ fill: "#414B5F" }}
        orientation="right"
      />
    }
    colorScale={["tomato", "orange", "gold", "cyan", "navy"]}
    innerRadius={100}
    events={[
      {
        target: "data",
        eventHandlers: {
          onMouseOver: (e) => {
            return [
              {
                target: "data",
                mutation: () => ({
                  style: { fill: "red", width: 30 }
                })
              },
              {
                target: "labels",
                mutation: () => ({ active: true })
              }
            ];
          },
          onMouseOut: () => {
            return [
              {
                target: "data",
                mutation: () => {}
              },
              {
                target: "labels",
                mutation: () => ({ active: false })
              }
            ];
          }
        }
      }
    ]}
  />
</div>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda