npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

echarts-middleware

v2.0.4

Published

在vue中轻巧优雅的使用echarts

Downloads

64

Readme

echarts-middleware

在vue中优雅,高效的使用echarts

2018年7月6日更新 可以自动检测图表数据变化

快速上手

安装

npm install -save echarts
npm install -save echarts-middleware

引入并注册

<script>
  import 'echarts'
  import Chart from 'echarts-middleware'
  export default {
    components: {
      Chart
    }
  }
</script>

使用

<template>
  <div class="my-chart">
    <Chart v-model="mock" :size="{w: 400, h: 400}"></Chart>
  <div>
</template>

<script>
  import Chart from 'echarts-middleware'
  export default {
    components: {
      Chart
    },
    data () {
      return {
        mock: {
          'series': [{
            'name': 'gauge',
            'type': 'gauge',
            'detail': {'formatter': '{value}'},
            'data': [{'value': 34}]
          }]
        }
      }
    }
  }
</script>

结果 结果

参数

| 参数名     | 含义         | 类型  | 是否必须 | | ----------- |:-------------:| -----:| -----:| |value| 图表的配置 | Object | true | |size| 图表的宽高| Object | false | |theme| 主题| String, Object | false | |renderer| 渲染器| String | false |

事件

| 事件名       | 触发情况         | 返回值类型  | | ----------- |:-------------:| -----:| |init| 图表注册完成 | echartsInstance(Echart实例) |

使用实例:

<Chart v-model="data" :size="{w: 400, h: 400}"></Chart>

注:如果 w h 参数没有设置,组件会以父组件宽高作为组件宽高, 如果父组件没有宽高则会以400px作为宽高。

组件特点

  • 高效: 直接将echarts实例暴露给vue,可以直接操作
  • 优雅: 自动处理echarts的 注册、重新赋值 及 销毁 操作
  • 简洁: 主要代码少于50行 没有性能损失
  • 接口丰富: 由于直接暴露 echarts实例 所以理论上能用 100% echarts方法

简单对比

传统vue中使用echarts

<template>
  <div class="box">
    <div class="charts" v-for="item in 6" :id="'chart' + item" :key="item"></div>
  </div>
</template>

<script>
  import echarts from 'echarts'
  let myChart = []
  export default {
    data () {
      return {
        mock: {
          'series': [{
            'name': 'gauge',
            'type': 'gauge',
            'detail': {'formatter': '{value}'},
            'data': [{'value': 34}]
          }]
        }
      }
    },
    created () {
      for (let i = 0; i < 6; i++) {
        myChart[i] = echarts.init(document.getElementById('chart' + i))
        myChart[i].setOption(this.mock)
      }
    },
    beforeDestroy () {
      for (let i = 0; i < 6; i++) {
        myChart[i].dispose()
      }
    }
  }
</script>

使用echarts-middleware达到同样的效果

<template>
  <div class="box">
    <Chart v-for="item in 6" v-model="mock" :key="item"></div>
  </div>
</template>

<script>
  import echarts from 'echarts'
  import Chart from 'echarts-middleware'
  export default {
    components: {
      Chart
    },
    data () {
      return {
        mock: {
          'series': [{
            'name': 'gauge',
            'type': 'gauge',
            'detail': {'formatter': '{value}'},
            'data': [{'value': 34}]
          }]
        }
      }
    }
  }
</script>

获取Echarts对象的方法

1.使用init事件取得Echarts实例

<template>
  <div>
    <Chart v-model="mock" :size="{w: 400, h: 400}", @init="chartInit"></Chart>
    <button @click="click">销毁图表</button>
  </div>
</template>
<script>
  import Chart from 'echarts-middleware'
  export default {
    components: {
      Chart
    },
    data () {
      return {
        chart: null,
        mock: {
          'series': [{
            'name': 'gauge',
            'type': 'gauge',
            'detail': {'formatter': '{value}'},
            'data': [{'value': 34}]
          }]
        }
      }
    },
    methods: {
      // 图表初始化完毕后的回调
      chartInit (chart) {
        this.chart = chart
      },
      click () {
        console.log('输出echarts对象', this.chart)
        console.log('销毁echarts图表')
        this.chart.dispose()
      }
    }
  }
</script>

支持的方法,理论支持echarts所有方法,具体方法列表请查阅 API文档