Skip to main content

Overview

The Pack class builds a circle packing diagram where hierarchical data is displayed using nested circles. The size of each circle represents its value.
import * as am5hierarchy from "@amcharts/amcharts5/hierarchy";

const pack = root.container.children.push(
  am5hierarchy.Pack.new(root, {
    nodePadding: 5
  })
);

Class Hierarchy

  • Series
    • Hierarchy
      • Pack

Settings

nodePadding

nodePadding
number
Gap between nodes in pixels.Since: 5.2.6
pack.set("nodePadding", 5);

Inherited Settings

Pack inherits all settings from Hierarchy, including:
  • sort - How to sort nodes by value (“ascending” | “descending” | “none”)
  • valueField - Field in data holding numeric value
  • categoryField - Field in data holding category name
  • childDataField - Field in data holding array of children
  • downDepth - Number of child levels to show when drilling down
  • upDepth - Number of parent levels to show from selected node
  • initialDepth - Number of levels to show on first load
  • topDepth - Starting level to show (hides upper levels)
  • singleBranchOnly - Collapse other branches when one expands
  • animationDuration - Duration for drill animations in milliseconds
  • animationEasing - Easing function for animations
  • colors - ColorSet for auto-assigning node colors
  • patterns - PatternSet for auto-assigning node patterns

Data Item Properties

Each data item in a Pack chart has the following properties:

children

children
Array<DataItem<IPackDataItem>>
An array of data items of node’s children.

parent

parent
DataItem<IPackDataItem>
A data item of node’s parent.

circle

circle
Circle
A Circle element of the node.

value

value
number
Value of the node as set in data.

sum

sum
number
Sum of child values.

valuePercentTotal

valuePercentTotal
number
Percent value of the node, based on total sum of all nodes in upper level.

valuePercent

valuePercent
number
Percent value of the node, based on the value of its direct parent.Since: 5.2.21

category

category
string
Category name of the node.

depth

depth
number
Node’s depth within the hierarchy (0 for root).

node

node
HierarchyNode
Reference to the visual HierarchyNode element.

label

label
Label
Reference to node’s Label element.

fill

fill
Color
Node’s auto-assigned color.

fillPattern

fillPattern
Pattern
Node’s auto-assigned pattern.Since: 5.10.0

disabled

disabled
boolean
Indicates if node is currently disabled (collapsed).

Data Format

interface IPackDataObject {
  name?: string;
  value?: number;
  children?: IPackDataObject[];
}

Example Data

pack.data.setAll([{
  name: "Root",
  children: [
    {
      name: "Group A",
      children: [
        { name: "A1", value: 100 },
        { name: "A2", value: 60 },
        { name: "A3", value: 40 }
      ]
    },
    {
      name: "Group B",
      children: [
        { name: "B1", value: 135 },
        { name: "B2", value: 98 }
      ]
    }
  ]
}]);

List Templates

Pack provides access to these list templates for customization:

nodes

nodes
ListTemplate<HierarchyNode>
List of all node container elements.
pack.nodes.template.setAll({
  toggleKey: "disabled"
});

circles

circles
ListTemplate<Circle>
List of node circle elements in the Pack chart.
pack.circles.template.setAll({
  strokeWidth: 2,
  stroke: am5.color(0xffffff),
  fillOpacity: 0.7
});

labels

labels
ListTemplate<Label>
List of label elements.
pack.labels.template.setAll({
  text: "{category}",
  fontSize: 12,
  centerX: am5.p50,
  centerY: am5.p50,
  oversizedBehavior: "hide"
});

Containers

nodesContainer

nodesContainer
Container
Container that holds all node elements.

Methods

selectDataItem()

selectDataItem(dataItem: DataItem<IPackDataItem>): void
Selects a specific data item, triggering drill-down animation.
dataItem
DataItem<IPackDataItem>
The data item to select.

getDataItemById()

getDataItemById(id: string): DataItem<IPackDataItem> | undefined
Looks up and returns a data item by its ID.
id
string
The ID to search for.
return
DataItem<IPackDataItem> | undefined
The found data item, or undefined if not found.

enableDataItem()

enableDataItem(
  dataItem: DataItem<IPackDataItem>,
  maxDepth?: number,
  depth?: number,
  duration?: number
): void
Enables a disabled (collapsed) data item.
dataItem
DataItem<IPackDataItem>
Target data item to enable.
maxDepth
number
Maximum depth to enable children to.
depth
number
Current depth (used internally for recursion).
duration
number
Animation duration in milliseconds.

disableDataItem()

disableDataItem(
  dataItem: DataItem<IPackDataItem>,
  duration?: number
): void
Disables (collapses) a data item.
dataItem
DataItem<IPackDataItem>
Target data item to disable.
duration
number
Animation duration in milliseconds.

addChildData()

addChildData(
  dataItem: DataItem<IPackDataItem>,
  data: Array<any>
): void
Adds children data to the target data item dynamically.
dataItem
DataItem<IPackDataItem>
Parent data item to add children to.
data
Array<any>
Array of child data objects.
Since: 5.4.5

hoverDataItem()

hoverDataItem(dataItem: DataItem<IPackDataItem>): void
Triggers hover state on a data item.
dataItem
DataItem<IPackDataItem>
Target data item.
Since: 5.0.7

unhoverDataItem()

unhoverDataItem(dataItem: DataItem<IPackDataItem>): void
Removes hover state from a data item.
dataItem
DataItem<IPackDataItem>
Target data item.
Since: 5.0.7

Events

dataitemselected

pack.events.on("dataitemselected", (ev) => {
  console.log("Selected:", ev.dataItem);
});
Fired when a data item is selected.
dataItem
DataItem<IPackDataItem> | undefined
The selected data item.

Private Settings

These are read-only private settings managed internally:

scaleR

scaleR
number
Current radius scale factor.

See Also

Build docs developers (and LLMs) love