首页 > 其他分享 >在线echarts编辑器实现

在线echarts编辑器实现

时间:2022-11-11 18:13:42浏览次数:72  
标签:option chart 编辑器 editor monaco echarts 在线

一、简介

本文主要介绍如何开发一个同echarts官网里面在线echarts编辑工具,通过页面中的代码编辑器修改echarts的option属性来实时渲染echarts的展示效果。其中主要使用到了【monaco-editor】这个组件。

下图为完成的效果图:

二、如何实现

  • 引入monaco-editor包

    npm install monaco-editor
    npm install monaco-editor-webpack-plugin
    
  • 编辑器组件封装

    通过引入monaco-editor组件,然后进行指定dom节点以及配置进行页面渲染,该组件通过v-model绑定编辑器的内容。

    monaco-editor官方文档:https://microsoft.github.io/monaco-editor/api/modules/monaco.html

    <template>
      <div id="jsMonacoEditor" ref="main" />
    </template>
    
    <script>
    import * as monaco from 'monaco-editor'
    
    /*
    * @Description: monacoEditor组件封装
    * @Author: linmt
    * @Date: @Date: 2022-09-11 08:45:55
    */
    export default {
      name: 'JsMonacoEditor',
      components: {
      },
      model: { // 使用v-model绑定编辑器的内容
        prop: 'value',
        event: 'change'
      },
      props: {
        value: {
          type: String,
          default: ''
        }
      },
      data() {
        return {
          monacoEditor: null
        }
      },
      computed: {
    
      },
      watch: {
        value() {
          this.init()
        }
      },
      created() {
    
      },
      mounted() {
        this.init()
      },
      methods: {
        /**
         * 初始化
         */
        init() {
          if (this.monacoEditor) {
            return
          }
    
          this.monacoEditor = monaco.editor.create(this.$refs.main, {
            theme: 'vs', // 主题
            value: this.value, // 默认显示的值
            language: 'javascript',
            folding: true, // 是否折叠
            foldingHighlight: true, // 折叠等高线
            foldingStrategy: 'indentation', // 折叠方式  auto | indentation
            showFoldingControls: 'always', // 是否一直显示折叠 always | mouseover
            disableLayerHinting: true, // 等宽优化
            emptySelectionClipboard: false, // 空选择剪切板
            selectionClipboard: false, // 选择剪切板
            automaticLayout: true, // 自动布局
            codeLens: false, // 代码镜头
            scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
            colorDecorators: true, // 颜色装饰器
            accessibilitySupport: 'off', // 辅助功能支持  "auto" | "off" | "on"
            lineNumbers: 'on', // 行号 取值: "on" | "off" | "relative" | "interval" | function
            lineNumbersMinChars: 5, // 行号最小字符   number
            enableSplitViewResizing: false,
            readOnly: false, // 是否只读  取值 true | false
            minimap: {
              enabled: true // 是否启用预览图
            } // 预览图设置
          })
    
          const self = this
          
          // _.debounce为lodashjs工具包的方法,见https://www.lodashjs.com/
          // 主要用来防抖动
          const changeValueFun = _.debounce(function() {
            self.changeValue()
          }, 500)
    
    	  // 监听编辑器内容改变事件
          this.monacoEditor.onDidChangeModelContent((e) => {
            changeValueFun()
          })
        },
        changeValue() {
          this.$emit('change', this.monacoEditor.getValue())
        }
      }
    }
    
    </script>
    <style lang='scss' scoped>
    #jsMonacoEditor{
      height: 100%;
      width: 100%;
    }
    </style>
    
    
  • 将代码编辑器和echarts渲染结合

    实现逻辑:通过监听编辑器的组件的change事件来刷新echarts图,用编辑器里面的内容使用new Function(code)方式实现js动态的代码执行并获取返回值,也就是echarts的option属性,然后通过option的值进行echarts渲染,再通过通过定时的方式进行echarts的大小重绘。

    <template>
      <div id="codeEditor">
        <div class="code-box">
          <JsMonacoEditor v-model="code" @change="changeCode" />
        </div>
        <div class="view-box">
          <el-button type="primary" size="small" @click="refreshChart">运行/刷新</el-button>
          <div ref="chart" class="chart" />
        </div>
      </div>
    </template>
    
    <script>
    import JsMonacoEditor from '@/businessComponents/jsMonacoEditor'
    
    let timer = null
    /*
    * @Description: 代码编辑器
    * @Author: linmt
    * @Date: @Date: 2022-09-11 09:01:31
    */
    export default {
      name: 'CodeEditor',
      components: {
        JsMonacoEditor
      },
      props: {
        echartDemoData: {
          type: Object,
          default: null
        }
      },
      data() {
        return {
          code: 'const option = {};',
          chart: null
        }
      },
      computed: {
    
      },
      watch: {
    
      },
      created() {
        if (this.echartDemoData) {
          this.code = this.echartDemoData.code
        }
      },
      mounted() {
        this.refreshChart()
      },
      beforeDestroy() {
        if (timer) window.clearInterval(timer)
      },
      methods: {
        changeCode() {
          this.refreshChart()
        },
        refreshChart() {
          let _option = null
          try {
            const funCode = new Function(`option=null;${this.code};return option;`)
            _option = funCode()
          } catch (e) {
            console.error(e)
          }
    
          if (!_option) {
            this.$message.error('代码存在问题')
            if (this.chart) {
              this.chart.clear()
            }
            return
          }
    
          if (_option) {
            if (!this.chart) {
              this.chart = window.echarts.init(this.$refs.chart)
            } else {
              this.chart.clear()
            }
    
            // 通过定时的方式进行echarts的大小重绘
            if (timer) window.clearInterval(timer)
            timer = setInterval(() => {
              if (this.chart) {
                this.chart.resize()
              }
            }, 200)
    
            this.chart.setOption(_option, true)
          }
        }
      }
    }
    
    </script>
    <style lang='scss' scoped>
    #codeEditor{
      height: 100%;
      width: 100%;
      display: flex;
      .code-box{
        width: 50%;
        height: 100%;
      }
      .view-box{
        width: 50%;
        height: 100%;
        padding-left: 10px;
        .chart{
          margin-top: 20px;
          height: calc(100% - 80px);
          width: 100%;
        }
      }
    }
    </style>
    

标签:option,chart,编辑器,editor,monaco,echarts,在线
From: https://www.cnblogs.com/linmt/p/16881332.html

相关文章

  • iTunes Connect在线创建 App
    创建完成环境文件后,在iTunesConnect上创建APP首先,进入(https://developer.apple.com/membercenter/index.action)选择iTunesConnect编辑切换为居中添加图片注释,......
  • 如何实现导入Word文档到ueditor编辑器中
    ueditor粘贴不能粘贴word中的图片是一个很头疼的问题,在我们的业务场景中客户要求必须使用ueditor并且支持word的图片粘贴,因为这个需求头疼了半个月,因为前端方面因为安全的......
  • vue-codemirror 代码编辑器
    codemirror是一个非常强大的代码编辑器插件,但官方并没有提供vue的支持版本,不过跟vue集成的步骤并不复杂,以下是具体实现更多精彩更多技术博客,请移步IT人才终生实训......
  • Ueditor集成Word导入(富文本编辑器)
    ​ 当前功能基于PHP,其它语言流程大致相同 1.新增上传wordjson配置在ueditor\php\config.json中新增如下配置:     /* 上传word配置 */    "wordAction......
  • 利用无线物联网控制器实现土壤温湿度的在线测量
    现如今物联网技术深入千家万户,带动各行各业的发展。比如快递分拣机器人,送餐机器人,智慧农业,智慧养殖等。物联网技术提供了更加快捷方便的生活,协助工人与工程师完成生产制造......
  • 百度富文本编辑器UEditor配置及功能实现详解
    ​ 当前功能基于PHP,其它语言流程大抵相同。大概流程:1.将docx文件上传到服务器中2.使用PHPoffice/PHPword实现将word转换为HTML3.将HTML代码返回并赋值到编辑器中......
  • VI文本编辑器
    ......
  • Session和Application实现网络在线聊天室实例
    login.aspx代码如下:<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Sample_chat_login.aspx.cs"Inherits="Sample_chart_login"%><!DOCTYPEhtmlPUBLIC"-//......
  • RCP添加一个多页编辑器
    其余代码参考RCP添加一个编辑器视图-信铁寒胜-博客园(cnblogs.com)1、在plugin中,添加一个新的编辑器 2、添加多页编辑器的类packagemyviewrcp2.editors;imp......
  • RCP添加一个编辑器视图
    RCP提供专门的编辑器视图,里面内置了很多编辑器的专用用户,如果修改后,视图中名称显示命名,提示已修改要进行保存。1、plugin.xml中添加视图view扩展点,和org.eclipse.ui.edito......