当前位置:   article > 正文

echarts3d饼图实现_echarts 3d饼图

echarts 3d饼图

一、vue中使用3d饼图

效果图:

二、使用步骤 

1.引入库

安装echarts

package.json文件中添加

  1. "dependencies": {
  2. "echarts": "^5.1.2"
  3. "echarts-gl": "^1.1.0",
  4. // "echarts-gl": "^2.0.8"
  5. },
npm install echarts-gl@1.1.0 --save

2.使用

完整代码如下(示例):

HTML代码
  1. <template>
  2. <div :class="className" :style="{height:height,width:width,backgroundImage:'url(' + require('@/assets/bigScreen/deviceBg.png') + ')'}"/>
  3. </template>
js脚本代码
  1. <script>
  2. import "echarts-gl";
  3. import echarts from 'echarts'
  4. require('echarts/theme/macarons') // echarts theme
  5. export default {
  6. props: {
  7. className: {
  8. type: String,
  9. default: 'chart'
  10. },
  11. width: {
  12. type: String,
  13. default: '50%'
  14. },
  15. height: {
  16. type: String,
  17. default: '300px'
  18. },
  19. optionTest:{}
  20. },
  21. data() {
  22. return {
  23. myChart: null,
  24. seriesData: [],
  25. };
  26. },
  27. mounted() {
  28. this.getList();
  29. window.addEventListener("resize", () => {
  30. this.reloadChart();
  31. });
  32. },
  33. methods: {
  34. getList() {
  35. this.seriesData = [
  36. {
  37. name: "canshu1",
  38. value: 58,
  39. },
  40. {
  41. name: "canshu2",
  42. value: 3,
  43. },
  44. {
  45. name: "canshu3",
  46. value: 15,
  47. },
  48. {
  49. name: "canshu4",
  50. value: 30,
  51. },
  52. ];
  53. this.initChart(this.seriesData);
  54. },
  55. initChart(data) {
  56. if (data.length == 0) return;
  57. // this.myChart = this.$echarts.init(this.$refs.myChart);
  58. this.myChart = echarts.init(this.$el, 'macarons')
  59. let colors = [
  60. "RGBA(36, 154, 163, 1)",
  61. "RGBA(245, 169, 64, 1)",
  62. "RGBA(240, 136, 64, 1)",
  63. "RGBA(123, 198, 239, 1)",
  64. "RGBA(245, 169, 64, 1)",
  65. "RGBA(240, 136, 64, 1)",
  66. "RGBA(123, 198, 239, 1)",
  67. ];
  68. const getPie3D = (pieData, internalDiameterRatio) => {
  69. //internalDiameterRatio:透明的空心占比
  70. let series = [];
  71. let sumValue = 0;
  72. let startValue = 0;
  73. let endValue = 0;
  74. let k = 1 - internalDiameterRatio;
  75. pieData.sort((a, b) => {
  76. return b.value - a.value;
  77. });
  78. // 为每一个饼图数据,生成一个 series-surface 配置
  79. for (let i = 0; i < pieData.length; i++) {
  80. sumValue += pieData[i].value;
  81. let seriesItem = {
  82. name:
  83. typeof pieData[i].name === "undefined"
  84. ? `series${i}`
  85. : pieData[i].name,
  86. type: "surface",
  87. parametric: true,
  88. wireframe: {
  89. show: false,
  90. },
  91. pieData: pieData[i],
  92. pieStatus: {
  93. selected: false,
  94. hovered: false,
  95. k: k,
  96. },
  97. radius: "20%",
  98. center: ["10%", "20%"],
  99. };
  100. if (typeof colors[i] != "undefined") {
  101. let itemStyle = {};
  102. typeof colors[i] != "undefined"
  103. ? (itemStyle.color = colors[i])
  104. : null;
  105. seriesItem.itemStyle = itemStyle;
  106. }
  107. series.push(seriesItem);
  108. }
  109. // 使用上一次遍历时,计算出的数据和 sumValue,调用 getParametricEquation 函数,
  110. // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation,也就是实现每一个扇形。
  111. for (let i = 0; i < series.length; i++) {
  112. endValue = startValue + series[i].pieData.value;
  113. series[i].pieData.startRatio = startValue / sumValue;
  114. series[i].pieData.endRatio = endValue / sumValue;
  115. series[i].parametricEquation = getParametricEquation(
  116. series[i].pieData.startRatio,
  117. series[i].pieData.endRatio,
  118. false,
  119. false,
  120. k,
  121. series[i].pieData.value
  122. );
  123. startValue = endValue;
  124. }
  125. // let boxHeight = getHeight3D(series, 15); //通过传参设定3d饼/环的高度,26代表26px
  126. // 准备待返回的配置项,把准备好的 legendData、series 传入。
  127. let option = {
  128. labelLine: {
  129. show: true,
  130. lineStyle: {
  131. color: "#7BC0CB",
  132. },
  133. },
  134. legend: {
  135. orient: "vertical",
  136. top: "center",
  137. right: this.fontSize(0.1),
  138. data: data,
  139. icon: "circle",
  140. itemWidth: this.fontSize(0.13), //矩形宽度
  141. itemHeight: this.fontSize(0.13), //矩形高度
  142. formatter: (res) => {
  143. let str = "";
  144. series.forEach((ele) => {
  145. if (res == ele.name) {
  146. let bfb = (
  147. (ele.pieData.endRatio - ele.pieData.startRatio) *
  148. 100
  149. ).toFixed(2);
  150. if (bfb == "NaN") {
  151. str =
  152. "{e|" +
  153. ele.pieData.name +
  154. "}" +
  155. "{a|" +
  156. ele.pieData.value +
  157. " " +
  158. 0 +
  159. "%}";
  160. } else {
  161. str =
  162. "{e|" +
  163. ele.pieData.name +
  164. "} " +
  165. "{a| " +
  166. ele.pieData.value +
  167. "}{b| " +
  168. bfb +
  169. "%}";
  170. }
  171. }
  172. });
  173. return str;
  174. },
  175. textStyle: {
  176. color: "#fff",
  177. fontSize: this.fontSize(0.35),
  178. padding: [5, 0],
  179. rich: {
  180. a: {
  181. fontSize: this.fontSize(0.35),
  182. color: "#00DCF6",
  183. // width: this.fontSize(0.5),
  184. },
  185. b: {
  186. fontSize: this.fontSize(0.35),
  187. color: "#00DCF6",
  188. // width: this.fontSize(0.85),
  189. },
  190. e: {
  191. fontSize: this.fontSize(0.35),
  192. // width: this.fontSize(1.0),
  193. },
  194. },
  195. },
  196. },
  197. label: {
  198. show: false,
  199. formatter: "",
  200. },
  201. tooltip: {
  202. show: true,
  203. formatter: (params) => {
  204. if (
  205. params.seriesName !== "mouseoutSeries" &&
  206. params.seriesName !== "pie2d"
  207. ) {
  208. let bfb = (
  209. (option.series[params.seriesIndex].pieData.endRatio -
  210. option.series[params.seriesIndex].pieData.startRatio) *
  211. 100
  212. ).toFixed(2);
  213. return (
  214. `${params.seriesName}<br/>` +
  215. `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>` +
  216. `${bfb}%`
  217. );
  218. }
  219. },
  220. },
  221. xAxis3D: {
  222. min: -1,
  223. max: 1,
  224. },
  225. yAxis3D: {
  226. min: -1,
  227. max: 1,
  228. },
  229. zAxis3D: {
  230. min: -1,
  231. max: 1,
  232. },
  233. grid3D: {
  234. show: false,
  235. boxHeight: 10, //圆环的高度
  236. left: -this.fontSize(2.2),
  237. top: -this.fontSize(0.1), //3d饼图的位置
  238. viewControl: {
  239. //3d效果可以放大、旋转等,请自己去查看官方配置
  240. alpha: 25, //角度
  241. distance: 200, //调整视角到主体的距离,类似调整zoom
  242. rotateSensitivity: 0, //设置为0无法旋转
  243. zoomSensitivity: 0, //设置为0无法缩放
  244. panSensitivity: 0, //设置为0无法平移
  245. autoRotate: false, //自动旋转
  246. },
  247. },
  248. series: series,
  249. };
  250. return option;
  251. };
  252. //获取3d丙图的最高扇区的高度
  253. // const getHeight3D = (series, height) => {
  254. // series.sort((a, b) => {
  255. // return b.pieData.value - a.pieData.value;
  256. // });
  257. // return (height * this.fontSize(1)) / series[0].pieData.value;
  258. // };
  259. // 生成扇形的曲面参数方程,用于 series-surface.parametricEquation
  260. const getParametricEquation = (
  261. startRatio,
  262. endRatio,
  263. isSelected,
  264. isHovered,
  265. k,
  266. h
  267. ) => {
  268. // 计算
  269. let midRatio = (startRatio + endRatio) / 2;
  270. let startRadian = startRatio * Math.PI * 2;
  271. let endRadian = endRatio * Math.PI * 2;
  272. let midRadian = midRatio * Math.PI * 2;
  273. // 如果只有一个扇形,则不实现选中效果。
  274. if (startRatio === 0 && endRatio === 1) {
  275. isSelected = false;
  276. }
  277. // 通过扇形内径/外径的值,换算出辅助参数 k(默认值 1/3)
  278. k = typeof k !== "undefined" ? k : 1 / 3;
  279. // 计算选中效果分别在 x 轴、y 轴方向上的位移(未选中,则位移均为 0)
  280. let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  281. let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  282. // 计算高亮效果的放大比例(未高亮,则比例为 1)
  283. let hoverRate = isHovered ? 1.05 : 1;
  284. // 返回曲面参数方程
  285. return {
  286. u: {
  287. min: -Math.PI,
  288. max: Math.PI * 3,
  289. step: Math.PI / 32,
  290. },
  291. v: {
  292. min: 0,
  293. max: Math.PI * 2,
  294. step: Math.PI / 20,
  295. },
  296. x: function(u, v) {
  297. if (u < startRadian) {
  298. return (
  299. offsetX +
  300. Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  301. );
  302. }
  303. if (u > endRadian) {
  304. return (
  305. offsetX +
  306. Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  307. );
  308. }
  309. return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
  310. },
  311. y: function(u, v) {
  312. if (u < startRadian) {
  313. return (
  314. offsetY +
  315. Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate
  316. );
  317. }
  318. if (u > endRadian) {
  319. return (
  320. offsetY +
  321. Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate
  322. );
  323. }
  324. return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
  325. },
  326. z: function(u, v) {
  327. if (u < -Math.PI * 0.5) {
  328. return Math.sin(u);
  329. }
  330. if (u > Math.PI * 2.5) {
  331. return Math.sin(u) * h * 0.1;
  332. }
  333. return Math.sin(v) > 0 ? 1 * h * 0.1 : -1;
  334. },
  335. };
  336. };
  337. let option = getPie3D(data, 0.8);
  338. //是否需要label指引线,如果要就添加一个透明的2d饼状图并调整角度使得labelLine和3d的饼状图对齐,并再次setOption
  339. option.series.push({
  340. name: "pie2d",
  341. type: "pie",
  342. labelLine: {
  343. show: false,
  344. length: 15,
  345. length2: 15,
  346. },
  347. startAngle: -this.fontSize(0.3), //起始角度,支持范围[0, 360]。
  348. clockwise: false, //饼图的扇区是否是顺时针排布。上述这两项配置主要是为了对齐3d的样式
  349. radius: [this.fontSize(1.3) + "%", this.fontSize(1.4) + "%"],
  350. center: [this.fontSize(1.5) + "%", this.fontSize(1.8) + "%"], //指示线的位置
  351. data: data,
  352. itemStyle: {
  353. opacity: 0,
  354. },
  355. });
  356. this.myChart.setOption(option);
  357. },
  358. // 字体自适应。
  359. fontSize(res) {
  360. const clientWidth =
  361. window.innerWidth ||
  362. document.documentElement.clientWidth ||
  363. document.body.clientWidth;
  364. if (!clientWidth) return;
  365. const fontSize = 40 * (clientWidth / 1920);
  366. return res * fontSize;
  367. },
  368. },
  369. //重新加载图表
  370. reloadChart() {
  371. this.disposeChart();
  372. this.initChart(this.seriesData);
  373. },
  374. //销毁图表以及重置各个数据
  375. disposeChart() {
  376. if (this.myChart) {
  377. this.myChart.dispose();
  378. }
  379. },
  380. beforeDestroy() {
  381. this.disposeChart();
  382. },
  383. };
  384. </script>

三、会旋转的3D饼图

  1. <template>
  2. <div class="chart" ref="myLineChart" id="charts" :style="{height:height,width:width,border:border,backgroundImage:'url(' + require('@/assets/images/chart_opacity_bg.png') + ')',backgroundRepeat:'no-repeat',backgroundPosition:'center center'}"></div>
  3. </div>
  4. <!-- 饼图 -->
  5. <!-- <div class="container">
  6. <div class="chartsGl" id="charts"></div>
  7. <div class="buttomCharts"></div>
  8. </div> -->
  9. </template>
  10. <script>
  11. import "echarts-gl";
  12. import echarts from 'echarts'
  13. require('echarts/theme/macarons') // echarts theme
  14. export default {
  15. props: {
  16. className: {
  17. type: String,
  18. default: 'chart'
  19. },
  20. width: {
  21. type: String,
  22. default: '30%'
  23. },
  24. height: {
  25. type: String,
  26. default: '300px'
  27. },
  28. border: {
  29. type: String,
  30. default: '1px solid red'
  31. },
  32. optionTest:{}
  33. },
  34. data() {
  35. return {
  36. myChart: null,
  37. optionData:[
  38. {
  39. "name": "病区四",
  40. "value": 80,
  41. },
  42. {
  43. "name": "病区一",
  44. "value": 200,
  45. },
  46. {
  47. "name": "病区三",
  48. "value": 20,
  49. },
  50. {
  51. "name": "病区二",
  52. "value": 400,
  53. },
  54. {
  55. "name": "病区五",
  56. "value": 500,
  57. },
  58. {
  59. "name": "病区66",
  60. "value": 590,
  61. }
  62. ]
  63. }
  64. },
  65. mounted() {
  66. this.init()
  67. },
  68. methods: {
  69. //初始化构建
  70. init() {
  71. this.myChart = echarts.init(this.$el, 'macarons')
  72. // this.myChart = echarts.init(document.getElementById('charts'));
  73. // 传入数据生成 option ; getPie3D(数据,透明的空心占比(调节中间空心范围的0就是普通饼1就很镂空))
  74. this.option = this.getPie3D(this.optionData, 0.85);
  75. //将配置项设置进去
  76. this.myChart.setOption(this.option);
  77. //鼠标移动上去特效效果
  78. this.bindListen(this.myChart);
  79. },
  80. // 监听鼠标事件,实现饼图选中效果(单选),近似实现高亮(放大)效果。
  81. bindListen(myChart) {
  82. let that = this;
  83. let selectedIndex = '';
  84. let hoveredIndex = '';
  85. // 监听点击事件,实现选中效果(单选)
  86. myChart.on('click', function (params) {
  87. // 从 option.series 中读取重新渲染扇形所需的参数,将是否选中取反。
  88. let isSelected = !that.option.series[params.seriesIndex].pieStatus.selected;
  89. let isHovered = that.option.series[params.seriesIndex].pieStatus.hovered;
  90. let k = that.option.series[params.seriesIndex].pieStatus.k;
  91. let startRatio = that.option.series[params.seriesIndex].pieData.startRatio;
  92. let endRatio = that.option.series[params.seriesIndex].pieData.endRatio;
  93. // 如果之前选中过其他扇形,将其取消选中(对 option 更新)
  94. if (selectedIndex !== '' && selectedIndex !== params.seriesIndex) {
  95. that.option.series[selectedIndex].parametricEquation = that.getParametricEquation(that.option.series[
  96. selectedIndex].pieData
  97. .startRatio, that.option.series[selectedIndex].pieData.endRatio, false, false, k, that.option.series[
  98. selectedIndex].pieData
  99. .value);
  100. that.option.series[selectedIndex].pieStatus.selected = false;
  101. }
  102. // 对当前点击的扇形,执行选中/取消选中操作(对 option 更新)
  103. that.option.series[params.seriesIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio,
  104. isSelected,
  105. isHovered, k, that.option.series[params.seriesIndex].pieData.value);
  106. that.option.series[params.seriesIndex].pieStatus.selected = isSelected;
  107. // 如果本次是选中操作,记录上次选中的扇形对应的系列号 seriesIndex
  108. isSelected ? selectedIndex = params.seriesIndex : null;
  109. // 使用更新后的 option,渲染图表
  110. myChart.setOption(that.option);
  111. });
  112. // 监听 mouseover,近似实现高亮(放大)效果
  113. myChart.on('mouseover', function (params) {
  114. // 准备重新渲染扇形所需的参数
  115. let isSelected;
  116. let isHovered;
  117. let startRatio;
  118. let endRatio;
  119. let k;
  120. // 如果触发 mouseover 的扇形当前已高亮,则不做操作
  121. if (hoveredIndex === params.seriesIndex) {
  122. return;
  123. // 否则进行高亮及必要的取消高亮操作
  124. } else {
  125. // 如果当前有高亮的扇形,取消其高亮状态(对 option 更新)
  126. if (hoveredIndex !== '') {
  127. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 false。
  128. isSelected = that.option.series[hoveredIndex].pieStatus.selected;
  129. isHovered = false;
  130. startRatio = that.option.series[hoveredIndex].pieData.startRatio;
  131. endRatio = that.option.series[hoveredIndex].pieData.endRatio;
  132. k = that.option.series[hoveredIndex].pieStatus.k;
  133. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  134. that.option.series[hoveredIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio,
  135. isSelected,
  136. isHovered, k, that.option.series[hoveredIndex].pieData.value);
  137. that.option.series[hoveredIndex].pieStatus.hovered = isHovered;
  138. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  139. hoveredIndex = '';
  140. }
  141. // 如果触发 mouseover 的扇形不是透明圆环,将其高亮(对 option 更新)
  142. if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') {
  143. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true。
  144. isSelected = that.option.series[params.seriesIndex].pieStatus.selected;
  145. isHovered = true;
  146. startRatio = that.option.series[params.seriesIndex].pieData.startRatio;
  147. endRatio = that.option.series[params.seriesIndex].pieData.endRatio;
  148. k = that.option.series[params.seriesIndex].pieStatus.k;
  149. // 对当前点击的扇形,执行高亮操作(对 option 更新)
  150. that.option.series[params.seriesIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio,
  151. isSelected, isHovered, k, that.option.series[params.seriesIndex].pieData.value + 5);
  152. that.option.series[params.seriesIndex].pieStatus.hovered = isHovered;
  153. // 记录上次高亮的扇形对应的系列号 seriesIndex
  154. hoveredIndex = params.seriesIndex;
  155. }
  156. // 使用更新后的 option,渲染图表
  157. myChart.setOption(that.option);
  158. }
  159. });
  160. // 修正取消高亮失败的 bug
  161. myChart.on('globalout', function () {
  162. // 准备重新渲染扇形所需的参数
  163. let isSelected;
  164. let isHovered;
  165. let startRatio;
  166. let endRatio;
  167. let k;
  168. if (hoveredIndex !== '') {
  169. // 从 option.series 中读取重新渲染扇形所需的参数,将是否高亮设置为 true。
  170. isSelected = that.option.series[hoveredIndex].pieStatus.selected;
  171. isHovered = false;
  172. k = that.option.series[hoveredIndex].pieStatus.k;
  173. startRatio = that.option.series[hoveredIndex].pieData.startRatio;
  174. endRatio = that.option.series[hoveredIndex].pieData.endRatio;
  175. // 对当前点击的扇形,执行取消高亮操作(对 option 更新)
  176. that.option.series[hoveredIndex].parametricEquation = that.getParametricEquation(startRatio, endRatio,
  177. isSelected,
  178. isHovered, k, that.option.series[hoveredIndex].pieData.value);
  179. that.option.series[hoveredIndex].pieStatus.hovered = isHovered;
  180. // 将此前记录的上次选中的扇形对应的系列号 seriesIndex 清空
  181. hoveredIndex = '';
  182. }
  183. // 使用更新后的 option,渲染图表
  184. myChart.setOption(that.option);
  185. });
  186. },
  187. getPie3D(pieData, internalDiameterRatio) {
  188. let that = this;
  189. let series = [];
  190. let sumValue = 0;
  191. let startValue = 0;
  192. let endValue = 0;
  193. let legendData = [];
  194. let legendBfb = [];
  195. let k = 1 - internalDiameterRatio;
  196. pieData.sort((a, b) => {
  197. return (b.value - a.value);
  198. });
  199. // 为每一个饼图数据,生成一个 series-surface(参数曲面) 配置
  200. for (let i = 0; i < pieData.length; i++) {
  201. sumValue += pieData[i].value;
  202. let seriesItem = {
  203. //系统名称
  204. name: typeof pieData[i].name === 'undefined' ? `series${i}` : pieData[i].name,
  205. type: 'surface',
  206. //是否为参数曲面(是)
  207. parametric: true,
  208. //曲面图网格线(否)上面一根一根的
  209. wireframe: {
  210. show: false
  211. },
  212. pieData: pieData[i],
  213. pieStatus: {
  214. selected: false,
  215. hovered: false,
  216. k: k
  217. },
  218. };
  219. //曲面的颜色、不透明度等样式。
  220. if (typeof pieData[i].itemStyle != 'undefined') {
  221. let itemStyle = {};
  222. typeof pieData[i].itemStyle.color != 'undefined' ? itemStyle.color = pieData[i].itemStyle.color : null;
  223. typeof pieData[i].itemStyle.opacity != 'undefined' ? itemStyle.opacity = pieData[i].itemStyle.opacity : null;
  224. seriesItem.itemStyle = itemStyle;
  225. }
  226. series.push(seriesItem);
  227. }
  228. // 使用上一次遍历时,计算出的数据和 sumValue,调用 getParametricEquation 函数,
  229. // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation,也就是实现每一个扇形。
  230. legendData = [];
  231. legendBfb = [];
  232. for (let i = 0; i < series.length; i++) {
  233. endValue = startValue + series[i].pieData.value;
  234. series[i].pieData.startRatio = startValue / sumValue;
  235. series[i].pieData.endRatio = endValue / sumValue;
  236. series[i].parametricEquation = this.getParametricEquation(series[i].pieData.startRatio, series[i].pieData.endRatio,
  237. false, false, k, series[i].pieData.value);
  238. startValue = endValue;
  239. let bfb = that.fomatFloat(series[i].pieData.value / sumValue, 4);
  240. legendData.push({
  241. name: series[i].name,
  242. value: bfb
  243. });
  244. legendBfb.push({
  245. name: series[i].name,
  246. value: bfb
  247. });
  248. }
  249. //(第二个参数可以设置你这个环形的高低程度)
  250. let boxHeight = this.getHeight3D(series, 15);//通过传参设定3d饼/环的高度
  251. // 准备待返回的配置项,把准备好的 legendData、series 传入。
  252. let option = {
  253. //图例组件
  254. legend: {
  255. data: legendData,
  256. //图例列表的布局朝向。vertical
  257. orient: 'horizontal',
  258. type: 'scroll',
  259. left: '0%',
  260. bottom:'5%',
  261. //图例文字每项之间的间隔
  262. itemGap: 0,
  263. textStyle: {
  264. color: 'black',
  265. fontSize: '0.5rem',
  266. },
  267. show: true,
  268. icon: "circle",
  269. //格式化图例文本(我是数值什么显示什么)
  270. formatter: function (name) {
  271. var target;
  272. for (var i = 0, l = pieData.length; i < l; i++) {
  273. if (pieData[i].name == name) {
  274. target = pieData[i].value;
  275. }
  276. }
  277. return `${name}: ${target}`;
  278. },
  279. // 这个可以显示百分比那种(可以根据你想要的来配置)
  280. formatter: function(param) {
  281. let item = legendBfb.filter(item => item.name == param)[0];
  282. let bfs = that.fomatFloat(item.value * 100, 2) + "%";
  283. console.log(item.name)
  284. return `${item.name} :${bfs}`;
  285. }
  286. },
  287. //移动上去提示的文本内容
  288. tooltip: {
  289. formatter: params => {
  290. if (params.seriesName !== 'mouseoutSeries' && params.seriesName !== 'pie2d') {
  291. let bfb = ((option.series[params.seriesIndex].pieData.endRatio - option.series[params.seriesIndex].pieData.startRatio) *
  292. 100).toFixed(2);
  293. return `${params.seriesName}<br/>` +
  294. `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>` +
  295. `${bfb}`;
  296. }
  297. }
  298. },
  299. //这个可以变形
  300. xAxis3D: {
  301. min: -0.8,
  302. max: 1
  303. },
  304. yAxis3D: {
  305. min: -1,
  306. max: 1
  307. },
  308. zAxis3D: {
  309. min: -1,
  310. max: 1
  311. },
  312. //此处是修改样式的重点
  313. grid3D: {
  314. show: false,
  315. boxHeight: boxHeight, //圆环的高度
  316. //这是饼图的位置
  317. top: '-10%',
  318. viewControl: { //3d效果可以放大、旋转等,请自己去查看官方配置
  319. alpha: 35, //角度(这个很重要 调节角度的)
  320. distance: 165,//调整视角到主体的距离,类似调整zoom(这是整体大小)
  321. rotateSensitivity: 1, //设置为0无法旋转
  322. zoomSensitivity: 0, //设置为0无法缩放
  323. panSensitivity: 0, //设置为0无法平移
  324. autoRotate: false //自动旋转
  325. }
  326. },
  327. series: series
  328. };
  329. return option;
  330. },
  331. getHeight3D(series, height) {
  332. series.sort((a, b) => {
  333. return (b.pieData.value - a.pieData.value);
  334. })
  335. return height * 25 / series[0].pieData.value;
  336. },
  337. getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, h) {
  338. // 计算
  339. let midRatio = (startRatio + endRatio) / 2;
  340. let startRadian = startRatio * Math.PI * 2;
  341. let endRadian = endRatio * Math.PI * 2;
  342. let midRadian = midRatio * Math.PI * 2;
  343. // 如果只有一个扇形,则不实现选中效果。
  344. if (startRatio === 0 && endRatio === 1) {
  345. isSelected = false;
  346. }
  347. // 通过扇形内径/外径的值,换算出辅助参数 k(默认值 1/3)
  348. k = typeof k !== 'undefined' ? k : 1 / 3;
  349. // 计算选中效果分别在 x 轴、y 轴方向上的位移(未选中,则位移均为 0)
  350. let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  351. let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  352. // 计算高亮效果的放大比例(未高亮,则比例为 1)
  353. let hoverRate = isHovered ? 1.05 : 1;
  354. // 返回曲面参数方程
  355. return {
  356. u: {
  357. min: -Math.PI,
  358. max: Math.PI * 3,
  359. step: Math.PI / 32
  360. },
  361. v: {
  362. min: 0,
  363. max: Math.PI * 2,
  364. step: Math.PI / 20
  365. },
  366. x: function (u, v) {
  367. if (u < startRadian) {
  368. return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
  369. }
  370. if (u > endRadian) {
  371. return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
  372. }
  373. return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
  374. },
  375. y: function (u, v) {
  376. if (u < startRadian) {
  377. return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
  378. }
  379. if (u > endRadian) {
  380. return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
  381. }
  382. return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
  383. },
  384. z: function (u, v) {
  385. if (u < -Math.PI * 0.5) {
  386. return Math.sin(u);
  387. }
  388. if (u > Math.PI * 2.5) {
  389. return Math.sin(u) * h * .1;
  390. }
  391. return Math.sin(v) > 0 ? 1 * h * .1 : -1;
  392. }
  393. };
  394. },
  395. fomatFloat(num, n) {
  396. var f = parseFloat(num);
  397. if (isNaN(f)) {
  398. return false;
  399. }
  400. f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
  401. var s = f.toString();
  402. var rs = s.indexOf('.');
  403. //判定如果是整数,增加小数点再补0
  404. if (rs < 0) {
  405. rs = s.length;
  406. s += '.';
  407. }
  408. while (s.length <= rs + n) {
  409. s += '0';
  410. }
  411. return s;
  412. },
  413. },
  414. }
  415. </script>
  416. <style>
  417. .chart{
  418. background-size: 60% 60%;
  419. /* background:rgba(34,34,34,0.5); */
  420. }
  421. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/582610
推荐阅读
相关标签
  

闽ICP备14008679号