n8n工作流自动化平台的实操:生成统计图的两种方式

打印 上一主题 下一主题

主题 1850|帖子 1850|积分 5550

1.成果展示

1.1n8n的工作流


 扳连节点:Postgres、Code、QuickChart、Edit Fields、HTTP Request
12.表现结果

 

 
2.实操过程

2.1节点说明

2.1.1Postgres节点:


注:将明细数据举行汇总。 
2.1.2code节点:


注:将 查询的数据转换成QuickChart必要的格式,代码如下:
  1. let data = $input.all();
  2. let rawData = data.map(item => item.json);
  3. // 按年份分组数据
  4. const groupedData = rawData.reduce((acc, item) => {
  5.     if (!acc[item.nf]) acc[item.nf] = [];
  6.     acc[item.nf].push({ month: item.yf, value: parseInt(item.sl) });
  7.     return acc;
  8. }, {});
  9. // 构建完整的12个月份列表,作为 X 轴标签
  10. const allMonths = Array.from({ length: 12 }, (_, i) => {
  11.     const month = (i + 1).toString().padStart(2, '0'); // 01 - 12
  12.     return month;
  13. });
  14. // 构建 QuickChart 需要的 chartData 格式
  15. const chartData = {
  16.     type: 'line',
  17.     data: {
  18.         labels: allMonths, // 使用完整12个月作为 X 轴
  19.         datasets: Object.keys(groupedData).map(year => {
  20.             // 补全缺失月份的数据为 null(图表上该点为空)
  21.             const values = allMonths.map(month => {
  22.                 const found = groupedData[year].find(d => d.month === month);
  23.                 return found ? found.value : null;
  24.             });
  25.             return {
  26.                 label: `${year}年 事故起数`,
  27.                 data: values,
  28.                 borderColor: getRandomColor(),
  29.                 fill: false,
  30.                 pointRadius: values.map(v => v !== null ? 3 : 0), // 可选:隐藏空值点
  31.             };
  32.         })
  33.     },
  34.     options: {
  35.         responsive: true,
  36.         scales: {
  37.             xAxes: [{
  38.                 scaleLabel: {
  39.                     display: true,
  40.                     labelString: '月份'
  41.                 }
  42.             }],
  43.             yAxes: [{
  44.                 ticks: {
  45.                     beginAtZero: true
  46.                 },
  47.                 scaleLabel: {
  48.                     display: true,
  49.                     labelString: '事故起数'
  50.                 }
  51.             }]
  52.         }
  53.     }
  54. };
  55. // 随机颜色生成函数
  56. function getRandomColor() {
  57.     const r = Math.floor(Math.random() * 256);
  58.     const g = Math.floor(Math.random() * 256);
  59.     const b = Math.floor(Math.random() * 256);
  60.     return `rgba(${r},${g},${b},1)`;
  61. }
  62. return chartData;
复制代码
2.1.3Edit Fields节点:


注:将json对象转换成字符串,主要图中的红框。 
2.1.4HTTP Request节点:

 


注:通过https://quickchart.io/chart?width=650&height=450&c={{ $json.data }}实现图片的生成,图中红框部分。
通过 QuickChart节点,最后也是转成url地点。因此不能在无互联网的环境下生成图片,希望有缘人提供更好的思路。
2.1.5QuickChart节点:


注:生成单曲线,单柱状图,通过 QuickChart节点没有题目,假如有上传多条曲线,则无法实现,只能通过https://quickchart.io/chart?width=650&height=450&c={{ $json.data }}方式实现。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表