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