This post is part of a series on converting the official D3 examples to
Svelte .
D3 treemap written in Svelte
Tiling method
binary squarify slice-dice slice dice analytics animate data display flex physics query scale util vis
flare.vis.operator.layout.NodeLinkTreeLayout
12,870 Node Link Tree Layout 12,870 flare.vis.operator.layout.RadialTreeLayout
12,348 Radial Tree Layout 12,348 flare.vis.operator.layout.CirclePackingLayout
12,003 Circle Packing Layout 12,003 flare.vis.operator.layout.CircleLayout
9,317 Circle Layout 9,317 flare.vis.operator.layout.TreeMapLayout
9,191 Tree Map Layout 9,191 flare.vis.operator.layout.StackedAreaLayout
9,121 Stacked Area Layout 9,121 flare.vis.operator.layout.ForceDirectedLayout
8,411 Force Directed Layout 8,411 flare.vis.operator.layout.Layout
7,881 Layout 7,881 flare.vis.operator.layout.AxisLayout
6,725 Axis Layout 6,725 flare.vis.operator.layout.IcicleTreeLayout
4,864 Icicle Tree Layout 4,864 flare.vis.operator.layout.DendrogramLayout
4,853 Dendrogram Layout 4,853 flare.vis.operator.layout.BundledEdgeRouter
3,727 Bundled Edge Router 3,727 flare.vis.operator.layout.IndentedTreeLayout
3,174 Indented Tree Layout 3,174 flare.vis.operator.layout.PieLayout
2,728 Pie Layout 2,728 flare.vis.operator.layout.RandomLayout
870 Random Layout 870 flare.vis.operator.label.Labeler
9,956 Labeler 9,956 flare.vis.operator.label.RadialLabeler
3,899 Radial Labeler 3,899 flare.vis.operator.label.StackedAreaLabeler
3,202 Stacked Area Labeler 3,202 flare.vis.operator.encoder.PropertyEncoder
4,138 Property Encoder 4,138 flare.vis.operator.encoder.Encoder
4,060 Encoder 4,060 flare.vis.operator.encoder.ColorEncoder
3,179 Color Encoder 3,179 flare.vis.operator.encoder.SizeEncoder
1,830 Size Encoder 1,830 flare.vis.operator.encoder.ShapeEncoder
1,690 Shape Encoder 1,690 flare.vis.operator.distortion.Distortion
6,314 Distortion 6,314 flare.vis.operator.distortion.BifocalDistortion
4,461 Bifocal Distortion 4,461 flare.vis.operator.distortion.FisheyeDistortion
3,444 Fisheye Distortion 3,444 flare.vis.operator.filter.FisheyeTreeFilter
5,219 Fisheye Tree Filter 5,219 flare.vis.operator.filter.VisibilityFilter
3,509 Visibility Filter 3,509 flare.vis.operator.filter.GraphDistanceFilter
3,165 Graph Distance Filter 3,165 flare.vis.operator.OperatorList
5,248 Operator List 5,248 flare.vis.operator.OperatorSequence
4,190 Operator Sequence 4,190 flare.vis.operator.OperatorSwitch
2,581 Operator Switch 2,581 flare.vis.operator.Operator
2,490 Operator 2,490 flare.vis.operator.SortOperator
2,023 Sort Operator 2,023 flare.vis.operator.IOperator
1,286 I Operator 1,286 flare.vis.data.Data
20,544 Data 20,544 flare.vis.data.DataList
19,788 Data List 19,788 flare.vis.data.NodeSprite
19,382 Node Sprite 19,382 flare.vis.data.ScaleBinding
11,275 Scale Binding 11,275 flare.vis.data.DataSprite
10,349 Data Sprite 10,349 flare.vis.data.TreeBuilder
9,930 Tree Builder 9,930 flare.vis.data.render.EdgeRenderer
5,569 Edge Renderer 5,569 flare.vis.data.render.ShapeRenderer
2,247 Shape Renderer 2,247 flare.vis.data.render.ArrowType
698 Arrow Type 698 flare.vis.data.render.IRenderer
353 I Renderer 353 flare.vis.data.Tree
7,147 Tree 7,147 flare.vis.data.EdgeSprite
3,301 Edge Sprite 3,301 flare.vis.controls.TooltipControl
8,435 Tooltip Control 8,435 flare.vis.controls.SelectionControl
7,862 Selection Control 7,862 flare.vis.controls.PanZoomControl
5,222 Pan Zoom Control 5,222 flare.vis.controls.HoverControl
4,896 Hover Control 4,896 flare.vis.controls.ControlList
4,665 Control List 4,665 flare.vis.controls.ClickControl
3,824 Click Control 3,824 flare.vis.controls.ExpandControl
2,832 Expand Control 2,832 flare.vis.controls.DragControl
2,649 Drag Control 2,649 flare.vis.controls.AnchorControl
2,138 Anchor Control 2,138 flare.vis.controls.Control
1,353 Control 1,353 flare.vis.controls.IControl
763 I Control 763 flare.vis.legend.Legend
20,859 Legend 20,859 flare.vis.legend.LegendRange
10,530 Legend Range 10,530 flare.vis.legend.LegendItem
4,614 Legend Item 4,614 flare.vis.axis.Axis
24,593 Axis 24,593 flare.vis.axis.CartesianAxes
6,703 Cartesian Axes 6,703 flare.vis.axis.Axes
1,302 Axes 1,302 flare.vis.axis.AxisGridLine
652 Axis Grid Line 652 flare.vis.axis.AxisLabel
636 Axis Label 636 flare.vis.Visualization
16,540 Visualization 16,540 flare.vis.events.DataEvent
2,313 Data Event 2,313 flare.vis.events.SelectionEvent
1,880 Selection Event 1,880 flare.vis.events.TooltipEvent
1,701 Tooltip Event 1,701 flare.vis.events.VisualizationEvent
1,117 Visualization Event 1,117 flare.util.Strings
22,026 Strings 22,026 flare.util.Shapes
19,118 Shapes 19,118 flare.util.Maths
17,705 Maths 17,705 flare.util.Displays
12,555 Displays 12,555 flare.util.palette.ColorPalette
6,367 Color Palette 6,367 flare.util.palette.SizePalette
2,291 Size Palette 2,291 flare.util.palette.ShapePalette
2,059 Shape Palette 2,059 flare.util.palette.Palette
1,229 Palette 1,229 flare.util.Geometry
10,993 Geometry 10,993 flare.util.heap.FibonacciHeap
9,354 Fibonacci Heap 9,354 flare.util.heap.HeapNode
1,233 Heap Node 1,233 flare.util.Colors
10,001 Colors 10,001 flare.util.math.SparseMatrix
3,366 Sparse Matrix 3,366 flare.util.math.DenseMatrix
3,165 Dense Matrix 3,165 flare.util.math.IMatrix
2,815 I Matrix 2,815 flare.util.Arrays
8,258 Arrays 8,258 flare.util.Dates
8,217 Dates 8,217 flare.util.Sort
6,887 Sort 6,887 flare.util.Stats
6,557 Stats 6,557 flare.util.Property
5,559 Property 5,559 flare.util.Filter
2,324 Filter 2,324 flare.util.Orientation
1,486 Orientation 1,486 flare.util.IValueProxy
874 I Value Proxy 874 flare.util.IPredicate
383 I Predicate 383 flare.util.IEvaluable
335 I Evaluable 335 flare.animate.interpolate.Interpolator
8,746 Interpolator 8,746 flare.animate.interpolate.MatrixInterpolator
2,202 Matrix Interpolator 2,202 flare.animate.interpolate.ColorInterpolator
2,047 Color Interpolator 2,047 flare.animate.interpolate.RectangleInterpolator
2,042 Rectangle Interpolator 2,042 flare.animate.interpolate.ArrayInterpolator
1,983 Array Interpolator 1,983 flare.animate.interpolate.PointInterpolator
1,675 Point Interpolator 1,675 flare.animate.interpolate.ObjectInterpolator
1,629 Object Interpolator 1,629 flare.animate.interpolate.NumberInterpolator
1,382 Number Interpolator 1,382 flare.animate.interpolate.DateInterpolator
1,375 Date Interpolator 1,375 flare.animate.Transitioner
19,975 Transitioner 19,975 flare.animate.Easing
17,010 Easing 17,010 flare.animate.Transition
9,201 Transition 9,201 flare.animate.Tween
6,006 Tween 6,006 flare.animate.FunctionSequence
5,842 Function Sequence 5,842 flare.animate.Scheduler
5,593 Scheduler 5,593 flare.animate.Sequence
5,534 Sequence 5,534 flare.animate.Parallel
5,176 Parallel 5,176 flare.animate.TransitionEvent
1,116 Transition Event 1,116 flare.animate.ISchedulable
1,041 I Schedulable 1,041 flare.animate.Pause
449 Pause 449 flare.query.methods.range
772 range 772 flare.query.methods.iff
748 iff 748 flare.query.methods.gte
625 gte 625 flare.query.methods.lte
619 lte 619 flare.query.methods.gt
603 gt 603 flare.query.methods.mul
603 mul 603 flare.query.methods.sub
600 sub 600 flare.query.methods.neq
599 neq 599 flare.query.methods.lt
597 lt 597 flare.query.methods.div
595 div 595 flare.query.methods.eq
594 eq 594 flare.query.methods.add
593 add 593 flare.query.methods.mod
591 mod 591 flare.query.methods.isa
461 isa 461 flare.query.methods.fn
460 fn 460 flare.query.methods.not
386 not 386 flare.query.methods.stddev
363 stddev 363 flare.query.methods.xor
354 xor 354 flare.query.methods.variance
335 variance 335 flare.query.methods.and
330 and 330 flare.query.methods.or
323 or 323 flare.query.methods.orderby
307 orderby 307 flare.query.methods.update
307 update 307 flare.query.methods.where
299 where 299 flare.query.methods.select
296 select 296 flare.query.methods.distinct
292 distinct 292 flare.query.methods.average
287 average 287 flare.query.methods.max
283 max 283 flare.query.methods.min
283 min 283 flare.query.methods.sum
280 sum 280 flare.query.methods.count
277 count 277 flare.query.methods._
264 _ 264 flare.query.Query
13,896 Query 13,896 flare.query.Expression
5,130 Expression 5,130 flare.query.Comparison
5,103 Comparison 5,103 flare.query.DateUtil
4,141 Date Util 4,141 flare.query.StringUtil
4,130 String Util 4,130 flare.query.Arithmetic
3,891 Arithmetic 3,891 flare.query.Match
3,748 Match 3,748 flare.query.CompositeExpression
3,677 Composite Expression 3,677 flare.query.ExpressionIterator
3,617 Expression Iterator 3,617 flare.query.Fn
3,240 Fn 3,240 flare.query.BinaryExpression
2,893 Binary Expression 2,893 flare.query.If
2,732 If 2,732 flare.query.IsA
2,039 IsA 2,039 flare.query.Variance
1,876 Variance 1,876 flare.query.AggregateExpression
1,616 Aggregate Expression 1,616 flare.query.Range
1,594 Range 1,594 flare.query.Not
1,554 Not 1,554 flare.query.Literal
1,214 Literal 1,214 flare.query.Variable
1,124 Variable 1,124 flare.query.Xor
1,101 Xor 1,101 flare.query.And
1,027 And 1,027 flare.query.Or
970 Or 970 flare.query.Distinct
933 Distinct 933 flare.query.Average
891 Average 891 flare.query.Maximum
843 Maximum 843 flare.query.Minimum
843 Minimum 843 flare.query.Sum
791 Sum 791 flare.query.Count
781 Count 781 flare.analytics.graph.MaxFlowMinCut
7,840 Max Flow Min Cut 7,840 flare.analytics.graph.ShortestPaths
5,914 Shortest Paths 5,914 flare.analytics.graph.LinkDistance
5,731 Link Distance 5,731 flare.analytics.graph.BetweennessCentrality
3,534 Betweenness Centrality 3,534 flare.analytics.graph.SpanningTree
3,416 Spanning Tree 3,416 flare.analytics.cluster.HierarchicalCluster
6,714 Hierarchical Cluster 6,714 flare.analytics.cluster.AgglomerativeCluster
3,938 Agglomerative Cluster 3,938 flare.analytics.cluster.CommunityStructure
3,812 Community Structure 3,812 flare.analytics.cluster.MergeEdge
743 Merge Edge 743 flare.analytics.optimization.AspectRatioBanker
7,074 Aspect Ratio Banker 7,074 flare.scale.TimeScale
5,833 Time Scale 5,833 flare.scale.QuantitativeScale
4,839 Quantitative Scale 4,839 flare.scale.Scale
4,268 Scale 4,268 flare.scale.OrdinalScale
3,770 Ordinal Scale 3,770 flare.scale.LogScale
3,151 Log Scale 3,151 flare.scale.QuantileScale
2,435 Quantile Scale 2,435 flare.scale.IScaleMap
2,105 I Scale Map 2,105 flare.scale.ScaleType
1,821 Scale Type 1,821 flare.scale.RootScale
1,756 Root Scale 1,756 flare.scale.LinearScale
1,316 Linear Scale 1,316 flare.data.converters.GraphMLConverter
9,800 GraphML Converter 9,800 flare.data.converters.DelimitedTextConverter
4,294 Delimited Text Converter 4,294 flare.data.converters.JSONConverter
2,220 JSON Converter 2,220 flare.data.converters.IDataConverter
1,314 I Data Converter 1,314 flare.data.converters.Converters
721 Converters 721 flare.data.DataSource
3,331 Data Source 3,331 flare.data.DataUtil
3,322 Data Util 3,322 flare.data.DataSchema
2,165 Data Schema 2,165 flare.data.DataField
1,759 Data Field 1,759 flare.data.DataTable
772 Data Table 772 flare.data.DataSet
586 Data Set 586 flare.physics.NBodyForce
10,498 N Body Force 10,498 flare.physics.Simulation
9,983 Simulation 9,983 flare.physics.Particle
2,822 Particle 2,822 flare.physics.Spring
2,213 Spring 2,213 flare.physics.SpringForce
1,681 Spring Force 1,681 flare.physics.GravityForce
1,336 Gravity Force 1,336 flare.physics.DragForce
1,082 Drag Force 1,082 flare.physics.IForce
319 I Force 319 flare.display.TextSprite
10,066 Text Sprite 10,066 flare.display.DirtySprite
8,833 Dirty Sprite 8,833 flare.display.RectSprite
3,623 Rect Sprite 3,623 flare.display.LineSprite
1,732 Line Sprite 1,732 flare.flex.FlareVis
4,116 Flare Vis 4,116
Original Plot |
Svelte REPL |
Github
Code
<!-- Treemap.svelte -->
< script >
import * as d3 from "d3" ;
import Swatches from "./Swatches.svelte" ;
// Receive plot data as prop.
export let data;
// Specify the chart’s dimensions.
const width = 1154 ;
const height = 1154 ;
// Specify the color scale.
const color = d3. scaleOrdinal (
data.children. map (( d ) => d.name),
d3.schemeTableau10,
);
// Specify formatting function.
const format = d3. format ( ",d" );
// Specify initial tiling method.
let tile = d3.treemapBinary;
// Compute the layout.
$ : root = d3
. treemap ()
. tile (tile) // e.g., d3.treemapSquarify
. size ([width, height])
. padding ( 1 )
. round ( true )(
d3
. hierarchy (data)
. sum (( d ) => d.value)
. sort (( a , b ) => b.value - a.value),
);
// Helper function to recursively find the parent of a given leaf at depth 1.
function getFirstDepthParent ( leaf ) {
return leaf.depth > 1 ? getFirstDepthParent (leaf.parent) : leaf;
}
</ script >
<!-- Tiling method selection menu -->
< label >
Tiling method
< select bind : value ={tile} style = "margin-bottom: 16px; color: #374151;" >
< option value ={d3.treemapBinary} selected >binary</ option >
< option value ={d3.treemapSquarify}>squarify</ option >
< option value ={d3.treemapSliceDice}>slice-dice</ option >
< option value ={d3.treemapSlice}>slice</ option >
< option value ={d3.treemapDice}>dice</ option >
</ select >
</ label >
<!-- Legend -->
< Swatches { color } />
< svg
{ width }
{ height }
viewBox = "0 0 { width } { height }"
style = "max-width: 100%; height: auto;"
>
{# each root. leaves () as leaf, leafIndex}
<!-- Get nodes of current leaf -->
{@ const nodes = leaf.data.name
. split ( / (?= [A-Z][a-z] ) | \s + / g )
. concat ( format (leaf.value))}
<!-- Add a cell for each leaf of the hierarchy -->
< g transform = "translate({ leaf . x0 },{ leaf . y0 })" >
<!-- Add a tooltip -->
< title >
{ `${ leaf
. ancestors ()
. reverse ()
. map (( leaf ) => leaf . data . name )
. join ( "." ) } \n ${ format ( leaf . value ) }` }
</ title >
<!-- Add a color rectangle -->
< rect
id = "rect-{ leafIndex }"
fill ={ color ( getFirstDepthParent (leaf).data.name)}
fill-opacity ={ 0.8 }
width ={leaf.x1 - leaf.x0}
height ={leaf.y1 - leaf.y0}
/>
<!-- Add a clipPath to ensure text does not overflow -->
< clipPath id = "clip-{ leafIndex }" >
< use href = "#rect-{ leafIndex }" />
</ clipPath >
<!-- Add multiline text. The last line shows the value and has a specific formatting. -->
< text clip-path = "url(#clip-{ leafIndex })" >
{# each nodes as node, nodeIndex}
< tspan
x = " 3 "
y = "{ ( nodeIndex === nodes . length - 1 ) * 0.3 +
1.1 +
nodeIndex * 0.9 }em"
fill-opacity ={nodeIndex === nodes. length - 1 ? 0.7 : null }
>
{node}
</ tspan >
{/ each }
</ text >
</ g >
{/ each }
</ svg >
<!-- Swatches.svelte -->
< script >
export let color;
export let swatchWidth = 15 ;
export let swatchHeight = 15 ;
export let marginLeft = 0 ;
</ script >
< div
style = "
display: flex;
align-items: center;
min-height: 33px;
margin-left: { marginLeft }px;
font: 10px sans-serif;
"
>
{# each color. domain () as category}
< span
style = "
display: inline-flex;
align-items: center;
margin-right: 1em;
--color: { color ( category ) };
--width: { swatchWidth }px;
--height: { swatchHeight }px;
"
>
{category}
</ span >
{/ each }
</ div >
< style >
span ::before {
content : "" ;
width : var ( --width );
height : var ( --height );
margin-right : 0.5 em ;
background : var ( --color );
}
</ style >