PHP 利用 Jpgraph 生成统计图(柱状图、折线图等)

本文介绍了如何将钉钉系统中的统计信息通过Jpgraph创建丰富的图表,应用于飞书消息中,提升用户交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       最近公司办公系统由钉钉迁移至飞书,需要把原有内部系统的统计信息迁移至飞书。之前信息内容只是简单的文字内容提醒,看起来比较单调。这次就想着插入一些统计图表,使消息看起来更为的丰富,也能增加一些用户粘性,看起来更为有趣。最终效果如下图所示

    

       一开始本想来用echarts,但实现起来较为繁琐,于是就转为使用Jpgraph,其效果还相当不错的,实现起来也较为简单,

    //生成图片,上传至飞书,生成img_key
    $path = \Yii::$app->getRuntimePath();
    require_once (Yii::getAlias("@backend").'/components/jpgraph/src/jpgraph.php');
    require_once (Yii::getAlias("@backend").'/components/jpgraph/src/jpgraph_bar.php');

    // Create the graph. These two calls are always required
    $graph = new \Graph(400,280,'auto');
    $graph->SetScale("textlin");

    $theme_class=new \UniversalTheme;
    $graph->SetTheme($theme_class);

    $graph->SetBox(false);
    $graph->xaxis->SetFont(FF_CHINESE);
    $graph->ygrid->SetFill(false);
    $graph->xaxis->SetTickLabels(array_values($company));
    $graph->yaxis->HideLine(false);
    $graph->yaxis->HideTicks(false,false);
    $graph->graph_theme = null;  //设置成null,否则$plot->value->Show() 不生效
    // Create the bar plots
    $b1plot = new \BarPlot($worker_bar_data);
    $b2plot = new \BarPlot($project_bar_data);
    $b1plot->value->Show(); //设置显示数字 
    $b2plot->value->Show(); //设置显示数字 
    $b1plot->SetLegend("工单");
    $b2plot->SetLegend("项目");
    $graph->SetMargin(40,40,20,70);
    // Create the grouped bar plot
    $gbplot = new \GroupBarPlot(array($b1plot,$b2plot));

    // ...and add it to the graPH
    $graph->Add($gbplot);


    $b1plot->SetColor("white");
    $b1plot->SetFillColor("#cc1111");

    $b2plot->SetColor("white");
    $b2plot->SetFillColor("#11cccc");
    $graph->title->Set("RET");

    $graph->Stroke();
    $filename = $path.$img_name.'.png';
    $graph->img->Stream($filename);//将生成的图片保存到本地
    //上传飞书获取image_key
    $PSize = filesize($filename);
    $picturedata = fread(fopen($filename, "r"), $PSize);
    $feishu = new \Feishu\Message();
    $info = $feishu->UploadImg($picturedata);
    return $info['data']['image_key'];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值