经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Echarts » 查看文章
vue中使用echarts的示例
来源:jb51  时间:2021/1/4 9:00:56  对本文有异议

1:首先要用到echarts

2:在vue中安装这个依赖

3:引入要用的页面

  1. import echarts from 'echarts';

4:然后初始化

  1. <template>
  2. <a-col span="12" style="min-height:343px;width:100%;background:#fff" ref="getwidth" :style="'display:'+ model">
  3. <div class="layui-col-md6 tjgx-panel" >
  4. <div class="layui-card">
  5. <div class="layui-card-header panel-title">
  6. <span class="left-text" >统计更新及时率</span>
  7. <a class="close-link right-text" @click="onHide">
  8. <a-icon type="close" style="color: #b0b0b0;" />
  9. </a>
  10. <div class="right-text" style="color: #b0b0b0;">
  11. <span v-for="(item,index) in activeList" :key="index"
  12. :class="item.is_active?'acitive':''"
  13. @click.stop="Onactive(item,index)"
  14. class="week " ref="alist">{{item.name}}</span>
  15. <!-- <span class="month">本月</span>
  16. <span class="year">本年</span> -->
  17. </div>
  18. </div>
  19. <div class="layui-card-body common-height">
  20. <echartsModal
  21. :Maxwidths ="widths"
  22. :MinHeight="MHeight"
  23. :echartsColor="xAxis.color"
  24. :legendWAndH="legendWAndH"
  25. :xAxisData="xAxis.data"
  26. :seriesData="xAxis.x.data"
  27. :SerNameValue="xAxis.SerNameValue"
  28. :echartsName="xAxis.name"
  29. />
  30. <!-- <div class="tjgx-con" id="container" style="min-height:280px;" :style="'width:'+widths+'px'" ></div> -->
  31. </div>
  32. </div>
  33. </div>
  34. </a-col>
  35. </template>
  36. <script>
  37. import echartsModal from '../../viewModal/Echarts'
  38. // import echarts from 'echarts';
  39. export default {
  40. components:{
  41. echartsModal
  42. },
  43. data(){
  44. return{
  45. model:'block',
  46. widths:'',
  47. legendWAndH:[15,15,18],
  48. MHeight:'',
  49. xAxis:{
  50. data: ['广州', '深圳', '珠海', '汕头', '佛山', '韶关', '湛江', '肇庆', '江门', '茂名', '惠州', '梅州', '汕尾', '河源', '阳江', '清远', '东莞', '中山', '潮州', '揭阳', '云浮'],
  51. x:{
  52. data: [7.9, 2, 6, 5, 7, 5, 7, 80, 6, 7, 6, 86, 6, 56, 7, 6, 8, 5, 4, 7, 8],
  53. // data: [87.9, 58, 67, 56, 72, 54, 74, 59, 64, 75, 68, 47, 63, 89, 78, 64, 82, 56, 40, 73, 80],
  54. },
  55. name:'及时率', //图表名称
  56. SerNameValue:{
  57. position:'top',
  58. color:'#3398DB', //
  59. },
  60. // color:'' //图表颜色
  61. color:'#3398DB' //图表颜色
  62. },
  63. activeList:[
  64. {
  65. name:'本周',
  66. is_active:true,
  67. },
  68. {
  69. name:'本月',
  70. is_active:false,
  71. },
  72. {
  73. name:'本年',
  74. is_active:false,
  75. },
  76. ]
  77. }
  78. },
  79. mounted(){
  80. this.widths = String((this.$refs.getwidth.$el.clientWidth) - 30) ; //画布宽度
  81. this.MHeight =String((this.$refs.getwidth.$el.clientHeight) - 68) //画布高度
  82. },
  83. methods:{
  84. onHide(){
  85. const _this = this;
  86. _this.model = 'none';
  87. _this.$emit('isHide', _this.model,0)
  88. },
  89. Onactive(val,index){
  90. const _this = this
  91. let Alist = _this.activeList
  92. let Length = _this.$refs.alist.length
  93. let axis = _this.xAxis
  94. let sumber = 0;
  95. let arr = [];
  96. for( let t = 0; t < Length; t++ ){
  97. Alist[t].is_active = false;
  98. }
  99. if( val.name == '本周' ){
  100. sumber = 1
  101. }else if ( val.name =='本月' ){
  102. sumber = 1
  103. }else if( val.name == '本年' ){
  104. sumber = 1
  105. }
  106. Alist[index].is_active = true;
  107. axis.x.data.map(item=>{
  108. item += sumber
  109. arr.push(item)
  110. })
  111. axis.x.data = arr
  112. _this.xAxis = axis
  113. _this.activeList = Alist
  114. }
  115. }
  116. }
  117. </script>
  118. <style scoped>
  119. /* .week{
  120. padding: 0 1%;
  121. } */
  122. .layui-card-header {
  123. position: relative;
  124. height: 42px;
  125. line-height: 42px;
  126. padding: 0 15px;
  127. border-bottom: 1px solid #f6f6f6;
  128. color: #333;
  129. border-radius: 2px 2px 0 0;
  130. font-size: 14px;
  131. }
  132. .common-height {
  133. height: 280px;
  134. }
  135. .layui-card {
  136. margin-bottom: 15px;
  137. border-radius: 2px;
  138. background-color: #fff;
  139. box-shadow: 0 1px 2px 0 rgba(0,0,0,.05);
  140. }
  141. .layui-card-body {
  142. position: relative;
  143. padding: 10px 15px;
  144. line-height: 24px;
  145. }
  146. .layui-col-md20 {
  147. width: 20%;
  148. }
  149. .time-city-panel {
  150. display: flex !important;
  151. justify-content: space-between;
  152. align-items: center;
  153. }
  154. .time-city-panel img {
  155. width: 73px !important;
  156. height: 61px !important;
  157. padding: 0 10px 10px !important;
  158. }
  159. .time-city-panel .one-row,
  160. .time-city-panel .two-row {
  161. padding: 0 10px;
  162. }
  163. .time-city-panel .one-row p:first-of-type {
  164. font-size: 18px;
  165. font-weight: bold;
  166. padding: 0 0 10px;
  167. text-align: left;
  168. }
  169. .time-city-panel .two-row p:first-of-type {
  170. padding: 0 0 10px;
  171. color: #96acbc;
  172. font-weight: bold;
  173. line-height: 22px;
  174. }
  175. .db_img {
  176. width: 20px;
  177. height: 22px;
  178. margin: 0 5px;
  179. vertical-align: middle;
  180. }
  181. .panel-title .left-text {
  182. padding-left: 10px;
  183. border-left: 8px solid #239fe6;
  184. font-size: 20px;
  185. display: inline-block;
  186. height: 30px;
  187. line-height: 30px;
  188. vertical-align: middle;
  189. }
  190. .panel-title .right-text {
  191. float: right;
  192. color: #239fe6;
  193. font-size: 14px;
  194. margin-left: 15px;
  195. }
  196. /*筒高度*/
  197. .common-height {
  198. height: 280px;
  199. }
  200. .text-overflow {
  201. white-space: nowrap;
  202. overflow: hidden;
  203. text-overflow: ellipsis;
  204. }
  205. .tjgx-panel .right-text span,
  206. .ajtj-panel .right-text span {
  207. display: inline-block;
  208. color: #b0b0b0;
  209. }
  210. .tjgx-panel .right-text span.acitive,
  211. .ajtj-panel .right-text span.acitive {
  212. color: #239fe6;
  213. }
  214. #chart,
  215. #ajtjChart {
  216. width: 100%;
  217. height: 90%;
  218. }
  219. </style>

5:Echarts 组件

  1. <template>
  2. <div>
  3. <!-- style="min-height:280px;" -->
  4. <div id="Echarts" :style="'width:'+Maxwidths+'px;min-height:'+MinHeight+'px'" ></div>
  5. <!-- <div id="Echarts" v-if="Isgrid == false" :style="'width:'+Maxwidths+'px;height:'+MinHeight+'px'" ></div> -->
  6. </div>
  7. </template>
  8. <script>
  9. import echarts from 'echarts';
  10. import { number } from 'echarts/lib/export';
  11. export default {
  12. props:{
  13. Isgrid:{
  14. type:Boolean,
  15. default:()=> true //默认显示柱状图
  16. },
  17. ss:{
  18. type:Object,
  19. default:()=>{}
  20. },
  21. Maxwidths:{
  22. type:String, //整个容器的宽度
  23. default:''
  24. },
  25. MinHeight:{
  26. type:String, //最小高度
  27. default:''
  28. },
  29. echartsColor:{ //图像的颜色
  30. type:String,
  31. default:''
  32. },
  33. legendWAndH:{ //图例的宽高位置等
  34. type:Array,
  35. default:()=>[15,15,18]
  36. },
  37. legendColor:{
  38. type:String,
  39. default:()=>'' //图例的颜色,如果不加则默认是图形颜色
  40. },
  41. xAxisData:{ //图形X轴的数据
  42. type:Array,
  43. default:()=>[]
  44. },
  45. YAxisName:{ //图形y轴的名称设置 数组或者百分比
  46. type:Object,
  47. default:()=>{}
  48. // default:{name:'111',formatter: '{value}%'}
  49. },
  50. seriesData:{
  51. type:Array, // 显示图像的数据 占据的多少
  52. default:()=>[]
  53. },
  54. SerNameValue:{ //图形数据分析的占据的位置 left right top bottom 和显示的颜色
  55. type:Object,
  56. default:()=>{}
  57. // default:{position:top,color: '#3398DB',formatter: '{c}%'}
  58. },
  59. echartsName:{ //图形的名称
  60. type:String,
  61. default:()=>'柱状图'
  62. },
  63. XorYatter:{
  64. type:Array,
  65. default:()=>['{value}%','{c}%'] //默认为百分比
  66. },
  67. //雷达图的参数
  68. gWidth:{ //宽度默认为百分百
  69. type:String,
  70. default:()=>'100'
  71. },
  72. gHeight:{
  73. type:String,
  74. default:()=> '90' //高度默认为90
  75. },
  76. gridValue:{ /// 雷达图的名称等等 格式[{text:11}]
  77. type:Array,
  78. default:()=>[]
  79. },
  80. gridraius:{ //圆圈大小
  81. type:Number,
  82. default:()=> 80 //默认为80
  83. },
  84. gridTextStyle:{ //名称的默认颜色 text
  85. type:String,
  86. default:()=>'#6bbcef'
  87. },
  88. symbolSize:{ // 圆点的大小
  89. type:Number,
  90. default:()=> 10
  91. },
  92. SerValue:{
  93. type:Array,
  94. default:()=>[] //网格里面对应的数值
  95. },
  96. serVcolor:{
  97. type:String,
  98. default:()=> '#1bbdf8' //数值的默认颜色 SerValue的
  99. },
  100. lineStyleColor:{
  101. type:String,
  102. default:()=> '#1cbdf8' //连接线的颜色
  103. },
  104. itemStyleColor:{
  105. type:String,
  106. default:()=> '#1cbdf8' //连接圆点的默认颜色
  107. },
  108. BgColor:{
  109. type:String,
  110. default:()=> 'rgba(189, 230, 249, 0.5)'
  111. }
  112. },
  113. data(){
  114. return{
  115. option:{
  116. color:[], //图例颜色
  117. legend:{
  118. data:[], //图例名称
  119. itemWidth:0, //图例的宽度
  120. itemHeight:0, //图例的高度
  121. right:0,
  122. textStyle:{
  123. color:''
  124. }
  125. },
  126. xAxis:{
  127. data:[] //x轴数据
  128. },
  129. yAxis:{
  130. name:'', //名称
  131. axisLabel:{
  132. formatter:'' //是否显示百分比
  133. }
  134. },
  135. series:[
  136. {
  137. name:'',
  138. type:'bar', //类型
  139. data:[], //X轴显示的数据
  140. label:{
  141. normal:{
  142. show:true,
  143. position:'',
  144. color:'',
  145. formatter:''
  146. }
  147. }
  148. }
  149. ]
  150. },
  151. options:{
  152. radar:[
  153. {
  154. indicator:[], // 显示的名称的字段只能是text ps:[{text:'aaa'}]
  155. radius:0, //园角
  156. name:{
  157. textStyle:{
  158. color:'' //颜色
  159. }
  160. },
  161. symbolSize:0, //圆点大小
  162. }
  163. ],
  164. series:[
  165. {
  166. type:'radar',
  167. data:[{
  168. value:[], //对应的数据
  169. label:{
  170. normal:{
  171. show:true,
  172. color:'', //选中的时候颜色
  173. formatter:(params)=>{
  174. return params.value;
  175. }
  176. }
  177. },
  178. //连接线颜色
  179. lineStyle:{
  180. color:'',
  181. },
  182. // 连接圆点颜色
  183. itemStyle:{
  184. color:''
  185. },
  186. // 图表背景网格的颜色
  187. areaStyle:{
  188. normal:{
  189. opacity:0.9,
  190. color:''
  191. }
  192. }
  193. }]
  194. }
  195. ]
  196. }
  197. }
  198. },
  199. created(){
  200. const _this = this;
  201. console.log(_this.Isgrid)
  202. console.log(this.ECharts)
  203. if( _this.Isgrid ){
  204. _this.onloads() //初始化数据
  205. }else{
  206. _this.isInitGrid()
  207. }
  208. },
  209. mounted(){
  210. const _this = this;
  211. if(_this.ss){
  212. _this.option = {}
  213. _this.option = _this.ss;
  214. }
  215. setTimeout(()=>{
  216. _this.$nextTick(()=>{
  217. let Echarts = echarts.init(document.getElementById('Echarts'))
  218. Echarts.setOption(_this.option,true)
  219. })
  220. },500)
  221. },
  222. watch:{
  223. seriesData(val){
  224. this.seriesData = val
  225. this.onloads(true)
  226. }
  227. },
  228. methods:{
  229. onloads(is){
  230. const _this = this;
  231. let op = _this.option
  232. op.color = [_this.echartsColor]
  233. op.legend.data = [_this.echartsName]
  234. op.legend.itemWidth =_this.legendWAndH[0]
  235. op.legend.itemHeight =_this.legendWAndH[1]
  236. op.legend.right =_this.legendWAndH[2]
  237. op.legend.textStyle.color =_this.legendColor == '' ? _this.echartsColor:''
  238. op.xAxis.data = _this.xAxisData
  239. op.yAxis.name = _this.echartsName
  240. op.yAxis.axisLabel.formatter = _this.XorYatter[0]
  241. op.series[0].data = _this.seriesData
  242. op.series[0].name = _this.echartsName
  243. op.series[0].label.normal.position = _this.SerNameValue.position
  244. op.series[0].label.normal.formatter = _this.XorYatter[1]
  245. op.series[0].label.normal.color = _this.SerNameValue.color == '' ? _this.echartsColor:_this.SerNameValue.color
  246. _this.option = op;
  247. if( is ){
  248. _this.$nextTick(()=>{
  249. let Echarts = echarts.init(document.getElementById('Echarts'))
  250. Echarts.setOption(op,true)
  251. })
  252. }
  253.  
  254. },
  255. isInitGrid(){
  256. const _this = this;
  257. let g = _this.options;
  258. g.radar[0].indicator = _this.gridValue
  259. g.radar[0].radius = _this.gridraius
  260. g.radar[0].name.textStyle.color = _this.gridTextStyle
  261. g.radar[0].symbolSize = _this.symbolSize
  262. g.series[0].data[0].value = _this.SerValue
  263. g.series[0].data[0].label.normal.color = _this.serVcolor
  264. g.series[0].data[0].lineStyle.color = _this.lineStyleColor
  265. g.series[0].data[0].itemStyle.color = _this.itemStyleColor
  266. g.series[0].data[0].areaStyle.normal.color = _this.BgColor
  267. }
  268. }
  269. }
  270. </script>

6:效果图

以上就是vue中使用echarts的步骤的详细内容,更多关于vue 使用echarts的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号