Git Product home page Git Product logo

webtreemap's Introduction

webtreemap

New 2017-Oct-16: master is now webtreemap v2, a complete rewrite with bug fixes, more features, and a different (simpler) API. If you're looking for the old webtreemap, see the v1 branch.

A simple treemap implementation using web technologies (DOM nodes, CSS styling and transitions) rather than a big canvas/svg/plugin. It's usable as a library as part of a larger web app, but it also includes a command-line app that dumps a self-contained HTML file that displays a map.

Play with a demo.

Usage

Web

The data format is a tree of Node, where each node is an object in the shape described at the top of tree.ts.

<script src='webtreemap.js'></script>
<script>
// Container must have its own width/height.
const container = document.getElementById('myContainer');
// See typings for full API definition.
webtreemap.render(container, data, options);

Options

Option Type Default
padding [number, number, number, number] [14, 3, 3, 3]
lowerBound number 0.1
applyMutations (node: Node) => void () => void
caption (node: Node) => string (node) => node.id
showNode (node: Node, width: number, height: number) => boolean (_, width, height) => (width > 20) && (height >= options.padding[0])
showChildren (node: Node, width: number, height: number) => boolean (_, width, height) => (width > 40) && (height > 40)
Option Description
padding In order: padding-top, padding-right, padding-bottom, padding-left of each node
lowerBound Lower bound of ratio that determines how many children can be displayed inside of a node. Example with a lower bound of 0.1: the total area taken up by displaying child nodes of any given node cannot be less than 10% of the area of its parent node.
applyMutations A function that exposes a node as an argument after it's dom element has been assigned. Use this to add inline styles and classes. Example: (node) => { node.dom.style.color = 'blue' }
caption A function that takes a node as an argument and returns a string that is used to display as the caption for the node passed in.
showNode A function that takes a node, its width, and its height, and returns a boolean that determines if that node should be displayed. Fires after showChildren.
showChildren A function that takes a node, its width, and its height, and returns a boolean that determines if that node's children should be displayed. Fires before showNode.

Command line

Install with

$ npm i webtreemap

Then run with:

$ webtreemap -o output_file < my_data

Input data format is space-separated lines of "size path", where size is a number and path is a '/'-delimited path. For example:

$ cat my_data
100 all
50 all/thing1
25 all/thing2

This is exactly the output produced by du, so this works:

$ du -ab some_path | webtreemap -o out.html

But note that there's nothing file-system-specific about the data format -- it just uses slash as a nesting delimiter.

Development

The modules of webtreemap can be used both from the web and from the command line, so the build has two layers. The command line app embeds the output of the build into its output so it's a bit confusing.

To build everything, run yarn run build.

Build layout

To hack on webtreemap, the pieces of the build are:

  1. yarn run tsc builds all the .ts files;
  2. yarn run webpack builds the UMD web version from JS of the above.

Because command line embeds the web version in its output, you need to run step 2 before running the output of step 1. Also note we intentionally don't use webpack's ts-loader because we want the TypeScript output for the command-line app.

Command line app

Use yarn run tsc -w to keep the npm-compatible JS up to date, then run e.g.:

$ du -ab node_modules/ | node build/src/cli.js --title 'node_modules usage' -o demo.html

License

webtreemap is licensed under the Apache License v2.0. See LICENSE.txt for the complete license text.

webtreemap's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webtreemap's Issues

cli.js seem to assume a ../dist path exists

%du -a /Users/eseidel/Downloads/flutter  | node cli.js -o out.html    [/src/webtreemap]

{ Error: ENOENT: no such file or directory, open '/src/webtreemap/../dist/webtreemap.js'
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/src/webtreemap/../dist/webtreemap.js' }

du output (on mac) can contain spaces which causes a spurious duplicate path

The output of du can contain spaces which can cause a spurious duplicate path.

Consider the following output of du in some file output.txt:

1	./a a
2	./a b
3	.

(This can be created by mkdir "a a" and mkdir "a b" and putting some content in a a and a b directory. Note that leaving directories empty doesn't reproduce as it is special case with size 0 and the webtreemap, probably erroneously, doesn't detect the duplication).

Run webtreemap < output.txt

Expected: Valid tree map emitted as an html file.
Received:

Error: duplicate path ./a 1
    at _loop_1 (webtreemap/build/tree.js:20:27)
    at Object.treeify (webtreemap/build/tree.js:27:13)
    at treeFromLines (webtreemap/build/cli.js:83:21)
    at webtreemap/build/cli.js:128:31
    at step (webtreemap/build/cli.js:33:23)
    at Object.next (webtreemap/build/cli.js:14:53)
    at fulfilled (webtreemap/build/cli.js:5:58)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

package.json in published package is different

package.json in the published package is different from one at master branch

{
  "author": {
    "name": "Evan Martin",
    "email": "[email protected]"
  },
  "bin": {
    "webtreemap": "build/cli.js"
  },
  "bugs": {
    "url": "https://github.com/evmar/webtreemap/issues"
  },
  "bundleDependencies": false,
  "dependencies": {
    "commander": "^2.11.0"
  },
  "deprecated": false,
  "description": "treemap visualization",
  "devDependencies": {
    "@types/commander": "^2.11.0",
    "@types/node": "^8.0.34",
    "prettier": "^1.7.0",
    "ts-loader": "^2.3.7",
    "typescript": "^2.5.2",
    "webpack": "^3.7.1"
  },
  "files": [
    "build/*.js",
    "build/*.d.ts",
    "dist/webtreemap.js"
  ],
  "homepage": "https://github.com/evmar/webtreemap#readme",
  "keywords": [
    "treemap",
    "visualization"
  ],
  "license": "Apache-2.0",
  "main": "treemap.js",
  "name": "webtreemap",
  "prettier": {
    "singleQuote": true,
    "trailingComma": "es5",
    "bracketSpacing": false,
    "parser": "typescript"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/evmar/webtreemap.git"
  },
  "scripts": {
    "build": "webpack --optimize-minimize",
    "demo": "du -ab node_modules/ | node build/cli.js --title 'node_modules for webtreemap' > docs/demo.html",
    "dev": "webpack --watch",
    "fmt": "prettier --write *.ts demo/*.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "version": "2.0.1"
}

Publish to npm

Could you publish this to npm so it's easier to use as a dependency?

Willing to create a PR with a package.json if you're up for it.

Thanks!

Colors

๐Ÿ‘ love it, fast and best.

1 how can I style colors?
2 will it work inside bootstrap without conflict
3 onMousehover is there a way to draw lines from adjoining boxes to show relationships and connections, I can email, pictures it to you.

Give webtreemap ownership away

I realistically don't have time to maintain this project, so I'd like to responsibly drop it.

Some options are:

  1. find a new maintainer
  2. bless a fork (roughly the same idea as #1)
  3. document enough of the algorithm so that people can write their own

Any volunteers are welcome.

Upstreaming various changes from fork

Hi Evan!

The Lighthouse team forked this repo: https://github.com/paulirish/webtreemap-cdt

We will be using webtreemap for a new feature in Lighthouse, @paulirish is also working on a new feature in Chrome DevTools that uses it too.

We made a few changes in that fork:

a) webpack -> rollup (not necessary to upstream imo)
b) add option for not injecting CSS with JS
c) split out a public layout() method
d) a11y: keyboard navigation
e) options.spacing

Would you be open to receiving some PRs for upstreaming these changes?

The `join` is not actually used.

webtreemap/src/tree.ts

Lines 68 to 82 in 5dbfbff

export function flatten(
n: Node,
join = (parent: string, child: string) => `${parent}/${child}`
) {
if (n.children) {
for (const c of n.children) {
flatten(c, join);
}
if (n.children.length === 1) {
const child = n.children[0];
n.id += '/' + child.id;
n.children = child.children;
}
}
}

The join function is not used though.

I think it's possible to modify the line 78. However the id property mismatch the join function type.

Maybe you can give me a hint.

Thanks.

stuff isn't escaped right

If you use this json file:

var kTree = {
  "data": {
    "$area": 89118673, 
    "$dominant_symbol": ""
  }, 
  "name": "[everything] 89.1m", 
  "children": [
    {
      "data": {
        "$area": 19992565, 
        "$dominant_symbol": "code"
      }, 
      "name": "[path] 20.0m", 
      "children": [
        {
          "data": {
            "$area": 43, 
            "$symbol": "code"
          }, 
          "name": "\" <meta http-equiv='refresh' content='0;%s'>\" 43"
        }, 
      ]
    }
  ]
}

then the treemap will be replaced with a useless page ("%s") once it's done loading. (Happens when using it with chromium.)

Node corruption while zooming out

I found the following bug that can be observed at the http://evmar.github.io/webtreemap/ page. It irrecoverably deletes nodes while zooming out.

To reproduce we need to make three clicks on the page.

  1. Click on the uglify_js/node_modules square.
    Now we can see CHANGELOG.md, README.md and other files.
  2. Click on the most outer block node_modules (50.0m)
  3. Click again on the uglify_js/node_modules and there are no files that we have seen at the point 1.

I think the problem might be in this loop:

for (; i < children.length; i++) {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.