noximo / php-colored-ascii-linechart
在控制台、html或图像中显示漂亮的线形图
v1.1.4
2021-01-26 14:53 UTC
Requires
- php: >= 7.1
- ext-json: >=1.3.7
Requires (Dev)
- php-parallel-lint/php-parallel-lint: ^v1.0.0
- phpstan/phpstan: ^0.12
- phpstan/phpstan-deprecation-rules: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- roave/security-advisories: dev-master
- symplify/easy-coding-standard: ^6.0
This package is auto-updated.
Last update: 2024-09-26 22:36:01 UTC
README
在终端内创建漂亮的、多功能的ASCII线形图,使用 PHP
编写。
- 在单个图表中创建多条线,每条线都有自己的颜色,
- 在图表中使用点,
- 将图表缩放到所需的高度或让它自由伸缩,
- 根据上升趋势和下降趋势显示多色线条,
- 将图表打印为ASCII彩色文本、HTML片段或png图像,
- 使用简单的API,有助于动画序列的图表。
基于 kroitor/asciichart 构建
安装
$ composer require noximo/php-colored-ascii-linechart
使用方法
简单输出
$linechart = new Linechart(); echo $linechart->addMarkers([1,2,3,4,5,6])->addPoint(4, 2)->chart();
这将打印一个包含单个点的简单图表,使用默认颜色。
高级输出
$settings = new Settings(); // Note that any setting can be ommited. $settings ->setColorizer(new AsciiColorizer()) // Colorizer, choose between Ascii, HTML and image colorizers ->setFPS(24) // Control speed of chart::wait method ->setHeight(30) // Set fixed height of chart. chart will scale accordingly. If not set, height will be calculated based on highest and lowest numbers across all sets of markers. ->setPadding(5, ' ') // Set lenght of a padding and character used ->setOffset(10) // Offset left border ->setFormat( // Control how y axis labels will be printed out function ($x, Settings $settings) { $padding = $settings->getPadding(); $paddingLength = \strlen($padding); return substr($padding . round($x, 2), -$paddingLength); } ); $linechart = new Linechart(); $linechart->setSettings($settings); for ($y = 0; $y < 1200; $y++) { // Move sinusoid $lineA = []; $lineB = []; for ($i = $y; $i < $y + 120; $i++) { $lineA[] = 10 * sin($i * ((M_PI * 4) / 120)); // Draw sinusoid $lineB[] = 20 * sin($i * ((M_PI * 4) / 120)); // Draw sinusoid } $linechart->addMarkers( $lineA, // Chart data - note that any elements with non-integer keys will be discarded [AsciiColorizer::GREEN, AsciiColorizer::BOLD], // Default color of line. Can be ommited. You can combine mutliple color codes together. If you set up HTML colorizer, you can enter css styles instead of codes. See below [AsciiColorizer::RED, AsciiColorizer::BOLD] // Color of downtrend. Can be ommited, then default color will be used instead. ); // Pro-tip - combine color with bold style - it will pop-out nicely $linechart->addMarkers($lineB, [AsciiColorizer::CYAN]); // Add as many datasets as you want $linechart->addLine( // Add a guiding line - a zero line for example 0, // Alias y coordinate [AsciiColorizer::CYAN], // You can set color the same way as with markers Linechart::FULL_LINE // Choose between full line and dashed line ); $linechart->addPoint( 10, 15, [AsciiColorizer::LIGHT_BLUE], Linechart::CROSS // Point can be made more visible with crosslines. Default is Linechart::POINT ); $chart = $linechart->chart(); // Chart is an object with all data drawn. It can be simply echoed or we can leverage its methods for output control $chart->clearScreen(); // Clears already outputed charts $chart->print(); // Alias of echo $chart with fluent method call $chart->wait(); // Naive implementation of animation. It simply sleeps for n microseconds (defined by setFPS earlier). It does not take into account time spent on chart generation or on retrieving data $linechart->clearAllMarkers(); // Get rid of already processed chart data so they won't get printed again. }
HTML输出
$linechart = new Linechart(); $settings = new Settings(); // Settings are needed in this case $settings->setColorizer(new HTMLColorizer()); // Here you need to set up HTMLColorizer $lineA = []; for ($i = 0; $i < +120; $i++) { $lineA[] = 10 * sin($i * ((M_PI * 4) / 120)); } $linechart->addLine(0, ['color:white'], Linechart::FULL_LINE); // Use css styles instead of ascii color codes $linechart->addMarkers($lineA, ['color: green'], ['color: red']); $linechart->setSettings($settings); echo $linechart->chart();
图像输出
图像正在开发中。您可以在 /examples
文件夹中查找 beta 实现。
开发路径/待办事项
- 颜色化和打印器的重构
- 无论输出类型如何,单一的颜色化器,
- 图表类重写,以便 $chart->toHtml(), $chart->toPng(), $chart->toAscii() 等存在,
- 适当的图像支持。通过gif动画图像,
- 更好的定制(背景、边框),
- 带有标签的X轴。