codeagent/treemap
Composer 安装命令:
composer require codeagent/treemap
包简介
Builds an hierarchical structures (like file system) into treemap form
README 文档
README
Sometimes we need to convenient represent hierarchical data structures such as file system. One of the famous methods is a treemap. This php package provides you to build treemaps in formats of html, canvas and image from native php multiarrays. Special architecture of classes gives you an opportunity for customization at level of one node: node content, node color and other.
See demos.
More info about treemaps you may see in wiki.
Installation
Install the latest version via composer:
composer require codeagent\treemap
Also, it's necessary to plug in required css-styles to your page for correct display of html markup.
You can do that by copying treemap.css file to web-accessible folder of your
server. To do that, simply run vendor/bin/treemap command with target directory as argument. Then you need to include it into the head section of page via <link> tag,
for example <link rel="stylesheet" href="assets/treemap.css" />
Basic usage
Include composer’s autoloader and inject Treemap class from codeagent\treemap namespace into php script.
Map is waiting at the entrance your data as first argument, width and height as second and third arguments respectively.
To obtain the result of rendering (html markup, image), call render method of Presenter interface:
include("vendor/autoload.php"); use codeagent\treemap\Treemap; // your data in consistent format $data = [["value" => 2, "children" => [...]], ["value" => "4", "children" => [...]], [...], ...]; $presenter = Treemap::html($data, 1200, 800); echo $presenter->render();
By default, Treemap class considers, that "weight" non-negative value are located in the value attribute and the
"children" nodes addressed via correspond children key of node.
If it is not suitable for you, you can tell treemap which keys to pick explicitly:
$treemap = new Treemap($data, 1200, 800); $treemap->valueAttribute = "volume"; $treemap->childrenAttribute = "department";
And then draw treemap using appropriate Presenter:
$presenter = new HtmlPresenter($treemap); echo $presenter->render();
Not forget to inject presenter class to you script via use statement.
Advanced usage
Formats of representation
Of course, presentation of treemap does not limited only by html. Besides html there are classes for building images and canvas elements in this package.
include("vendor/autoload.php"); use codeagent\treemap\Treemap; use codeagent\treemap\presenter\HtmlPresenter; use codeagent\treemap\presenter\NestedHtmlPresenter; use codeagent\treemap\presenter\CanvasPresenter; use codeagent\treemap\presenter\ImagePresenter; $data = [...]; // your hierarhical data const WIDTH = 1200; const HEIGHT = 800; $treemap = new Treemap($data, WIDTH, HEIGHT); $html = (new HtmlPresenter($treemap))->render(); // same as $html = Treemap::html($data, WIDTH, HEIGHT)->render(); echo $html; $canvas = (new CanvasPresenter($treemap))->render(); // same as $canvas = Treemap::canvas($data, WIDTH, HEIGHT)->render(); echo $canvas; $image = (new ImagePresenter($treemap, "png"))->render(); // same as $image = Treemap::image($data, WIDTH, HEIGHT, "png")->render(); header("Content-Type: image/png"); echo $image;
ImagePresenter outputs image in form of raw data.
For this reason, pass appropriate content-type header with image data: header("Content-Type: image/png").
It is worth noting the NestedtmlPresenter. In fact, it is a collection of treemaps with different detalization degree,
which gives you conveniently navigate from one map to another via headers and breadcrumbs at top of presenter.
Custom node styles
Presenter interface gives an ability to adjust a single node rendering via a utility Nodeinfo class, for example:
use codeagent\treemap\Treemap; use codeagent\treemap\presenter\HtmlPresenter; use codeagent\treemap\presenter\NodeInfo; $presenter = Treemap::html($data, $width, $height) ->render(function(NodeIndo $node){ $data = $node->data(); $node->content()->html("<span>{$data['name']}</span>"); $node->background("calculated_color_here"); });
NodeInfo api provides an access to node information:
- background() - sets/gets background color of node
- content() - access to content of node (NodeContent)
- rectangle() - access to geometry (rectangle) of node
- level() - depth of node (0 is root Node)
- isLeaf() - whether node is leaf
- isRoot() - whether node is root
- id() - order between node siblings
- visible() - whether node is visible/not
- data() - node data
License
MIT
codeagent/treemap 适用场景与选型建议
codeagent/treemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 129 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tree」 「treemap」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codeagent/treemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codeagent/treemap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codeagent/treemap 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Sorted Collections for PHP >= 8.4
jsTree widget for yii2
Doctrine2 behavior traits - slowhop fork
A Laravel Nova card to show Treemap charts
A fast and dynamic Merkle tree implementation
The KaririCode DataStructure component offers advanced PHP data structures, including lists, stacks, queues, maps, and sets. It features efficient, strongly-typed, object-oriented implementations like ArrayList, LinkedList, BinaryHeap, and TreeMap.
统计信息
- 总下载量: 129
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-06-20