• Vue2中Echarts组件二次封装


    Echarts官网(Apache ECharts)

    下载npm install echarts

    在哪里用哪里引入import * as echarts from 'echarts';

    根据步骤进行将图表渲染到页面

    app.vue

    1. <template>
    2. <!-- 就传这个图表 -->
    3. <MyEcharts :options="options" ></MyEcharts>
    4. <button @click="changeOpt">切换样式</button>
    5. <RouterView />
    6. </template>
    7. <script>
    8. import MyEcharts from './components/MyEcharts.vue'
    9. import {options1,options2} from './options'
    10. import _ from 'lodash';
    11. export default {
    12. name: 'App',
    13. components: {
    14. MyEcharts
    15. },
    16. data(){
    17. return {
    18. //创建一个options.js文件
    19. options:options1,
    20. //宽度
    21. width:"600px"
    22. }
    23. },
    24. methods: {
    25. changeOpt() {
    26. if (_.isEqual(this.options, options1)) {
    27. this.options = options2; // 切换到options2
    28. console.log('Switched to options2');
    29. } else {
    30. this.options = options1; // 切换回options1
    31. console.log('Switched to options1');
    32. }
    33. }
    34. }
    35. }
    36. </script>
    37. <style scoped>
    38. </style>

    options.js

    1. export const options1 = {
    2. color:['rad'],
    3. title: {
    4. text: "ECharts 入门示例",
    5. },
    6. tooltip: {},
    7. legend: {
    8. data: ["销量"],
    9. },
    10. xAxis: {
    11. data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    12. },
    13. yAxis: {},
    14. series: [
    15. {
    16. name: "销量",
    17. type: "bar",
    18. data: [5, 20, 36, 10, 10, 20],
    19. },
    20. ]
    21. }
    22. export const options2 = {
    23. color:['tomato'],
    24. title: {
    25. text: "ECharts 入门示例1",
    26. },
    27. tooltip: {},
    28. legend: {
    29. data: ["销量"],
    30. },
    31. xAxis: {
    32. data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    33. },
    34. yAxis: {},
    35. series: [
    36. {
    37. name: "销量",
    38. type: "bar",
    39. data: [50, 100, 36, 100, 200, 250],
    40. },
    41. ]
    42. }

    MyEcharts.vue

    1. <template>
    2. <div :id="uuid" :style="style" >div>
    3. template>
    4. <script>
    5. import * as echarts from "echarts";
    6. //准备一个生成uuid的方法
    7. const idGen=()=>{
    8. return new Date().getTime()
    9. }
    10. export default {
    11. props:{
    12. height:{
    13. type:String,
    14. default:"400px"
    15. },
    16. width:{
    17. type:String,
    18. default:"600px"
    19. },
    20. options:{
    21. type:Object,
    22. default:null
    23. }
    24. },
    25. data() {
    26. return {
    27. //现将id改为自动生成的
    28. uuid: null,
    29. myChart:null
    30. };
    31. },
    32. //实现响应式
    33. watch:{
    34. options(){
    35. if(this.myChart){
    36. this.myChart.setOption(this.options)
    37. }
    38. }
    39. },
    40. computed:{
    41. style(){
    42. return {
    43. height:this.height,
    44. width:this.width
    45. }
    46. }
    47. },
    48. created(){
    49. this.uuid = idGen();
    50. },
    51. //一进入页面就开始渲染Echarts图表
    52. mounted() {
    53. //官方流程
    54. // 基于准备好的dom,初始化echarts实例
    55. //准备实例
    56. this.myChart = echarts.init(document.getElementById(this.uuid));
    57. // 指定图表的配置项和数据
    58. //组织配置项
    59. // 使用刚指定的配置项和数据显示图表。
    60. //应用配置项
    61. this.myChart.setOption(this.options);
    62. },
    63. };
    64. script>

  • 相关阅读:
    【论文知识点笔记】具有定量特征融合的粒子群优化模糊 CNN 用于超声图像质量识别
    一、音视频小白入门|搭建 FFmpeg你自己的直播平台
    怎样重置ubuntu mysql8密码
    凭着这份java面试一战到底,直接碾压面试官
    微分中值定理证明和总结
    1. Nginx 基本功能配置
    【多线程】定时器 Timer
    matplotlib与django如何集成? matplotlib生成的图可以嵌入到网页吗?
    Vue笔记十二:Vuex辅助函数
    天天写SQL,这些神奇的特性你知道吗?
  • 原文地址:https://blog.csdn.net/Wx001214/article/details/139686935