louisauthie / php2plotly

此包的最新版本(dev-master)没有可用的许可信息。

PHP 中 Plotly 脚本生成

dev-master 2024-06-28 17:42 UTC

This package is not auto-updated.

Last update: 2024-09-30 14:13:09 UTC


README

php2plotly 是一个 PHP 库,允许您使用 Plotly.js 库轻松创建交互式图表和图形。使用 php2plotly,您可以为您 Web 应用程序生成美观且动态的可视化。

特性

  • 支持各种图表类型,包括柱状图、折线图、散点图等。
  • 与 Plotly.js 无缝集成,提供高级功能。

安装

要安装 php2plotly,只需使用 Composer 引入库

composer require louisauthie/php2plotly

用法

使用 php2plotly 非常简单。以下是一个基本示例以供您开始

<?php
namespace App;
require_once __DIR__ . '/../vendor/autoload.php';
use Php2plotly\basic\BarChart;
use Php2plotly\basic\PieChart;
use Php2plotly\basic\ScatterPlot;
use Php2plotly\preprocessor\Preprocessor;
use Php2plotly\stats\BoxPlot;
use Php2plotly\stats\Histogram;
use Php2plotly\scientific\Heatmap;
?>

<html>
    <head>
        <script src="../assets/js/plotly-2.32.0.min.js" charset="utf-8"></script>
    </head>

    <body style="display:flex; flex-wrap:wrap; max-with:100vw;">

    <div id="scatter" style="width:600px;height:400px;"></div>
    <?php
        $scatter = new ScatterPlot('scatter', [
            ['x' => [1, 2, 3, 4], 'y' => [10, 15, 13, 17], 'mode' => 'markers'],
            ['x' => [2, 3, 4, 5], 'y' => [16, 5, 11, 9], 'mode' => 'lines']
        ]);
        echo '<script>'.$scatter->render().'</script>';

    ?>

    <div id="bar" style="width:600px;height:400px;"></div>

    <?php
        $bar = new BarChart('bar', ['x' => ["Cat1", "Cat2", "Cat3", "Cat4"], 'y' => [10, 15, 13, 17]]);
        echo '<script>'.$bar->render().'</script>';
    ?>

    <div id="pie" style="width:600px;height:400px;"></div>
    <?php
        $pie = new PieChart('pie', ['values' => [10, 15, 13, 17], 'labels' => ["Cat1", "Cat2", "Cat3", "Cat4"]], ['height' => 400, 'width' => 600]);
        echo '<script>'.$pie->render().'</script>';
    ?>

    <div id="histogram" style="width:600px;height:400px;"></div>
    <?php
        $histogram = new Histogram('histogram', ['x' => [1, 2, 3, 4]]);
        echo '<script>'.$histogram->render().'</script>';
    ?>

    <div id="bplot" style="width:600px;height:400px;"></div>
    <?php
        $listsBoxPlot  = [
            ['y' => [1, 2, 3, 4, 5, 8, 10, 40, 0, -8]],
            ['y' => [1, 2, 3, 4, 5, 8, 10, 40, 0, -8]],
            ['y' => [1, 2, 3, 4, 5, 8, 10, 40, 0, -8]],
            ['y' => [1, 2, 3, 4, 5, 8, 10, 10, 0, -7]],
            ['y' => [1, 2, 3, 4, 5, 8, 10, 10, 0, -7]],
        ];
        $boxplot = new BoxPlot('bplot', $listsBoxPlot);
        echo '<script>'.$boxplot->render().'</script>';
    ?>

    <div id="heatmap" style="width:600px;height:400px;"></div>
    <?php
        $heatmap = new Heatmap('heatmap', 
            ['x' => ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], 'y' => ['Morning', 'Afternoon', 'Evening'], 'z' => [[1, 0, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]]]
        );
        echo '<script>'.$heatmap->render().'</script>';
    ?>

    <div id="countHistog" style="width:600px;height:400px;"></div>
    <?php
        $values= [1.2,1.4, 2, 3, 7, 4, 4, 4, 4, 4.5, 4, 4];
        $histogram = new BarChart('countHistog', Preprocessor::countNumberByInterval($values, 3));
        echo '<script>'.$histogram->render().'</script>';
    ?>
    </body>
</html>

贡献

欢迎贡献!如果您有任何错误报告、功能请求或拉取请求,请在 GitHub 仓库 上打开一个问题。

许可

php2plotly 在 MIT 许可下授权。有关更多信息,请参阅 LICENSE