comecc / c-pchart
将 "pChart" 库移植到 PHP 5
Requires
- php: >=5.5
- ext-gd: *
Requires (Dev)
- behat/behat: ~3.0
- behat/mink: ~1.7
- behat/mink-goutte-driver: ~1.2
- bossa/phpspec2-expect: ~1.0
- sensiolabs/behat-page-object-extension: ~2.0@dev
- squizlabs/php_codesniffer: ~2.3
This package is not auto-updated.
Last update: 2024-09-14 19:36:59 UTC
README
关于
该项目将 Composer 支持和一些基本的 PHP 5 标准引入到 pChart 2.0 库中。目标是允许 pChart 与现代框架(如 Symfony2)集成。
这是 2.0 版本,旨在进一步更新代码,但尽可能不改变功能。它将引入一些轻微的向后兼容性中断,所以如果这成为问题,请使用 1.* 版本。
已执行的操作
-
更新支持的 PHP 版本到 5.5。
-
完全移植了库的功能。
-
为所有类定义并添加了命名空间。
-
将所有
exit()
/die()
命令替换为throw
语句以允许一定程度上的错误控制。 -
重构代码以符合 PSR-2 标准,并添加了注释(尽可能确定)。同时,在可能的方法中添加了类型提示,因此可能会发生一些向后兼容性中断。
-
添加了一个用于加载类的工厂服务。
-
将所有常量移动到单个文件
src/Resources/data/constants.php
中。此文件是库运行所必需的,现在通过 Composer 加载。
许可证
之前声明该软件包采用 MIT 许可证,但这不符合原作者设定的要求。现在它采用 GNU GPL v3 许可证,因此如果您想在商业项目中使用它,您需要支付 适当的费用。
贡献
如果您希望为 1.*
版本做出贡献,有一个名为 legacy
的分支,您可以提交 pull requests。否则,您可以自由使用 master
分支。
安装(通过 Composer)
对于 composer 安装,将以下内容添加到您的 composer.json 文件中,并更新您的依赖项。或者您可以在您的项目目录中运行以下命令,其中包含 composer.json 文件。
"require": { "szymach/c-pchart": "~2.0@dev" },
to your composer.json file and update your dependencies. Or you can run
$ composer require szymach/c-pchart
in your project directory, where the composer.json file is.
之后,所有类都在 CpChart\Chart
命名空间下或 CpChart\Factory
下可用,用于工厂。
用法
现在您可以通过它们的命名空间自动加载或使用这些类。如果您愿意,您可以使用提供的工厂类。以下是一些使用库的示例,图表本身来自官方文档。
仅通过 Image 类绘制图表
并非所有图表都需要通过单独的类(例如条形图或样条图)创建,一些通过 Image 类创建(在绘图前请检查官方文档)。以下是一个样条图的示例
require __DIR__.'/../vendor/autoload.php'; use CpChart\Factory\Factory; use Exception; try { // Create a factory class - it will load necessary files automatically, // otherwise you will need to add them on your own $factory = new Factory(); $myData = $factory->newData(array(), "Serie1"); // Create the image and set the data $myPicture = $factory->newImage(700, 230, $myData); $myPicture->setShadow( true, array("X" => 1, "Y" => 1, "R" => 0, "G" => 0, "B" => 0, "Alpha" => 20) ); // 1st spline drawn in white with control points visible $firstCoordinates = array(array(40, 80), array(280, 60), array(340, 166), array(590, 120)); $fistSplineSettings = array("R" => 255, "G" => 255, "B" => 255, "ShowControl" => true); $myPicture->drawSpline($firstCoordinates, $fistSplineSettings); // 2nd spline dashed drawn in white with control points visible $secondCoordinates = array(array(250, 50), array(250, 180), array(350, 180), array(350, 50)); $secondSplineSettings = array( "R" => 255, "G" => 255, "B" => 255, "ShowControl" => true, "Ticks" => 4 ); $myPicture->drawSpline($secondCoordinates, $secondSplineSettings); // Output the chart to the browser $myPicture->Render("example.drawSpline.png"); $myPicture->Stroke(); } catch (Exception $ex) { echo sprintf('There was an error: %s', $ex->getMessage()); }
通过专用类绘制图表
某些图表需要使用专用类,您可以通过工厂创建它。注意,您指定的是图表类型,而不是类名。以下是一个饼图的示例
require __DIR__.'/../vendor/autoload.php'; use CpChart\Chart\Pie; use CpChart\Factory\Factory; use Exception; try { $factory = new Factory(); // Create and populate data $myData = $factory->newData(array(40, 60, 15, 10, 6, 4), "ScoreA"); $myData->setSerieDescription("ScoreA", "Application A"); // Define the absissa serie $myData->addPoints(array("<10", "10<>20", "20<>40", "40<>60", "60<>80", ">80"), "Labels"); $myData->setAbscissa("Labels"); // Create the image $myPicture = $factory->newImage(700, 230, $myData); // Draw a solid background $backgroundSettings = array( "R" => 173, "G" => 152, "B" => 217, "Dash" => 1, "DashR" => 193, "DashG" => 172, "DashB" => 237 ); $myPicture->drawFilledRectangle(0, 0, 700, 230, $backgroundSettings); //Draw a gradient overlay $gradientSettings = array( "StartR" => 209, "StartG" => 150, "StartB" => 231, "EndR" => 111, "EndG" => 3, "EndB" => 138, "Alpha" => 50 ); $myPicture->drawGradientArea(0, 0, 700, 230, DIRECTION_VERTICAL, $gradientSettings); $myPicture->drawGradientArea( 0, 0, 700, 20, DIRECTION_VERTICAL, array( "StartR" => 0, "StartG" => 0, "StartB" => 0, "EndR" => 50, "EndG" => 50, "EndB" => 50, "Alpha" => 100 ) ); // Add a border to the picture $myPicture->drawRectangle(0, 0, 699, 229, array("R" => 0, "G" => 0, "B" => 0)); // Write the picture title $myPicture->setFontProperties(array("FontName" => "Silkscreen.ttf", "FontSize" => 6)); $myPicture->drawText(10, 13, "pPie - Draw 2D pie charts", array("R" => 255, "G" => 255, "B" => 255)); // Set the default font properties $myPicture->setFontProperties( array("FontName" => "Forgotte.ttf", "FontSize" => 10, "R" => 80, "G" => 80, "B" => 80) ); // Enable shadow computing $myPicture->setShadow( true, array("X" => 2, "Y" => 2, "R" => 150, "G" => 150, "B" => 150, "Alpha" => 100) ); $myPicture->drawText( 140, 200, "Single AA pass", array("R" => 0, "G" => 0, "B" => 0, "Align" => TEXT_ALIGN_TOPMIDDLE) ); // Create and draw the chart /* @var $pieChart CpPie */ $pieChart = $factory->newChart("pie", $myPicture, $myData); $pieChart->draw2DPie(140, 125, array("SecondPass" => false)); $pieChart->draw2DPie(340, 125, array("DrawLabels" => true, "Border" => true)); $pieChart->draw2DPie( 540, 125, array( "DataGapAngle" => 10, "DataGapRadius" => 6, "Border" => true, "BorderR" => 255, "BorderG" => 255, "BorderB" => 255 ) ); $myPicture->drawText( 540, 200, "Extended AA pass / Splitted", array("R" => 0, "G" => 0, "B" => 0, "Align" => TEXT_ALIGN_TOPMIDDLE) ); // Save the chart to a test directory and output it to a browser $pieChart->pChartObject->Render("charts/example.draw2DPie.png"); $pieChart->pChartObject->stroke(); } catch (Exception $ex) { echo sprintf('There was an error: %s', $ex->getMessage()); }
注意
基本上,所有操作都应该像在 pChart 2.0 文档中定义的那样进行,同时增加了 try/catch 功能的支持。工厂类有方法加载 pChart 库中存在的所有类型的类。
重要! 如果您想使用任何字体或调色板文件,只提供实际文件的名称,不要将 'fonts' 或 'palettes' 文件夹添加到函数提供的字符串中。如果您想从默认目录以外的目录加载它们,则需要添加文件的完整路径(例如:__DIR__.'/folder/to/my/palletes'
)。
变更日志
1.0 稳定版本,具有基本功能。
1.1 添加了工厂服务。
1.1.1 通过工厂略微改变了图表的加载方式(请参阅类注释)。
1.1.2 更新了服务类,以处理缺失/错误的类名异常。
1.1.3 现在通过 Composer 加载类常量文件(感谢 ThaDafinser)。
1.1.4 修复了破坏代码的打字错误(感谢 subtronic)。
1.1.5 添加了一个选项,可以隐藏 X 轴或仅隐藏其值(感谢 julien-gm)。
1.1.6 在格式化刻度时添加了对闭包的支持(感谢 funkjedi)。
2.0 将所有类更新为 PSR-2 标准,尽可能添加类型提示,更新方法注释以确保尽可能准确。添加了 Behat 测试,并重构了命名空间结构。
参考文献
GitHub 上的 PHP 编码标准 PHP 框架互操作性小组