找回密码
 立即注册
首页 业界区 业界 echarts大屏项目指南

echarts大屏项目指南

蒋炸役 昨天 18:46
大屏项目指南

资源列表
  1. 1. echars官方文档:[Apache ECharts](https://echarts.apache.org/zh/index.html)
  2.         1. api: [Api](https://echarts.apache.org/zh/api.html#echarts)
  3.         2. 配置项手册:[Options](https://echarts.apache.org/zh/option.html#title)
  4.         3. 术语快查手册:[术语速查手册](https://echarts.apache.org/zh/cheat-sheet.html)
  5.         4. [echarts的渲染器ZRender](https://ecomfe.github.io/zrender-doc/public/)
  6.         5. [echarts源码](https://github.com/apache/echarts)
  7.         6. ...
  8. 2. 示例网站大全
  9.         1. [PPChart - 让图表更简单](https://ppchart.com/#/)
  10.         2. [makeapie echarts社区图表可视化案例](https://www.makeapie.cn/echarts)
  11.         3. [MCChart](https://echarts.zhangmuchen.top/#/index)
  12.         4. [分享你我 - ECharts 作品集](http://chart.majh.top/)
  13.         5. [分享你的可视化作品isqqw.com](https://www.isqqw.com/)
  14.         6. [官网示例](https://demo.runoops.com/echarts-website/examples/zh/index.html#chart-type-line)
  15. 3. 其他免费资源
  16.         1. 阿里云免费地图数据下载:[DataV.GeoAtlas地理小工具系列](https://datav.aliyun.com/portal/school/atlas/area_selector)
  17.         2. 100套大屏可视化模板 (他这些都是jquery的项目,可以参考学习): [100+套大数据可视化炫酷大屏Html5模板;包含行业:社区、物业、政务、交通、金融银行等,全网最新、最多,最全、最酷、最炫大数据可视化模板。陆续更新中](https://github.com/iGaoWei/BigDataView)
  18.         3. 数据可视化设计:[统计图表 Chart | AntV](https://antv.antgroup.com/specification/graph/chart)
复制代码
大屏项目布局和响应式


  • 页面布局就用Gird布局(让ai生成)  CSS Grid 网格布局教程 - 阮一峰的网络日志 ,CSS 网格布局指南 | CSS-Tricks - CSS技巧
  • 响应式用rem
  • echats中的尺寸可以自己写个函数转换一下

    • 场景是:在echarts的options配置的尺寸是固定的px,比如设置label的fontSize:14,这个14是固定的就是14px,这里不会动态变化,当大分辨率下会显得很小所以需要我们写个函数处理下得到动态的结果
      1. // 基于设计稿宽度自适应的函数
      2. function autoSize(size) {
      3.   const clientWidth = window.innerWidth || document.documentElement.clientWidth;
      4.   const scale = clientWidth / 1920; // 1920是设计稿宽度
      5.   return Math.round(size * scale);
      6. }
      7. // ECharts 配置示例
      8. option = {
      9.   title: {
      10.     text: '自适应字体',
      11.     textStyle: {
      12.       fontSize: autoSize(20) // 20 是设计稿上写的字号
      13.     }
      14.   },
      15.   series: [
      16.     {
      17.       type: 'pie',
      18.       label: {
      19.         fontSize: autoSize(14)
      20.       }
      21.     }
      22.   ]
      23. };
      复制代码


echarts使用入门


  • 下载echarts包 npm install echarts 即可
  • let chart = echarts.init()//初始化,要保证能够拿到dom并且dom是可见的
  • 设置初始option
  • 绑定resize事件
  • 绑定点击事件
  • 拿到数据后更新option
    下面是封装的一个class,框架无关
  1. import charttheme from '../assets/charttheme.json';//主题文件,可在echarts官网自定义主题
  2. import * as Chart from 'echarts';
  3. class ChartClass {
  4.   // 静态属性存储已注册的主题
  5.   static registeredThemes = new Set();
  6.     //默认配置项,可以在初始化图标后设置,统一放在这个类的静态属性里,提高复用性
  7.   static defaultOption = {
  8.       grid: {
  9.         left: '3%',
  10.         right: '4%',
  11.         bottom: '10%',
  12.         containlabel: true
  13.       },
  14.                 //...
  15.     };
  16.   // 静态方法注册主题
  17.   static registerTheme(themeName = 'infographic', themeConfig = charttheme) {
  18.     if (!this.registeredThemes.has(themeName)) {
  19.       Chart.registerTheme(themeName, themeConfig);
  20.       this.registeredThemes.add(themeName);
  21.     }
  22.   }
  23.   constructor(dom, theme = 'infographic', initOptions = {}) {
  24.       //
  25.      if(Chart.getInstanceByDom(dom))return//如果已经挂载就退出
  26.     this.chart =  Chart.init(dom, theme, initOptions);
  27.     this.dom = dom;
  28.     this.observer = null;
  29.     this.resizeObserver = null;
  30.     // this.chart.setOption(this.defaultChartOptions)
  31.     //DOM 监听(用于销毁时清理)
  32.     //当dom销毁时,进行清理操作
  33.     this._initObserver();
  34.     // 初始化尺寸监听
  35.     this._initResizeObserver();
  36.   }
  37.   setOption(options) {
  38.     // 合并默认配置和传入的配置
  39.     const mergedOptions = Object.assign({}, this.defaultchartoptions, options);
  40.     this.chart.setOption(mergedOptions, true);
  41.   }
  42.   dispose() {
  43.     if (this.chart) {
  44.       this.chart.dispose();
  45.       this.chart = null;
  46.     }
  47.     if (this.observer) {
  48.       this.observer.disconnect();
  49.       this.observer = null;
  50.     }
  51.     if (this.resizeObserver) {
  52.       this.resizeObserver.disconnect();
  53.       this.resizeObserver = null;
  54.     }
  55.   }
  56.   dispatchAction(){
  57.     this.chart.dispatchAction(...arguments);
  58.   }
  59.   _initObserver() {
  60.     const parent = this.dom.parentNode;
  61.     if (!parent) {
  62.       this.dispose();
  63.       return;
  64.     }
  65.     const observer = new MutationObserver((mutations) => {
  66.       for (const mutation of mutations) {
  67.         if (mutation.type === 'childList') {
  68.           // 检查被移除的节点中是否包含当前图表DOM
  69.           for (const removedNode of mutation.removedNodes) {
  70.             if (removedNode.contains(this.dom) || removedNode === this.dom) {
  71.               this.dispose();
  72.               observer.disconnect();
  73.               return;
  74.             }
  75.           }
  76.          
  77.           // 同时检查DOM是否仍在文档中
  78.           if (!document.body.contains(this.dom)) {
  79.             this.dispose();
  80.             observer.disconnect();
  81.             return;
  82.           }
  83.         }
  84.       }
  85.     });
  86.     // 修改观察选项,增加subtree以监听所有子节点变化
  87.     observer.observe(document.body, {
  88.       childList: true,
  89.       subtree: true
  90.     });
  91.     this.observer = observer;
  92.   }
  93.   _initResizeObserver() {
  94.     // const resizeObserver = new ResizeObserver(() => {
  95.     //   if (this.chart && !this.chart.isDisposed()) {
  96.     //     this.chart.resize();
  97.     //   }
  98.     // });
  99.     // resizeObserver.observe(this.dom);
  100.     // this.resizeObserver = resizeObserver;
  101.     window.addEventListener('resize', () => {
  102.       if (this.chart && !this.chart.isDisposed()) {
  103.         console.log("窗口尺寸变化");
  104.         
  105.         this.chart.resize();
  106.       }
  107.     });
  108.   }
  109. }
  110. // 静态方法调用:类初始化时注册主题
  111. // ChartClass.registerTheme();
  112. export default ChartClass;
复制代码
使用
  1. import ChartClass from './chartClass';
  2. export default function sandian(dom) {
  3.     // 根据百分比计算背景色
  4.     function getBackgroundColor(percentStr) {
  5.         const percent = parseFloat(percentStr.replace('%', ''));
  6.         if (percent >= 60) return '#ff4757';  // 红色(高百分比)
  7.         if (percent >= 20) return '#ffa726';  // 橙色(中等百分比)
  8.         return '#42a5f5';  // 蓝色(低百分比)
  9.     }
  10.     function getBorderColor(percentStr) {
  11.         const percent = parseFloat(percentStr.replace('%', ''));
  12.         if (percent >= 60) return '#ff3742';
  13.         if (percent >= 20) return '#ff9800';
  14.         return '#2196f3';
  15.     }
  16.     var data2 = [
  17.         {
  18.             name: "马云马老板",
  19.             children: [
  20.                 {
  21.                     //子集
  22.                     name: "北京国风信通科技有限公司",
  23.                     value: "控股",
  24.                     percent: "60%",
  25.                     money: "120万元",
  26.                     bgColor: getBackgroundColor("60%"),
  27.                     borderColor: getBorderColor("60%")
  28.                 },
  29.                 {
  30.                     name: "北京阿里巴巴信息技术有限公司",
  31.                     value: "控股",
  32.                     percent: "1.43%",
  33.                     money: "800万元",
  34.                     bgColor: getBackgroundColor("1.43%"),
  35.                     borderColor: getBorderColor("1.43%")
  36.                 },
  37.                 {
  38.                     name: "高德软件有限公司",
  39.                     value: "控股",
  40.                     percent: "67%",
  41.                     money: "16242.4242万元",
  42.                     bgColor: getBackgroundColor("67%"),
  43.                     borderColor: getBorderColor("67%")
  44.                 },
  45.                 {
  46.                     name: "杭州大井头贰拾贰号文化艺术有限公司",
  47.                     value: "控股",
  48.                     percent: "99%",
  49.                     money: "990万元",
  50.                     bgColor: getBackgroundColor("99%"),
  51.                     borderColor: getBorderColor("99%")
  52.                 },
  53.             ],
  54.         },
  55.     ];
  56.     var chartDom = dom
  57.     var myChart = new ChartClass(chartDom);
  58.     var option;
  59.     option = {
  60.         tooltip: {
  61.             trigger: "item",
  62.             formatter: "{b}",
  63.         },
  64.         series: [
  65.             {
  66.                 type: "tree",
  67.                 name: "股权穿透图",
  68.                 edgeShape: "polyline", //链接线是折现还是曲线
  69.                 orient: "TB",
  70.                 data: data2,
  71.                 width: 1000,
  72.                 height: 200,
  73.                 top: "30%",
  74.                 left: "5%",
  75.                 symbolSize: 1,
  76.                 initialTreeDepth: 10,
  77.                 hoverAnimation: false, // 禁用悬停动画
  78.                 label: {
  79.                     // show: false,
  80.                     position: [-80, 10],  // 调整位置以适应更宽的标签
  81.                     verticalAlign: "middle",
  82.                     align: "left",
  83.                     backgroundColor: "#0084ff",
  84.                     color: "#fff",
  85.                     padding: [15, 20, 15, 20],  // 上右下左的内边距
  86.                     borderWidth: 2,
  87.                     borderColor: "#0070d9",
  88.                     fontWeight: "bold",
  89.                     fontSize: 14,  // 减小字体以适应更多文本
  90.                     minMargin: 15,  // 增加最小边距避免重叠
  91.                     width: 120,  // 设置固定宽度
  92.                     height: 40,   // 设置固定高度
  93.                     overflow: 'break',  // 文本换行而不是截断
  94.                     lineHeight: 16,  // 设置行高
  95.                     // formatter: function(params) {
  96.                     //     // 自定义格式器,确保文本适应宽度
  97.                     //     return params.name;
  98.                     // }
  99.                 },
  100.                 leaves: {
  101.                     label: {
  102.                         // show: false,
  103.                         position: [-120, 40],
  104.                         verticalAlign: "middle",
  105.                         align: "left",
  106.                         color: "#fff",
  107.                         padding: [15, 20, 15, 20],
  108.                         borderWidth: 2,
  109.                         width: 200,
  110.                         height: 50,
  111.                         fontSize: 12,
  112.                         fontWeight: "bold",
  113.                         overflow: 'truncate',
  114.                         lineHeight: 14,
  115.                         ellipsis: '...',
  116.                         formatter: function (params) {
  117.                             const percent = params.data.percent || '0%';
  118.                             const money = params.data.money || '';
  119.                             // const value = params.data.value || '';
  120.                             const name = params?.name || '';
  121.                             let bg = parseInt(money) >= 1000 ? 'red' : 'container';
  122.                             return `{${bg}|${name}}\n{${bg}|${percent}}\n{${bg}|${money}}`;
  123.                         },
  124.                         backgroundColor: 'transparent',
  125.                         borderColor: "transparent",
  126.                         rich: {
  127.                             container: {
  128.                                 backgroundColor: "#0084ff",
  129.                                 // borderColor: "#e0e0e0",
  130.                                 borderWidth: 1,
  131.                                 // borderRadius: 6,
  132.                                 padding: [20, 15],
  133.                                 width: 200,
  134.                                 lineHeight: 30,
  135.                                 // lineHeight: 14,
  136.                             },
  137.                             red: {
  138.                                 backgroundColor: "#ff4757",
  139.                                 // borderColor: "#e0e0e0",
  140.                                 borderWidth: 1,
  141.                                 // borderRadius: 6,
  142.                                 padding: [20, 15],
  143.                                 width: 200,
  144.                                 lineHeight: 30,
  145.                             }
  146.                         }
  147.                     }
  148.                 },
  149.                 lineStyle: {
  150.                     color: "#9b97beff",
  151.                 },
  152.                 expandAndCollapse: false,
  153.                 animationDuration: 550,
  154.                 animationDurationUpdate: 750,
  155.             },
  156.         ],
  157.     };
  158.     option && myChart.setOption(option);
  159.     //可以通过他的内部api得到坐标,然后通过graphic覆盖来自定义节点形状等
  160.    
  161.    
  162.    
  163.     // myChart.chart.on('finished', function () {
  164.     //     var seriesModel = myChart.chart.getModel().getSeriesByIndex(0);
  165.     //     var data = seriesModel.getData();
  166.     //     // 清空旧的 graphic
  167.     //     //   myChart.chart.setOption({ graphic: [] });
  168.     //     let graphic = [
  169.     //     ]
  170.     //     // 遍历所有节点并添加背景矩形 + 文本
  171.     //     data._graphicEls.forEach((el, index) => {
  172.     //         const layout = { x: 0, y: 0 }
  173.     //         layout.x = el.transform[4]
  174.     //         layout.y = el.transform[5]
  175.     //         if (!layout) return
  176.     //         // 使用 graphic 绘制背景和文本
  177.     //         const groupName = data._idList[index];
  178.     //         // 创建图形组
  179.     //         const graphicGroup = {
  180.     //             type: 'group',
  181.     //             name: groupName, // 分组名称,用于后续查找
  182.     //             id: `group_${index}`, // 唯一标识
  183.     //             position: [layout.x, layout.y], // 组定位(替代单个元素定位)
  184.     //             children: [
  185.     //                 // 第一个图特殊处理:矩形改为椭圆并设置黄色
  186.     //                 index === 2 ? {
  187.     //                     type: 'ellipse',  // 将矩形改为椭圆
  188.     //                     z: 1000,
  189.     //                     shape: {
  190.     //                         cx: 0,       // 椭圆中心x坐标(相对于组原点)
  191.     //                         cy: 0,       // 椭圆中心y坐标(相对于组原点)
  192.     //                         rx: 80,      // x轴半径(原矩形宽度的一半)
  193.     //                         ry: 20       // y轴半径(原矩形高度的一半)
  194.     //                     },
  195.     //                     style: {
  196.     //                         fill: 'rgba(141, 141, 25, 1)',  // 黄色填充
  197.     //                         stroke: 'rgba(0, 0, 0, 0.5)',
  198.     //                         borderRadius: 20  // 可选:添加圆角增强椭圆效果
  199.     //                     }
  200.     //                 } : {
  201.     //                     // 其他节点保持原矩形样式
  202.     //                     type: 'rect',
  203.     //                     z: 1000,
  204.     //                     shape: {
  205.     //                         x: -80, // 相对于组的偏移量
  206.     //                         y: -20,
  207.     //                         width: 160,
  208.     //                         height: 40,
  209.     //                     },
  210.     //                     style: {
  211.     //                         fill: 'rgba(80, 113, 184, 1)',
  212.     //                         stroke: 'rgba(0, 0, 0, 0.5)',
  213.     //                     }
  214.     //                 },
  215.     //                 // 文本元素(可选)
  216.     //                 {
  217.     //                     type: 'text',
  218.     //                     z: 1001,
  219.     //                     style: {
  220.     //                         text: (groupName || '未命名').length > 8 ?
  221.     //                             (groupName || '未命名').slice(0, 8) + '...' :
  222.     //                             (groupName || '未命名'),
  223.     //                         fontSize: 12,
  224.     //                         fill: '#fff',
  225.     //                         // 将textAlign和textVerticalAlign替换为正确的对齐属性
  226.     //                         align: 'center',         // 水平居中
  227.     //                         verticalAlign: 'middle'  // 垂直居中
  228.     //                     },
  229.     //                     // 文本元素定位(相对于组原点)
  230.     //                     left: -60,  // 组原点已在矩形中心
  231.     //                     top: -5,
  232.     //                 }
  233.     //             ]
  234.     //         };
  235.     //         graphic.push(graphicGroup);
  236.     //     });
  237.     //     myChart.chart.setOption({
  238.     //         graphic: graphic
  239.     //     });
  240.     // });
  241.     return myChart
  242. }
复制代码
效果图
1.png

整个项目的代码地址: dty/bigViewReact
重要概念

术语快查手册:术语速查手册
2.png

Option(配置项 → 就是一份“图表说明书”,告诉 ECharts 要画什么样的图。
Series(系列) → 决定画哪种图(柱状图/折线图/饼图)和用哪些数据。系列和维度是正交关系
Dataset(数据集) → 把原始数据统一放在一个表里,方便多个图表共享使用。只适合特定坐标系
Dimension(维度) → 数据表的“列”,决定某个字段用来当 x 轴、y 轴还是提示信息。
Coordinate System(坐标系) → 数据摆放的空间,比如直角坐标、极坐标、地图。
Component(组件) → 图表上的配件,比如标题、图例、提示框、缩放条。
Visual Encoding(视觉编码) → 数据转成图形的规则,比如数值大小对应柱子高度或颜色深浅。
Event & Action(事件与动作) → 图表的交互机制,你点一下图表,它能给你反馈或执行操作
不同图标的统计特性

图表类型统计特性适合场景柱状图 (Bar Chart)比较不同类别的数据大小各地区销量对比、部门业绩折线图 (Line Chart)数据随时间的趋势变化股票走势、网站流量、温度变化饼图 / 环形图 (Pie/Donut Chart)整体中各部分的占比市场份额、支出结构、人口分布散点图 (Scatter Plot)两个变量之间的相关性、分布学习时间 vs 成绩、广告费 vs 销售额雷达图 (Radar Chart)多维度指标对比运动员能力、公司竞争力堆叠图 (Stacked Bar/Line)整体及部分随时间的变化渠道销售额趋势、能源消耗结构热力图 (Heatmap)二维空间上的数值密度分布点击热区、时间-地点活动频率关系图 (Graph)节点之间的连接关系社交网络、知识图谱、网络拓扑地图 (Geo/Map)地理空间分布各省 GDP、疫情分布、物流覆盖箱线图 (Boxplot)数据分布特征(中位数、离散度、异常值)工资分布、考试成绩差异、实验数据分析
来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除

相关推荐

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