blob: 647146cd3833ef1df1f79fcfc03508c07d513f54 [file] [log] [blame]
(window.webpackJsonp=window.webpackJsonp||[]).push([[92],{386:function(n,t,e){"use strict";e.r(t),t.default="# Apache ECharts 5.2.0 介绍\n\n## 全局过渡动画\n\n在 Apache ECharts 中我们一直把自然流畅的过渡动画作为一个重要特性。通过避免数据带来的突变,不仅仅可以改善视觉效果,更为表达数据的关联和演变提供了可能。因此,在 5.2.0 中,我们进一步将过渡动画从表现系列内部数据的变化,泛化到全局能力。接下来,我们会看到这种**全局过渡动画 Universal Transition**是如何为图表增加表现力和叙事能力的。\n\n在之前的版本中,过渡动画有一定的局限性:只能用于相同类型的图形的位置、尺寸、形状,而且只能作用在相同类型的系列上。比如,下面例子就是通过饼图中扇区形状的变化反映了数据分布的变化:\n\n```js live {layout: 'lr'}\nfunction makeRandomData() {\n return [\n {\n value: Math.random(),\n name: 'A'\n },\n {\n value: Math.random(),\n name: 'B'\n },\n {\n value: Math.random(),\n name: 'C'\n }\n ];\n}\noption = {\n series: [\n {\n type: 'pie',\n radius: [0, '50%'],\n data: makeRandomData()\n }\n ]\n};\n\nsetInterval(() => {\n myChart.setOption({\n series: {\n data: makeRandomData()\n }\n });\n}, 2000);\n```\n\n而从 5.2.0 开始,我们引入了更强大的全局过渡动画能力,让过渡动画不再局限于相同类型的系列之间。现在,我们可以使用这种跨系列的形变,为任意类型的系列和任意类型的图形做形变动画。\n\n这会有多酷呢?我们一起来感受一下!\n\n### 跨系列的形变动画\n\n在设置`universalTransition: true`开启全局过渡动画后,从饼图切换成柱状图,或者从柱状图切换成散点图,甚至旭日图和矩形树图这类复杂的图表之间,都可以通过形变的方式自然的进行动画过渡。\n\n如下,饼图和柱状图之间的切换:\n\n```js live {layout: 'bt'}\nconst dataset = {\n dimensions: ['name', 'score'],\n source: [\n ['Hannah Krause', 314],\n ['Zhao Qian', 351],\n ['Jasmin Krause ', 287],\n ['Li Lei', 219],\n ['Karle Neumann', 253],\n ['Mia Neumann', 165],\n ['Böhm Fuchs', 318],\n ['Han Meimei', 366]\n ]\n};\nconst pieOption = {\n dataset: [dataset],\n series: [\n {\n type: 'pie',\n // 通过 id 关联需要过渡动画的系列\n id: 'Score',\n radius: [0, '50%'],\n universalTransition: true,\n animationDurationUpdate: 1000\n }\n ]\n};\nconst barOption = {\n dataset: [dataset],\n xAxis: {\n type: 'category'\n },\n yAxis: {},\n series: [\n {\n type: 'bar',\n // 通过 id 关联需要过渡动画的系列\n id: 'Score',\n // 每个数据都是用不同的颜色\n colorBy: 'data',\n encode: { x: 'name', y: 'score' },\n universalTransition: true,\n animationDurationUpdate: 1000\n }\n ]\n};\n\noption = barOption;\n\nsetInterval(() => {\n option = option === pieOption ? barOption : pieOption;\n // 使用 notMerge 的形式可以移除坐标轴\n myChart.setOption(option, true);\n}, 2000);\n```\n\n更多的常见基础图表之间的过渡:\n\n![](images/5-2-0/universal-transition.gif)\n\n这样的动画过渡不再仅仅局限于基础的折、柱、饼中,在柱状图和地图之间:\n\n![](images/5-2-0/universal-transition-2.gif)\n\n或者旭日图和矩形树图之间,甚至非常灵活的自定义系列之间都可以进行动画的过渡。\n\n![](images/5-2-0/universal-transition-3.gif)\n\n> 注意需要配置系列的 id 来保证需要动画过渡的系列之间能够一一对应。\n\n### 数据的分裂和合并动画\n\n除了常见的数据值的更新,有时候我们还会碰到数据的聚合、下钻等交互后的更新,这个时候我们就不能直接应用一对一的动画过渡,而需要使用更多像分裂、合并这样的动画效果,来通过正确的图形动画表达出数据的变换。\n\n为了能够表达数据之间可能存在的多对多联系,在 5.2.0 中我们新引入了一个数据组`groupId`的概念,我们可以通过 [series.dataGroupId](${optionPath}series-bar.dataGroupId) 设置整个系列所属的组,或者更细粒度的通过 [series.data.groupId](${optionPath}series-bar.dataGroupId) 设置每个数据所属的组。如果你使用了`dataset`管理数据则更方便了,可以使用`encode.itemGroupId`来指定一个维度编码成`groupId`。\n\n比如我们要实现一个柱状图下钻的动画,可以将下钻后的整个系列的数据都设置同一个`groupId`,然后跟下钻前的数据对应起来:\n\n```js live {layout: 'lr'}\noption = {\n xAxis: {\n data: ['Animals', 'Fruits', 'Cars']\n },\n yAxis: {},\n dataGroupId: '',\n animationDurationUpdate: 500,\n series: {\n type: 'bar',\n id: 'sales',\n data: [\n {\n value: 5,\n groupId: 'animals'\n },\n {\n value: 2,\n groupId: 'fruits'\n },\n {\n value: 4,\n groupId: 'cars'\n }\n ],\n universalTransition: {\n enabled: true,\n divideShape: 'clone'\n }\n }\n};\n\nconst drilldownData = [\n {\n dataGroupId: 'animals',\n data: [\n ['Cats', 4],\n ['Dogs', 2],\n ['Cows', 1],\n ['Sheep', 2],\n ['Pigs', 1]\n ]\n },\n {\n dataGroupId: 'fruits',\n data: [\n ['Apples', 4],\n ['Oranges', 2]\n ]\n },\n {\n dataGroupId: 'cars',\n data: [\n ['Toyota', 4],\n ['Opel', 2],\n ['Volkswagen', 2]\n ]\n }\n];\n\nmyChart.on('click', event => {\n if (event.data) {\n const subData = drilldownData.find(data => {\n return data.dataGroupId === event.data.groupId;\n });\n if (!subData) {\n return;\n }\n myChart.setOption({\n xAxis: {\n data: subData.data.map(item => {\n return item[0];\n })\n },\n series: {\n type: 'bar',\n id: 'sales',\n dataGroupId: subData.dataGroupId,\n data: subData.data.map(item => {\n return item[1];\n }),\n universalTransition: {\n enabled: true,\n divideShape: 'clone'\n }\n },\n graphic: [\n {\n type: 'text',\n left: 50,\n top: 20,\n style: {\n text: 'Back',\n fontSize: 18\n },\n onclick: function() {\n myChart.setOption(option, true);\n }\n }\n ]\n });\n }\n});\n```\n\n通过`groupId`,我们还可以实现更丰富的聚合,下钻动画。\n\n数据的聚合:\n\n![](images/5-2-0/group-transition.gif)\n\n单系列下钻成两个系列:\n\n![](images/5-2-0/group-transition-2.gif)\n\n全局过渡动画使得数据的关系和演变的表达能力走上新的台阶,为你的可视化创作灵感插上翅膀。\n\n看到这里,我们知道你已经跃跃欲试了。但是别急,5.2.0 还有更多值得一看的新功能。\n\n## 调色盘的取色策略\n\n在上面全局过渡动画的示例中,大家可能有注意到我们使用了一个之前版本没有的`colorBy`配置项,这个配置项也是我们这个版本新增加的一个特性,用来给系列配置不同粒度的调色盘取色。这个配置目前支持两种策略:\n\n- `'series'` 按照系列分配调色盘中的颜色,同一系列中的所有数据都是用相同的颜色。\n- `'data'` 按照数据项分配调色盘中的颜色,每个数据项都使用不同的颜色。\n\n在之前我们是按照系列的类型固定了这个策略,比如柱状图就是固定`'series'`的策略,而饼图则是固定`'data'`的策略。\n\n而现在新增这个配置项后,我们可以在柱状图中给每个数据项都分配不同的颜色:\n\n```js live {layout: 'lr'}\noption = {\n xAxis: {\n type: 'category',\n data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']\n },\n yAxis: {\n type: 'value'\n },\n series: [\n {\n data: [120, 200, 150, 80, 70, 110, 130],\n type: 'bar',\n colorBy: 'data'\n }\n ]\n};\n```\n\n或者在饼图中统一使用一个颜色:\n\n```js live {layout: 'lr'}\noption = {\n series: {\n type: 'pie',\n colorBy: 'series',\n radius: [0, '50%'],\n itemStyle: {\n borderColor: '#fff',\n borderWidth: 1\n },\n data: [\n {\n value: 335,\n name: 'Direct Visit'\n },\n {\n value: 234,\n name: 'Union Ad'\n },\n {\n value: 1548,\n name: 'Search Engine'\n }\n ]\n }\n};\n```\n\n这一配置项可以让我们避免了去找调色盘颜色然去一一设置的麻烦,可能在特定的场景需求中提供便捷。后续我们会对这个配置项做进一步的增强,提供更多的调色盘取色的策略。\n\n## 极坐标柱状图的标签\n\n这个版本中我们实现了极坐标上的柱状图的标签,并且支持丰富的标签定位配置。下面是一个最常见的标签显示在端点的进度图:\n\n```js live {layout: 'lr'}\noption = {\n angleAxis: {\n show: false,\n max: 10\n },\n radiusAxis: {\n show: false,\n type: 'category',\n data: ['AAA', 'BBB', 'CCC', 'DDD']\n },\n polar: {},\n series: [\n {\n type: 'bar',\n data: [3, 4, 5, 6],\n colorBy: 'data',\n roundCap: true,\n label: {\n show: true,\n // 试试改成 'insideStart'\n position: 'start',\n formatter: '{b}'\n },\n coordinateSystem: 'polar'\n }\n ]\n};\n```\n\n更多标签位置的配置:\n\n![](images/5-2-0/polar-bar-label.jpg)\n\n这一灵活的标签位置配置项大大丰富了极坐标柱状图的表达能力,让文字清晰地匹配对应的数据。\n\n## 空数据的饼图样式\n\n在之前的版本中,如果饼图没有数据,画面中可能就是完全空白的。因为没有任何的视觉元素,所以用户会疑惑是不是出现了 bug 导致图中没有内容。\n\n为了解决这个问题,这个版本我们会默认在无可显示数据的时候显示一个灰色的占位圆以防止画面中完全空白。我们可以通过`emptyCircleStyle`配置这个占位圆的样式。\n\n```js live {layout: 'lr'}\noption = {\n series: [\n {\n type: 'pie',\n data: [],\n // showEmptyCircle: false,\n emptyCircleStyle: {\n // 将样式改为空心圆\n color: 'transparent',\n borderColor: '#ddd',\n borderWidth: 1\n }\n }\n ]\n};\n```\n\n如果不想要显示这个灰色的圆,也可以设置`showEmptyCircle: false`关闭。\n\n## 高维数据的性能增强\n\n我们从 4.0 开始引入了 [dataset](${optionPath}dataset) 用来管理图表的数据,通常情况下`dataset`提供了更方便的数据管理方式而且跟传统的方式不会有什么性能上的差别。但是在一些极端的特别高维(>100)数据的场景下,我们还是会碰到一些性能急剧下降的问题,比如下面这种通过一千个系列去可视化千维数据的场景([#11907](https://github.com/apache/echarts/issues/11907)),甚至可能会卡住。\n\n```js\nconst indices = Array.from(Array(1000), (_, i) => {\n return `index${i}`;\n});\nconst option = {\n xAxis: { type: 'category' },\n yAxis: {},\n dataset: {\n // dimension: ['date', ...indices],\n source: Array.from(Array(10), (_, i) => {\n return {\n date: i,\n ...indices.reduce((item, next) => {\n item[next] = Math.random() * 100;\n return item;\n }, {})\n };\n })\n },\n series: indices.map(index => {\n return { type: 'line', name: index };\n })\n};\n```\n\n产生这个性能问题是因为我们在底层每个系列都会按照其需要处理一遍这个 dataset,然后保存一份处理过后的数据以及维度等元信息。这意味着刚才那个例子中需要处理并保存`1000 x 1000`个维度的信息,这带来了巨大的内存和垃圾回收的压力,从而导致了高维度的性能的急剧下降。\n\n在新版本中我们对这个问题做了优化,所有系列都尽可能共享 dataset 的数据(能否共享取决于系列怎么使用这份数据)存储而非每个系列都处理并存储一次,并且只处理和存储了使用到的维度。这些优化保证了内存不会随着 dataset 维度和系列的增长而爆炸,大幅度的提升了极端场景下的初始化性能。刚才例子的渲染耗时也从卡住降低到了可接受的 300 毫秒以下。\n\n这次优化带来收益的还不只是这种高维的场景,在使用维度不高但是数据量很大的 dataset 的时候,因为数据的共享所以多个系列只处理了一遍数据,因此也可以带来显著的性能提升。\n\n## 自定义系列的类型优化\n\n自定义系列提供了非常灵活的创建系列图形的方式,相对于其它系列,自定义系列的学习曲线可能有些陡峭,而且容易出错。因此在这个版本中,我们进一步的优化了自定义系列中的核心方法`renderItem`的类型,对于`renderItem`的参数和返回值类型做了更精确的推断,从而可以根据返回的图形类型推断出可以设置该图形的哪些属性:\n\n```ts\nseries = {\n type: 'custom',\n renderItem(params) {\n return {\n type: 'group',\n // group 类型使用 children 存储其它类型的子元素\n children: [\n {\n type: 'circle',\n // circle 拥有下面这些可以配置的 shape 属性\n shape: { r: 10, cx: 0, cy: 0 },\n // 可以配置的样式\n style: { fill: 'red' }\n },\n {\n type: 'rect',\n // rect 拥有下面这些可以配置的 shape 属性\n shape: { x: 0, y: 0, width: 100, height: 100 }\n },\n {\n type: 'path',\n // 自定义路径图形\n shape: { d: '...' }\n }\n ]\n };\n }\n};\n```\n\n## 小结\n\n以上我们介绍了 5.2.0 中的新特性以及优化,如果你对其中的一些特性感兴趣,不妨更新到最新版本的 Apache ECharts 亲自体验一下。\n\n如果你对 Apache ECharts 接下来的计划感兴趣,也可以在 [GitHub Milestone](https://github.com/apache/echarts/milestones) 关注我们的开发计划。也非常欢迎加入我们的贡献者行列(在 [Wiki](https://github.com/apache/echarts/wiki) 中了解更多)。\n\n### 版本更新记录\n\n#### 非兼容改动\n\n- [Fix][pie] 负值会被作为非法值过滤。[#15095](https://github.com/apache/echarts/issues/15095) ([ssthouse](https://github.com/ssthouse))\n\n#### 所有改动\n\n- **[Feature] 新增全局过渡动画。[#15208](https://github.com/apache/echarts/issues/15208) ([pissang](https://github.com/pissang))**\n- [Feature][color] 新增`series.colorBy`配置不同粒度的取色。[#13788](https://github.com/apache/echarts/issues/13788) ([Ovilia](https://github.com/Ovilia))\n- [Feature][label] 极坐标系柱状图支持标签显示。[#15182](https://github.com/apache/echarts/pull/15182) ([Ovilia](https://github.com/Ovilia))\n- [Feature][effectscatter] 新增`rippleEffect.number`配置涟漪数目。[#15335](https://github.com/apache/echarts/issues/15335) ([plainheart](https://github.com/plainheart))\n- [Feature][gauge] 新增`pointer.showAbove`配置指针和标签的显示层级。[#15337](https://github.com/apache/echarts/issues/15337) ([AmosChenYQ](https://github.com/AmosChenYQ)) [#15326](https://github.com/apache/echarts/issues/15326) ([susiwen8](https://github.com/susiwen8))\n- [Feature][emphasis] `emphasis.color`支持设置为`'inherit'`关闭高亮。[#15172](https://github.com/apache/echarts/issues/15172) ([Foreverwzh](https://github.com/Foreverwzh))\n- [Feature][pie] 无数据的时候默认显示灰色的占位圆。[#15095](https://github.com/apache/echarts/issues/15095) ([ssthouse](https://github.com/ssthouse))\n- [Fix][dataset] 优化高维数据`dataset`的性能。[#15355](https://github.com/apache/echarts/issues/15355) ([pissang](https://github.com/pissang))\n- [Fix][axis] 优化时间轴刻度标签的格式化显示。[#15465](https://github.com/apache/echarts/issues/15465) ([leavest](https://github.com/leavest)) [#15434](https://github.com/apache/echarts/issues/15434) ([zhiyuc123](https://github.com/zhiyuc123))\n- [Fix][custom] 优化旧代码对于`font`的兼容性。[#15454](https://github.com/apache/echarts/issues/15454) ([AmosChenYQ](https://github.com/AmosChenYQ))\n- [Fix][memory] 优化实例销毁后依旧持有实例时的内存占用。[#15417](https://github.com/apache/echarts/issues/15417) ([pissang](https://github.com/pissang))\n- [Fix][line] 优化有无穷大数据时的渐变色显示。 [#15416](https://github.com/apache/echarts/issues/15416) ([plainheart](https://github.com/plainheart))\n- [Fix][date] 优化`date`数据的解析。[#15410](https://github.com/apache/echarts/issues/15410) ([quillblue](https://github.com/quillblue))\n- [Fix][line] 修复渲染出错。[#788](https://github.com/ecomfe/zrender/issues/788) ([pissang](https://github.com/pissang))\n- [Fix][candlestick] 修复样式可能在`setOption`后丢失的问题。[#15368](https://github.com/apache/echarts/issues/15368) ([pissang](https://github.com/pissang))\n- [Fix][sankey] 修复垂直布局时的渐变色边。[#15363](https://github.com/apache/echarts/issues/15363) ([susiwen8](https://github.com/susiwen8))\n- [Fix][tooltip] 修复在设置`tooltip.position`后`formatter`返回 DOM 对象会被解析成字符串的问题。[#15313](https://github.com/apache/echarts/issues/15313) ([plainheart](https://github.com/plainheart))\n- [Fix][tooltip] `formatter`返回`null`时清空内容。[#15313](https://github.com/apache/echarts/issues/15313) ([plainheart](https://github.com/plainheart))\n- [Fix][bar] 标签位置设置为`'middle'`时应该显示在图形中间。[#15309](https://github.com/apache/echarts/issues/15309) ([Ovilia](https://github.com/Ovilia))\n- [Fix][marker] 修复可能会在极坐标柱状图报`'clampData' is undefined`的错误。[#15297](https://github.com/apache/echarts/issues/15297) ([AmosChenYQ](https://github.com/AmosChenYQ))\n- [Fix][treemap] 修复关闭动画后更新可能旧节点不会被移除的问题。[#15283](https://github.com/apache/echarts/issues/15283) ([villebro](https://github.com/villebro))\n- [Fix][tree] 修复更新数据时边可能会不被移除的问题。[#15251](https://github.com/apache/echarts/issues/15251) ([ssthouse](https://github.com/ssthouse))\n- [Fix][pie/sunburst] 修复`borderRadius`被设置为`null`或者`undefined`时无法重置的问题。[#15243](https://github.com/apache/echarts/issues/15243) ([plainheart](https://github.com/plainheart))\n- [Fix][canvas] 修复`fillStyle`被设置为`'none'`或者`null`时 FireFox 浏览器下会报警告的问题。 [#784](https://github.com/ecomfe/zrender/issues/784) ([plainheart](https://github.com/plainheart))\n- [Fix][highlight] 修复`chart.dispatchAction`高亮多个系列可能会不正确的问题。[#15207](https://github.com/apache/echarts/issues/15207) ([ssthouse](https://github.com/ssthouse))\n- [Fix][sankey] 修复使用`series.nodes`作为数据时拖拽功能失效的问题。[#15199](https://github.com/apache/echarts/issues/15199) ([DuLinRain](https://github.com/DuLinRain))\n- [Fix][svg] 优化导出的 SVG 文件在 Powerpoint 中的兼容性。[#767](https://github.com/ecomfe/zrender/pull/767) ([plainheart](https://github.com/plainheart))\n- [Fix][legend] 修复`text.lineHeight`不生效。[#773](https://github.com/ecomfe/zrender/issues/773) ([ssthouse](https://github.com/ssthouse))\n- [Fix][pie] 将默认的`borderJoin`设置为`round`。[#15145](https://github.com/apache/echarts/issues/15145) ([plainheart](https://github.com/plainheart))\n- [Fix][radar] 将默认的`borderJoin`设置为`round`。[#15381](https://github.com/apache/echarts/issues/15381) ([Ovilia](https://github.com/Ovilia))\n- [Fix][treemap] 修复设置`label.show`为`false`会报错。[#15141](https://github.com/apache/echarts/issues/15141) ([susiwen8](https://github.com/susiwen8))\n- [Fix][pictorialbar] 修复零数据标签的显示问题。[#15132](https://github.com/apache/echarts/issues/15132) ([ssthouse](https://github.com/ssthouse))\n- [Fix][lines] 修复调用`chart.clear()`可能会无法清除线条的问题。[#15088](https://github.com/apache/echarts/issues/15088) ([plainheart](https://github.com/plainheart))\n- [Fix][endlabel] 修复端点标签只设置`emphasis.show`为`true`时可能无法显示的问题。[#15072](https://github.com/apache/echarts/issues/15072) ([Ovilia](https://github.com/Ovilia))\n- [Fix][svg] 修复矩形路径没有合并的问题。[#767](https://github.com/ecomfe/zrender/issues/767) ([plainheart](https://github.com/plainheart))\n- [Fix][treemap] 在回调函数参数中添加`treeAncestors`属性。[#14976](https://github.com/apache/echarts/issues/14976) ([pissang](https://github.com/pissang))\n- [Fix][tree] 修复调用`setOption`两次更新数据时可能报错的问题。[#14930](https://github.com/apache/echarts/issues/14930) ([Map1en](https://github.com/Map1en))\n- [Fix][radar] 修复图形边框被缩放的问题。[#15396](https://github.com/apache/echarts/issues/15396) ([pissang](https://github.com/pissang))\n- [Fix][marker] 修复`symbolOffset`和`symbolKeepAspect`配置项不生效的问题。[#14737](https://github.com/apache/echarts/issues/14737) ([plainheart](https://github.com/plainheart))\n- [Fix][gauge] 支持进度条和指针的点击事件。[#14688](https://github.com/apache/echarts/issues/14688) ([yufeng04](https://github.com/yufeng04))\n- [Fix][tooltip] 优化箭头的边框宽度,跟配置同步。[#14393](https://github.com/apache/echarts/issues/14393) ([g7i](https://github.com/g7i))\n- [Fix][geo] 修复地理坐标组件从`show: false`配置为`show: true`后依旧不显示的问题。[#15361](https://github.com/apache/echarts/issues/15361) ([pissang](https://github.com/pissang))\n- [Fix][type] 优化自定义系列`renderItem`的类型推断。\n- [Fix][type] 优化`echarts.init`的配置项类型。[#15487](https://github.com/apache/echarts/issues/15487) ([John60676](https://github.com/John60676))\n- [Fix][type] 修复类型中`polarIndex`配置项丢失的问题。[#15281](https://github.com/apache/echarts/issues/15281) ([Map1en](https://github.com/Map1en))\n- [Fix][type] 优化 SVG 数据源的类型。[#15263](https://github.com/apache/echarts/issues/15263) ([leosxie](https://github.com/leosxie))\n- [Fix][type] 优化饼图和地图系列中的数据类型。[#15144](https://github.com/apache/echarts/issues/15144) ([plainheart](https://github.com/plainheart))\n"}}]);