ECharts之柱状图

旧城等待, 2021-09-29 22:48 927阅读 0赞

效果图

在这里插入图片描述

背景图片

20190725170845879.png

下载ECharts

  1. npm install echarts --save

引入并注册全局ECharts

    在 main.js 文件里引入并注册 ( 这里是 Vue3.0 的模板 )

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from './store'
  5. import echarts from 'echarts'
  6. Vue.prototype.$echarts = echarts
  7. Vue.config.productionTip = false
  8. new Vue({
  9. router,
  10. store,
  11. render: h => h(App)
  12. }).$mount('#app')

在组件中使用ECharts

  1. <template>
  2. <div class='wrapper'>
  3. <div class='chart' id='chart'></div>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data () {
  9. return { }
  10. },
  11. mounted () {
  12. this.drawChart()
  13. },
  14. methods: {
  15. drawChart () {
  16. // 基于准备好的dom,初始化echarts实例
  17. let chart = this.$echarts.init(document.getElementById('chart'))
  18. // 监听屏幕变化自动缩放图表
  19. window.addEventListener('resize', function () { chart.resize() })
  20. // 绘制图表
  21. chart.setOption({
  22. // 设置图表的位置
  23. grid: {
  24. x: 60, // 左间距
  25. y: 80, // 上间距
  26. x2: 60, // 右间距
  27. y2: 40, // 下间距
  28. containLabel: true // grid 区域是否包含坐标轴的刻度标签, 常用于『防止标签溢出』的场景
  29. },
  30. // dataZoom 组件 用于区域缩放
  31. dataZoom: [
  32. {
  33. type: 'inside',
  34. xAxisIndex: [0], // 设置 dataZoom-inside 组件控制的 x 轴
  35. // 数据窗口范围的起始和结束百分比 范围: 0 ~ 100
  36. start: 0,
  37. end: 80
  38. }
  39. ],
  40. // 图表主标题
  41. title: {
  42. text: '短袖销售额', // 主标题文本,支持使用 \n 换行
  43. top: 10, // 定位 值: 'top', 'middle', 'bottom' 也可以是具体的值或者百分比
  44. left: 'center', // 值: 'left', 'center', 'right' 同上
  45. textStyle: { // 文本样式
  46. fontSize: 24,
  47. fontWeight: 600,
  48. color: '#fff'
  49. }
  50. },
  51. // 设置自定义文字
  52. graphic: [
  53. {
  54. type: 'text', // 图形元素类型
  55. left: 35, // 进行定位
  56. bottom: 18,
  57. style: { // 文本样式
  58. fill: '#cdd3ee',
  59. text: '(月份)',
  60. font: 'normal 14px Microsoft' // style | weight | size | family
  61. }
  62. }
  63. ],
  64. // 提示框组件
  65. tooltip: {
  66. trigger: 'axis', // 触发类型, axis: 坐标轴触发
  67. axisPointer: {
  68. type: 'line' // 指示器类型
  69. },
  70. // 提示框浮层内容格式器,支持字符串模板和回调函数两种形式
  71. // 折线(区域)图、柱状(条形)图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)
  72. formatter: '{b}<br />{a0}: {c0}万<br />{a1}: {c1}%'
  73. },
  74. // 图例组件
  75. legend: {
  76. // 图例的数据数组
  77. data: [
  78. {
  79. name: '已销售' // 图例项的名称 与 series 里的 name 相对应
  80. },
  81. {
  82. name: '销售率'
  83. }
  84. ],
  85. itemWidth: 15, // 图例标记的图形宽度
  86. itemHeight: 15, // 图例标记的图形高度
  87. // 图例项的 icon 值: 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none'
  88. icon: 'roundRect',
  89. top: 13, // 定位
  90. right: 10,
  91. textStyle: { // 文本样式
  92. fontSize: 16,
  93. color: '#cdd3ee'
  94. }
  95. },
  96. // X轴
  97. xAxis: {
  98. show: true, // 不设置默认值为 true
  99. // 坐标轴类型, 'category' 类目轴,适用于离散的类目数据,为该类型时必须通过 data 设置类目数据
  100. type: 'category',
  101. // 坐标轴轴线
  102. axisLine: {
  103. lineStyle: {
  104. type: 'solid', // 坐标轴线线的类型 'solid', 'dashed', 'dotted'
  105. width: 1, // 坐标轴线线宽, 不设置默认值为 1
  106. color: '#cdd3ee' // 坐标轴线线的颜色
  107. }
  108. },
  109. // 坐标轴刻度
  110. axisTick: {
  111. show: false
  112. },
  113. // 分隔线
  114. splitLine: {
  115. show: false
  116. },
  117. // 坐标轴刻度标签
  118. axisLabel: {
  119. fontSize: 16, // 文字的字体大小
  120. color: '#cdd3ee', // 刻度标签文字的颜色
  121. // 使用函数模板 传入的数据值 -> value: number|Array,
  122. formatter: function (value) {
  123. return value.replace(/[\u4e00-\u9fa5]/g, '')
  124. }
  125. },
  126. // 类目名称
  127. data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  128. },
  129. yAxis: [
  130. // 左侧Y轴
  131. {
  132. type: 'value', // 坐标轴类型, 'value' 数值轴,适用于连续数据
  133. name: '单位/万', // 坐标轴名称
  134. nameLocation: 'end', // 坐标轴名称显示位置 'start', 'middle' 或者 'center', 'end'
  135. nameTextStyle: { // 坐标轴名称的文字样式
  136. fontSize: 16
  137. },
  138. nameGap: 13, // 坐标轴名称与轴线之间的距离
  139. // 坐标轴刻度
  140. axisTick: {
  141. show: true // 是否显示坐标轴刻度 默认显示
  142. },
  143. // 坐标轴轴线
  144. axisLine: { // 是否显示坐标轴轴线 默认显示
  145. show: true, // 是否显示坐标轴轴线 默认显示
  146. lineStyle: { // 坐标轴线线的颜色
  147. color: '#cdd3ee'
  148. }
  149. },
  150. // 坐标轴在图表区域中的分隔线
  151. splitLine: {
  152. show: false // 是否显示分隔线。默认数值轴显示
  153. },
  154. // 坐标轴刻度标签
  155. axisLabel: {
  156. show: true, // 是否显示刻度标签 默认显示
  157. fontSize: 16, // 文字的字体大小
  158. color: '#cdd3ee', // 刻度标签文字的颜色
  159. // 使用字符串模板,模板变量为刻度默认标签 {value}
  160. formatter: '{value}'
  161. }
  162. },
  163. // 右侧Y轴
  164. {
  165. // 坐标轴刻度
  166. axisTick: {
  167. show: true // 是否显示坐标轴刻度 默认显示
  168. },
  169. // 坐标轴轴线
  170. axisLine: { // 是否显示坐标轴轴线 默认显示
  171. show: true, // 是否显示坐标轴轴线 默认显示
  172. lineStyle: { // 坐标轴线线的颜色
  173. color: '#cdd3ee'
  174. }
  175. },
  176. // 坐标轴在图表区域中的分隔线
  177. splitLine: {
  178. show: false // 是否显示分隔线 默认数值轴显示
  179. },
  180. axisLabel: {
  181. show: true,
  182. fontSize: 16,
  183. color: '#cdd3ee',
  184. // 使用字符串模板,模板变量为刻度默认标签 {value}
  185. formatter: '{value}%'
  186. }
  187. }
  188. ],
  189. // 系列列表
  190. series: [
  191. {
  192. type: 'bar', // 系列类型
  193. name: '已销售', // 系列名称, 用于tooltip的显示, legend 的图例筛选
  194. barMaxWidth: 15, // 柱条的最大宽度,不设时自适应
  195. barGap: 0, // 不同系列的柱间距离, 为百分比 默认值为30%
  196. // 图形上的文本标签
  197. label: {
  198. show: false,
  199. fontSize: 16,
  200. color: '#fff'
  201. },
  202. // 图形样式
  203. itemStyle: {
  204. // 柱条的颜色, 这里是渐变色, 默认从全局调色盘 option.color 获取颜色
  205. color: {
  206. type: 'linear',
  207. x: 0,
  208. y: 0,
  209. x2: 0,
  210. y2: 1,
  211. colorStops: [{
  212. offset: 0,
  213. color: '#FAB363' // 0% 处的颜色
  214. }, {
  215. offset: 1,
  216. color: '#FB7C2B' // 100% 处的颜色
  217. }]
  218. },
  219. barBorderRadius: [10, 10, 0, 0] // 圆角半径, 单位px, 支持传入数组分别指定 4 个圆角半径
  220. },
  221. // 系列中的数据内容数组
  222. data: [200, 330, 400, 600, 830, 650, 690, 430, 550, 420, 420, 320]
  223. },
  224. {
  225. type: 'line', // 系列类型
  226. name: '销售率', // 系列名称, 用于tooltip的显示, legend 的图例筛选
  227. symbol: 'circle', // 标记的图形
  228. symbolSize: 10, // 标记的大小
  229. yAxisIndex: 1, // 使用的 y 轴的 index,在单个图表实例中存在多个 y轴的时候有用
  230. // 图形的样式
  231. itemStyle: {
  232. color: '#11abff'
  233. },
  234. // 线的样式, 修改 lineStyle 中的颜色不会影响图例颜色, 一般不设置线的样式
  235. lineStyle: {
  236. type: 'solid', // 线的类型 'solid', 'dashed', 'dotted'
  237. color: '#11abff'
  238. },
  239. // 图形上的文本标签
  240. label: {
  241. show: false,
  242. fontSize: 16,
  243. color: '#fff'
  244. },
  245. // 系列中的数据内容数组
  246. data: [20, 24, 33, 45, 63, 50, 42, 24, 23, 14, 20, 10]
  247. }
  248. ]
  249. })
  250. }
  251. }
  252. }
  253. </script>
  254. <style scoped>
  255. .wrapper {
  256. width: 100%;
  257. }
  258. .wrapper .chart {
  259. width: 60%;
  260. height: 400px;
  261. margin: 100px 50px 0;
  262. border: 1px solid #eeeeee;
  263. background: url(../../public/static/bg.png) no-repeat;
  264. background-size: 100% 100%;
  265. }
  266. </style>

发表评论

表情:
评论列表 (有 0 条评论,927人围观)

还没有评论,来说两句吧...

相关阅读