Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-d3-treemap

jquintozamora1.5kBSD-3-Clause2.0.6TypeScript support: included

Integrating D3 Treemap with React

React, D3, Treemap, motion, animation, zoomable, zoom, clickable

readme

react-d3-treemap

TreeMap component built with D3 Treemap and React based on Mike Bostock´s Treemap.

npm version Code Climate PRs Welcome

React-d3-treemap-gif

DEMO

React D3 Treemap demo

Features

  • React for painting SVG
  • D3 for doing the maths calculations

Installation

Steps to use react-d3-treemap in your React project

1.Install from NPM

npm install --save react-d3-treemap

2. Import and use in your application

import TreeMap from "react-d3-treemap"
// Include its styles in you build process as well
import "react-d3-treemap/dist/react.d3.treemap.css"

3. Usage

interface TreeMapInPutData {
  id: number
  name: string
  value?: number
  children?: Array<TreeMapInPutData>
  className?: string
  link?: string
}

<TreeMap<TreeMapInPutData>
    id="myTreeMap"
    width={500}
    height={400}
    data={<my data matching TreeMapInputData interface>}
    valueUnit={"MB"}
/>

Props

  /**
   * TreeMap id, will be use for create a unique for each node
   */
  id: string;

  /**
   * TreeMap width
   */
  width: number;

  /**
   * TreeMap height
   */
  height: number;

  /**
   * TreeMap data. Normally should have at least name, value and children.
   *
   * Example:
   * interface TreeMapInPutData {
   *   id: number
   *   name: string
   *   value?: number
   *   children?: Array<TreeMapInPutData>
   *   className?: string
   *   link?: string
   * }
   */
  data: TreeMapInputData;

  /*
        Unit for values. For example MB
    */
  valueUnit?: string;

  /*
   * Format for the values
   * https://github.com/d3/d3-format#format
   */
  valueFormat?: string;

  /**
   * Hide breadcrumb.
   *
   * If you app doesn't use breadcrumb, you can pass down a ref
   * and use the methods: zoomOut, resetZoom
   */
  disableBreadcrumb?: boolean;

  /**
   * There are few color strategies for nodes:
   *    Depth: different color per depth
   *    Value: different color depends on how big / small is the value
   *    NumberOfChildren: different color depends on how many children node has
   *    OneEachChildren: one color per each top children, then range of colors from white to that one
   */
  colorModel?: ColorModel;

  /**
   * Don't show the value
   */
  hideValue?: boolean;

  /**
   * Overrides top div main class
   */
  className?: string;

  /**
   * Overrides svg class
   */
  svgClassName?: string;

  /**
   * Overrides node class
   */
  nodeClassName?: string;

  /**
   * Overrides breadcrumb class
   */
  breadCrumbClassName?: string;

  /**
   * Overrides svg style
   */
  svgStyle?: React.CSSProperties;

  /**
   * Overrides node style
   */
  nodeStyle?: React.CSSProperties;

  /**
   * Padding between nodes ( calculated by D3 )
   */
  paddingInner?: number;

  /**
   * Custom ScaleSequential from D3
   */
  customD3ColorScale?: ScaleSequential<string>;

  /**
   * Name for the property `name` included in data
   *
   * @default "name"
   */
  namePropInData?: string;

  /**
   * Name for the property `link` included in data
   *
   * @default "link"
   */
  linkPropInData?: string;

  /**
   * Name for the property `value` included in data
   *
   * @default "value"
   */
  valuePropInData?: string;

  /**
   * Name for the property `children` included in data
   *
   * @default "children"
   */
  childrenPropInData?: string;

  /**
   * Captures on zoom event
   */
  onZoom?: (
    currentNode: HierarchyRectangularNode<TreeMapInputData>
  ) => void;

  /**
   * Indicates where to place NumberOfChildren box
   *
   * @default NumberOfChildrenPlacement.BottomRight
   */
  numberOfChildrenPlacement: NumberOfChildrenPlacement;

  /**
   * Color for text and children counter when background is dark
   *
   * @default white
   */
  darkNodeTextColor?: string;

  /**
   * Color for node border when background is dark
   *
   * @default white
   */
  darkNodeBorderColor?: string;

  /**
   * Color for text and children counter when background is light
   *
   * @default black
   */
  lightNodeTextColor?: string;

  /**
   * Color for node border when background is dark
   *
   * @default black
   */
  lightNodeBorderColor?: string;

  /**
   * Override value text for node
   */
  valueFn?: (value: number) => string

  /**
   * Tooltip placement. If none is specified then is automatic depending on
   * the quadrant of the treeMap
   */
  tooltipPlacement?: TooltipPlacement;

  /**
   * Tooltip custom css class
   */
  tooltipClassName?: string;

  /**
   * Disable custom tooltip
   *
   * @default false
   */
  disableTooltip?: boolean

  /**
   * Tooltip offset X
   *
   * @default 0
   */
  tooltipOffsetX?: number;

  /**
   * Tooltip offset Y
   *
   * @default 0
   */
  tooltipOffsetY?: number;

  /**
   * Number of levels to display in TreeMap
   *
   * @default 1
   */
  levelsToDisplay?: number;

App sample

You can see an example of App here.

Data sample

You can see an example of data here.

TypeScript sample

I created a TypeScript consume sample for React D3 Treemap.

License

BSD 3-Clause License

Copyright (c) 2024, José Quinto All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

changelog

2.0.6 (2024-11-18)

  • Add onNodeClick event

2.0.5 (2024-11-17)

  • Update npm ignore file

2.0.4 (2024-11-17)

  • Add npm ignore file

2.0.3 (2024-11-17)

  • Package.json updates
  • Gif update

2.0.2 (2024-11-15)

  • Fix text issue

2.0.1 (2024-11-15)

  • Migrate State Componets to Functional Components
  • Breaking change: remove onTreeMapDidMount from api
  • Breaking change: replace last params for onZoom, before was breadcrumbItems and now is currentNode: HierarchyRectangularNode<TreeMapInputData>
  • fix zoom and padding issues

1.0.29 (2021-09-27)

  • Bump major versions for dependencies

1.0.28 (2021-08-03)

  • Configure breaking words RegExp via splitRegExp prop

1.0.27 (2021-03-31)

  • Solve issue breaking down word by using "-"
  • Expose paddingOuter
  • Refactor padding inner solving issue displaying level 4

1.0.26 (2021-03-26)

  • Add tooltipOffsetXand tooltipOffsetY
  • Add levelsToDisplay

1.0.25 (2021-03-25)

  • Remove temp tooltip string

1.0.24 (2021-03-25)

  • Hide text when node height < text height
  • Add custom Tooltip implementation and some props:

     /**
      * Tooltip placement. If none is specified then is automatic depending on
      * the quadrant of the treeMap
      */
      tooltipPlacement?: TooltipPlacement;
    
      /**
       * Tooltip custom css class
      */
      tooltipClassName?: string;
    
      /**
       * Disable custom tooltip
      *
      * @default false
      */
      disableTooltip?: boolean

1.0.23 (2021-03-24)

  • Remove isTimeFormat
  • Expose formatFn to overrides the rendering of value in nodes

1.0.22 (2021-03-23)

  • Solve wrapping text issue
  • Limit number of text rows to show depending on the hight of the node
  • Set ratio(1) for d3 layout
  • Add d3-time-format and isTimeFormat prop to enable it
  • Add props to configure dark and light colors for text and border
    darkNodeTextColor?: string;
    darkNodeBorderColor?: string;
    lightNodeTextColor?: string;
    lightNodeBorderColor?: string;

1.0.21 (2021-03-23)

  • Enable Tree Shaking and reduce bundle size
  • UNSAFE_componentWillReceiveProps warning

1.0.19 (2021-03-21)

  • Add onTreeMapDidMount
  • Add numberOfChildrenPlacement
  • Export NumberOfChildrenPlacement enum
  • Solve bug with paddingTop and paddingLeft
  • Implement wrap of characters and ellipsis when text doesn't fit in the node

1.0.18 (2021-03-20)

  • Add App Sample to Readme.md
  • Solve convert to RBG color issue
  • Solve domain issue with color
  • Add onZoom for listening to changes in the zoom
  • Enable fontSize, paddingTop and paddingLeft for Nodes

1.0.16 (2021-03-20)

  • Refactor CSS.

    • Not using css modules anymore, just normal css-loader config
    • className and styles can be override
  • Refactor Typings

    • Remove most of the cases we were using any
    • TreeMap can be typed
      • <TreeMap<TreeMapInPutData> ... />
    • Add typing example in App.tsx
  • New props:

    • Styling
      • className?: string;
      • svgClassName?: string;
      • nodeClassName?: string;
      • svgStyle?: React.CSSProperties;
      • nodeStyle?: React.CSSProperties;
      • paddingInner?: number;
    • Color
      • Expose color scheme supporting scaleSequential and d3-scale-chromatic
      • customD3ColorScale?: ScaleSequential<string>;
    • Input Data
      • namePropInData?: string;
      • linkPropInData?: string;
      • valuePropInData?: string;
      • childrenPropInData?: string;
  • Expose new methods: resetZoom, zoomOut and getZoomLevel

    • Can be called via ref:

      const treeMapRef: React.RefObject<TreeMap<TreeMapInPutData>>
      ;<TreeMap<TreeMapInPutData>
        ref={treeMapRef}
        //...
      />
      
      treeMapRef.current.resetZoom()
  • Refactor node count label in the corner to allow rounded corner

  • Disable animated as it was increasing bundle size significantly

  • Remove styled-components as it was was also increasing bundle size just for breadcrumb

  • use yarn

1.0.15

  • React for painting SVG
  • D3 for doing the maths calculations