通过echarts实现缩放柱状图

功能

通过echart图表中的js代码,使柱状图实现缩放

用法

1.1 新建表格

页面内新建任意数里的表格后

1.2 新增echart图表

将js代码复制到echart图表中

v1.2代码

  1. dataSet = dataSet.map((data, i) => dataSet.map(d => d[i]));
  2. var dataAxis = dataSet[0];
  3. var data = dataSet[1];;
  4. var yMax = 500;
  5. var dataShadow = [];
  6. for (var i = 0; i < data.length; i++) {
  7. dataShadow.push(yMax);
  8. }
  9. option = {
  10. title: {
  11. text: '特性示例:渐变色 阴影 点击缩放',
  12. subtext: 'Feature Sample: Gradient Color, Shadow, Click Zoom'
  13. },
  14. xAxis: {
  15. data: dataAxis,
  16. axisLabel: {
  17. inside: true,
  18. textStyle: {
  19. color: '#fff'
  20. }
  21. },
  22. axisTick: {
  23. show: false
  24. },
  25. axisLine: {
  26. show: false
  27. },
  28. z: 10
  29. },
  30. yAxis: {
  31. axisLine: {
  32. show: false
  33. },
  34. axisTick: {
  35. show: false
  36. },
  37. axisLabel: {
  38. textStyle: {
  39. color: '#999'
  40. }
  41. }
  42. },
  43. dataZoom: [
  44. {
  45. type: 'inside'
  46. }
  47. ],
  48. series: [
  49. {
  50. type: 'bar',
  51. showBackground: true,
  52. itemStyle: {
  53. color: new echarts.graphic.LinearGradient(
  54. 0, 0, 0, 1,
  55. [
  56. {offset: 0, color: '#83bff6'},
  57. {offset: 0.5, color: '#188df0'},
  58. {offset: 1, color: '#188df0'}
  59. ]
  60. )
  61. },
  62. emphasis: {
  63. itemStyle: {
  64. color: new echarts.graphic.LinearGradient(
  65. 0, 0, 0, 1,
  66. [
  67. {offset: 0, color: '#2378f7'},
  68. {offset: 0.7, color: '#2378f7'},
  69. {offset: 1, color: '#83bff6'}
  70. ]
  71. )
  72. }
  73. },
  74. data: data
  75. }
  76. ]
  77. };
  78. // Enable data zoom when user click bar.
  79. var zoomSize = 6;
  80. myChart.on('click', function (params) {
  81. console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]);
  82. myChart.dispatchAction({
  83. type: 'dataZoom',
  84. startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
  85. endValue: dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
  86. });
  87. });