Socket
Socket
Sign inDemoInstall

hook-use-d3

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hook-use-d3

Helper hook to render d3 inside jsx


Version published
Maintainers
1
Install size
1.99 kB
Created

Readme

Source

npm i hook-use-d3

https://www.npmjs.com/package/hook-use-d3

Simple ReactJs hook helper to render d3 inside JSX after JSX is done rendering DOM element.

import React from "react";
import * as d3 from "d3";
import useD3 from "hook-use-d3";

export default function SomeComponent() {
  const d3ref = useD3((d3Container, dom) => {
    const svg = d3Container
      .append("svg")
      .attr("width", dom.getBoundingClientRect().width)
      .attr("height", dom.getBoundingClientRect().height)
      .call(
        d3.zoom().on("zoom", function (event) {
          svg.attr("transform", event.transform);
        })
      )
      .append("g");

    const circle = svg
      .append("circle")
      .attr("cx", 50)
      .attr("cy", 50)
      .attr("r", 1)
      .attr("fill", "#4CAF50");

    function animate() {
      let radius = 5;
      let distance = 35;
      circle
        .transition()
        .duration(2000)
        .attrTween("r", () => d3.interpolate(radius, radius + distance))
        .on("end", () => {
          radius = circle.attr("r");
          distance = 35;

          circle
            .transition()
            .duration(2000)
            .attrTween("r", () => d3.interpolate(radius, radius - distance))
            .on("end", animate);
        });
    }

    animate();
  }, []);

  return <svg ref={d3ref} style={{height: 100, width: 100}}></svg>;
}

FAQs

Last updated on 03 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc