当前位置:首页 > 技术分析 > 正文内容

Echarts助力Dify,给你的图表加上美颜

ruisui884周前 (05-21)技术分析15

简介

在数据可视化的领域,图表不仅仅是数据的展示工具,更是信息传递的桥梁。而在众多数据可视化框架中,ECharts无疑是一个广受欢迎的选择。它不仅功能强大,支持丰富的图表类型,还能提供细致入微的定制化设计,帮助用户创造出既美观又富有表现力的图表。而当ECharts与Dify结合时,带来的不仅是图表的美观,更是让数据展示变得更加直观和易于理解的全新体验。

样例

本次没有使用任何的大模型,主要是测试Echarts在Dify中的表现。

主要是使用DIFY的问题分类器做简单的切换。


实现方式

直接在Dify的回复组件中编写即可

```echarts

# echarts 相关代码。可以从https://echarts.apache.org/examples/zh/index.html#chart-type-themeRiver
# 官网获取
```



柱状图

```echarts
{
  "xAxis": {
    "type": "category",
    "data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  },
  "yAxis": {
    "type": "value"
  },
  "series": [
    {
      "data": [120, 200, 150, 80, 70, 110, 130],
      "type": "bar"
    }
  ]
}
```




折线图

```echarts
{
  "xAxis": {
    "type": "category",
    "data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  },
  "yAxis": {
    "type": "value"
  },
  "series": [
    {
      "data": [150, 230, 224, 218, 135, 147, 260],
      "type": "line"
    }
  ]
}

```



基础面积图

```echarts
{
  "xAxis": {
    "type": "category",
    "boundaryGap": false,
    "data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  },
  "yAxis": {
    "type": "value"
  },
  "series": [
    {
      "data": [820, 932, 901, 934, 1290, 1330, 1320],
      "type": "line",
      "areaStyle": {}
    }
  ]
}

```



折线图堆叠

```echarts
{
  "title": {
    "text": "Stacked Line"
  },
  "tooltip": {
    "trigger": "axis"
  },
  "legend": {
    "data": ["Email", "Union Ads", "Video Ads", "Direct", "Search Engine"]
  },
  "grid": {
    "left": "3%",
    "right": "4%",
    "bottom": "3%",
    "containLabel": true
  },
  "toolbox": {
    "feature": {
      "saveAsImage": {}
    }
  },
  "xAxis": {
    "type": "category",
    "boundaryGap": false,
    "data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  },
  "yAxis": {
    "type": "value"
  },
  "series": [
    {
      "name": "Email",
      "type": "line",
      "stack": "Total",
      "data": [120, 132, 101, 134, 90, 230, 210]
    },
    {
      "name": "Union Ads",
      "type": "line",
      "stack": "Total",
      "data": [220, 182, 191, 234, 290, 330, 310]
    },
    {
      "name": "Video Ads",
      "type": "line",
      "stack": "Total",
      "data": [150, 232, 201, 154, 190, 330, 410]
    },
    {
      "name": "Direct",
      "type": "line",
      "stack": "Total",
      "data": [320, 332, 301, 334, 390, 330, 320]
    },
    {
      "name": "Search Engine",
      "type": "line",
      "stack": "Total",
      "data": [820, 932, 901, 934, 1290, 1330, 1320]
    }
  ]
}

```



交错正负轴标签

```echarts
{
  "title": {
    "text": "Bar Chart with Negative Value"
  },
  "tooltip": {
    "trigger": "axis",
    "axisPointer": {
      "type": "shadow"
    }
  },
  "grid": {
    "top": 80,
    "bottom": 30
  },
  "xAxis": {
    "type": "value",
    "position": "top",
    "splitLine": {
      "lineStyle": {
        "type": "dashed"
      }
    }
  },
  "yAxis": {
    "type": "category",
    "axisLine": { "show": false },
    "axisLabel": { "show": false },
    "axisTick": { "show": false },
    "splitLine": { "show": false },
    "data": [
      "ten",
      "nine",
      "eight",
      "seven",
      "six",
      "five",
      "four",
      "three",
      "two",
      "one"
    ]
  },
  "series": [
    {
      "name": "Cost",
      "type": "bar",
      "stack": "Total",
      "label": {
        "show": true,
        "formatter": "{b}"
      },
      "data": [
        { "value": -0.07, "label": "labelRight" },
        { "value": -0.09, "label": "labelRight" },
        0.2,
        0.44,
        { "value": -0.23, "label": "labelRight" },
        0.08,
        { "value": -0.17, "label": "labelRight" },
        0.47,
        { "value": -0.36, "label": "labelRight" },
        0.18
      ]
    }
  ]
}

```



圆角环形图

```echarts
{
  "tooltip": {
    "trigger": "item"
  },
  "legend": {
    "top": "5%",
    "left": "center"
  },
  "series": [
    {
      "name": "Access From",
      "type": "pie",
      "radius": ["40%", "70%"],
      "avoidLabelOverlap": false,
      "itemStyle": {
        "borderRadius": 10,
        "borderColor": "#fff",
        "borderWidth": 2
      },
      "label": {
        "show": false,
        "position": "center"
      },
      "emphasis": {
        "label": {
          "show": true,
          "fontSize": 40,
          "fontWeight": "bold"
        }
      },
      "labelLine": {
        "show": false
      },
      "data": [
        { "value": 1048, "name": "Search Engine" },
        { "value": 735, "name": "Direct" },
        { "value": 580, "name": "Email" },
        { "value": 484, "name": "Union Ads" },
        { "value": 300, "name": "Video Ads" }
      ]
    }
  ]
}

```



嵌套环形图

```echarts
{
  "tooltip": {
    "trigger": "item",
    "formatter": "{a} <br/>{b}: {c} ({d}%)"
  },
  "legend": {
    "data": [
      "Direct",
      "Marketing",
      "Search Engine",
      "Email",
      "Union Ads",
      "Video Ads",
      "Baidu",
      "Google",
      "Bing",
      "Others"
    ]
  },
  "series": [
    {
      "name": "Access From",
      "type": "pie",
      "selectedMode": "single",
      "radius": [0, "30%"],
      "label": {
        "position": "inner",
        "fontSize": 14
      },
      "labelLine": {
        "show": false
      },
      "data": [
        { "value": 1548, "name": "Search Engine" },
        { "value": 775, "name": "Direct" },
        { "value": 679, "name": "Marketing", "selected": true }
      ]
    },
    {
      "name": "Access From",
      "type": "pie",
      "radius": ["45%", "60%"],
      "labelLine": {
        "length": 30
      },
      "label": {
        "formatter": "{a|{a}}{abg|}\n{hr|}\n  {b|{b}:}{c}  {per|{d}%}  ",
        "backgroundColor": "#F6F8FC",
        "borderColor": "#8C8D8E",
        "borderWidth": 1,
        "borderRadius": 4,
        "rich": {
          "a": {
            "color": "#6E7079",
            "lineHeight": 22,
            "align": "center"
          },
          "hr": {
            "borderColor": "#8C8D8E",
            "width": "100%",
            "borderWidth": 1,
            "height": 0
          },
          "b": {
            "color": "#4C5058",
            "fontSize": 14,
            "fontWeight": "bold",
            "lineHeight": 33
          },
          "per": {
            "color": "#fff",
            "backgroundColor": "#4C5058",
            "padding": [3, 4],
            "borderRadius": 4
          }
        }
      },
      "data": [
        { "value": 1048, "name": "Baidu" },
        { "value": 335, "name": "Direct" },
        { "value": 310, "name": "Email" },
        { "value": 251, "name": "Google" },
        { "value": 234, "name": "Union Ads" },
        { "value": 147, "name": "Bing" },
        { "value": 135, "name": "Video Ads" },
        { "value": 102, "name": "Others" }
      ]
    }
  ]
}

```



自定义雷达图

```echarts
{
  "color": ["#67F9D8", "#FFE434", "#56A3F1", "#FF917C"],
  "title": {
    "text": "Customized Radar Chart"
  },
  "legend": {},
  "radar": [
    {
      "indicator": [
        { "text": "Indicator1" },
        { "text": "Indicator2" },
        { "text": "Indicator3" },
        { "text": "Indicator4" },
        { "text": "Indicator5" }
      ],
      "center": ["25%", "50%"],
      "radius": 120,
      "startAngle": 90,
      "splitNumber": 4,
      "shape": "circle",
      "axisName": {
        "formatter": "【{value}】",
        "color": "#428BD4"
      },
      "splitArea": {
        "areaStyle": {
          "color": ["#77EADF", "#26C3BE", "#64AFE9", "#428BD4"],
          "shadowColor": "rgba(0, 0, 0, 0.2)",
          "shadowBlur": 10
        }
      },
      "axisLine": {
        "lineStyle": {
          "color": "rgba(211, 253, 250, 0.8)"
        }
      },
      "splitLine": {
        "lineStyle": {
          "color": "rgba(211, 253, 250, 0.8)"
        }
      }
    },
    {
      "indicator": [
        { "text": "Indicator1", "max": 150 },
        { "text": "Indicator2", "max": 150 },
        { "text": "Indicator3", "max": 150 },
        { "text": "Indicator4", "max": 120 },
        { "text": "Indicator5", "max": 108 },
        { "text": "Indicator6", "max": 72 }
      ],
      "center": ["75%", "50%"],
      "radius": 120,
      "axisName": {
        "color": "#fff",
        "backgroundColor": "#666",
        "borderRadius": 3,
        "padding": [3, 5]
      }
    }
  ],
  "series": [
    {
      "type": "radar",
      "emphasis": {
        "lineStyle": {
          "width": 4
        }
      },
      "data": [
        {
          "value": [100, 8, 0.4, -80, 2000],
          "name": "Data A"
        },
        {
          "value": [60, 5, 0.3, -100, 1500],
          "name": "Data B",
          "areaStyle": {
            "color": "rgba(255, 228, 52, 0.6)"
          }
        }
      ]
    },
    {
      "type": "radar",
      "radarIndex": 1,
      "data": [
        {
          "value": [120, 118, 130, 100, 99, 70],
          "name": "Data C",
          "symbol": "rect",
          "symbolSize": 12,
          "lineStyle": {
            "type": "dashed"
          },
          "label": {
            "show": true,
            "formatter": "function (params) { return params.value; }"
          }
        },
        {
          "value": [100, 93, 50, 90, 70, 60],
          "name": "Data D",
          "areaStyle": {
            "color": {
              "type": "radial",
              "x": 0.1,
              "y": 0.6,
              "r": 1,
              "colorStops": [
                {
                  "color": "rgba(255, 145, 124, 0.1)",
                  "offset": 0
                },
                {
                  "color": "rgba(255, 145, 124, 0.9)",
                  "offset": 1
                }
              ]
            }
          }
        }
      ]
    }
  ]
}

```



漏斗图

```echarts
{
  "title": {
    "text": "Funnel"
  },
  "tooltip": {
    "trigger": "item",
    "formatter": "{a} <br/>{b} : {c}%"
  },
  "toolbox": {
    "feature": {
      "dataView": { "readOnly": false },
      "restore": {},
      "saveAsImage": {}
    }
  },
  "legend": {
    "data": ["Show", "Click", "Visit", "Inquiry", "Order"]
  },
  "series": [
    {
      "name": "Funnel",
      "type": "funnel",
      "left": "10%",
      "top": 60,
      "bottom": 60,
      "width": "80%",
      "min": 0,
      "max": 100,
      "minSize": "0%",
      "maxSize": "100%",
      "sort": "descending",
      "gap": 2,
      "label": {
        "show": true,
        "position": "inside"
      },
      "labelLine": {
        "length": 10,
        "lineStyle": {
          "width": 1,
          "type": "solid"
        }
      },
      "itemStyle": {
        "borderColor": "#fff",
        "borderWidth": 1
      },
      "emphasis": {
        "label": {
          "fontSize": 20
        }
      },
      "data": [
        { "value": 60, "name": "Visit" },
        { "value": 40, "name": "Inquiry" },
        { "value": 20, "name": "Order" },
        { "value": 80, "name": "Click" },
        { "value": 100, "name": "Show" }
      ]
    }
  ]
}

```


扫描二维码推送至手机访问。

版权声明:本文由ruisui88发布,如需转载请注明出处。

本文链接:http://www.ruisui88.com/post/4189.html

分享给朋友:

“Echarts助力Dify,给你的图表加上美颜” 的相关文章

10分钟搞定gitlab-ci自动化部署

gitlab-ci 是持续集成工具/自动化部署工具,类似 jenkins。持续集成 是将代码集成到共享存储库并尽可能早地自动构建/测试每个更改的实践 - 通常一天几次。概述在编码完成时都会进行打包发布过程,如果每次都手动操作这一步骤就会浪费时间,效率低下。所以就有了持续集成。准备事项请提前安装以下软...

「云原生」Containerd ctr,crictl 和 nerdctl 命令介绍与实战操作

一、概述作为接替Docker运行时的Containerd在早在Kubernetes1.7时就能直接与Kubelet集成使用,只是大部分时候我们因熟悉Docker,在部署集群时采用了默认的dockershim。在V1.24起的版本的kubelet就彻底移除了dockershim,改为默认使用Conta...

vue v-html动态生成的html怎么加样式/事件

1、动态生成的html,样式不生效//html 布局 <view v-html="html"> {{html}} </view> //动态生成的元素 <view class="btngo" @tap="handleLink...

学前端,这30个CSS选择器,你必须熟记

你学会了基本的id,class类选择器和descendant后代选择器,然后就觉得完事了吗?如果这样,你就会错过许多灵活运用CSS的机会。虽然本文提到的许多选择器都属于CSS3,并且只能在现代的浏览器中使用,但学会这些是大有好处的。什么是CSS选择器呢?每一条css样式定义由两部分组成,形式如下:[...

JS数组过滤元素的方法

引言JavaScript 作为前端开发的核心技术之一,在现代 Web 开发中扮演着举足轻重的角色。随着 Web 应用越来越复杂,高效处理数据集合的需求日益凸显。本文旨在介绍 JavaScript 中数组过滤的基础知识及其在实际项目中的应用技巧。技术概述定义数组过滤是 JavaScript 提供的一种...

三勾商城(java+vue3)微信小程序商城+SAAS+前后端源码

项目介绍本系统功能包括: 前台展示+后台管理+SAAS管理端,包括最基本的用户登录注册,下单, 购物车,购买,结算,订单查询,收货地址,后台商品管 理,订单管理,用户管理等等功能,小伙伴一起来看看吧。三勾小程序商城基于springboot+element-ui+uniapp打造的面向开发的小程序商城...