Skip to content

Commit

Permalink
Merge pull request #8 from nikolaydubina/fixes-1
Browse files Browse the repository at this point in the history
Some fixes
  • Loading branch information
nikolaydubina authored Dec 6, 2021
2 parents cb1c219 + 30240eb commit a274382
Show file tree
Hide file tree
Showing 20 changed files with 9,131 additions and 3,844 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ docs:
cat testdata/gapminder-2007-population-life.csv | ./treemap -w 1080 -h 360 > docs/gapminder-2007-population-life-1080x360.svg
cat testdata/gapminder-2007-population-life.csv | ./treemap -color none > docs/gapminder-2007-population-life-nocolor.svg
cat testdata/gapminder-2007-population-life.csv | ./treemap -color RedBlu > docs/gapminder-2007-population-life-RedBlu.svg
cat testdata/gapminder-2007-population-life.csv | ./treemap -impute-heat > docs/gapminder-2007-population-life-impute-heat.svg
cat testdata/gapminder-2007-population-life.csv | ./treemap -color balanced > docs/gapminder-2007-population-life-balanced.svg
cat testdata/find-src-go-dir.csv | ./treemap -h 4096 -w 4096 > docs/find-src-go-dir.svg

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ $ ... | treemap -w 1080 -h 1080 > out.svg
```
![example-square](./docs/gapminder-2007-population-life-1080x1080.svg)

Imputing heat
```bash
$ ... | treemap -impute-heat > out.svg
```
![example-narrow](./docs/gapminder-2007-population-life-impute-heat.svg)

Tree-Hue coloring when there is no heat
```
$ ... | treemap -color balanced > out.svg
Expand All @@ -51,6 +57,7 @@ Size and heat is optional.
* `Squarified` algorithm for treemap layout problem. This is very common algorithm used in Plotly and most of visualization packages. _"Squarified Treemaps", Mark Bruls, Kees Huizing, and Jarke J. van Wijk, 2000_
* `Tree-Hue Color` algorithm for generating colors for nodes in treemap. The idea is to represent hierarchical structure by recursively painting similar hue to subtrees. _Nikolay Dubina, 2021_


## Contributions

Welcomed!
Expand Down
47 changes: 43 additions & 4 deletions cmd/treemap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"flag"
"fmt"
"image/color"
"io"
"log"
"os"

"github.com/nikolaydubina/treemap"
"github.com/nikolaydubina/treemap/parser"
"github.com/nikolaydubina/treemap/render"
)
Expand All @@ -27,6 +29,8 @@ Africa/Benin,8078314,56
Command options:
`

var grey = color.RGBA{128, 128, 128, 255}

func main() {
var (
w float64
Expand All @@ -35,6 +39,8 @@ func main() {
paddingBox float64
padding float64
colorScheme string
colorBorder string
imputeHeat bool
)

flag.Usage = func() {
Expand All @@ -47,6 +53,8 @@ func main() {
flag.Float64Var(&paddingBox, "padding-box", 4, "padding between box border and content")
flag.Float64Var(&padding, "padding", 32, "padding around root content")
flag.StringVar(&colorScheme, "color", "balance", "color scheme (RdBu, balance, none)")
flag.StringVar(&colorBorder, "color-border", "auto", "color of borders (light, dark, auto)")
flag.BoolVar(&imputeHeat, "impute-heat", false, "impute heat for parents(weighted sum) and leafs(0.5)")
flag.Parse()

in, err := io.ReadAll(os.Stdin)
Expand All @@ -60,6 +68,14 @@ func main() {
log.Fatal(err)
}

sizeImputer := treemap.SumSizeImputer{EmptyLeafSize: 1}
sizeImputer.ImputeSize(*tree)

if imputeHeat {
heatImputer := treemap.WeightedHeatImputer{EmptyLeafHeat: 0.5}
heatImputer.ImputeHeat(*tree)
}

tree.NormalizeHeat()

var colorer render.Colorer
Expand All @@ -70,27 +86,50 @@ func main() {
Hues: map[string]float64{},
C: 0.5,
L: 0.5,
DeltaH: 15,
DeltaC: 0.1,
DeltaL: 0.4,
DeltaH: 10,
DeltaC: 0.3,
DeltaL: 0.1,
}

var borderColor color.Color
borderColor = color.White

switch {
case colorScheme == "none":
colorer = render.NoneColorer{}
borderColor = grey
case colorScheme == "balanced":
colorer = treeHueColorer
borderColor = color.White
case hasPalette && tree.HasHeat():
colorer = render.HeatColorer{Palette: palette}
if imputeHeat {
borderColor = color.White
} else {
borderColor = grey
}
case tree.HasHeat():
palette, _ := render.GetPalette("RdBu")
colorer = render.HeatColorer{Palette: palette}
if imputeHeat {
borderColor = color.White
} else {
borderColor = grey
}
default:
colorer = treeHueColorer
}

switch {
case colorBorder == "light":
borderColor = color.White
case colorBorder == "dark":
borderColor = grey
}

uiBuilder := render.UITreeMapBuilder{
Colorer: colorer,
Colorer: colorer,
BorderColor: borderColor,
}
spec := uiBuilder.NewUITreeMap(*tree, w, h, marginBox, paddingBox, padding)
renderer := render.SVGRenderer{}
Expand Down
Loading

0 comments on commit a274382

Please sign in to comment.