123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <template>
- <view class="content">
- <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts">
- </view>
- </view>
- </template>
- <script>
- import echarts from 'static/echarts.min.js'
- export default {
- name: 'chart',
- props: {
- },
- data() {
- return {
- option: {
- tooltip: {
- formatter: "{a} <br/>{b} : {c}%"
- },
- toolbox: {
- feature: {
- restore: {},
- saveAsImage: {}
- }
- },
- series: [{
- name: '业务指标',
- type: 'gauge',
- detail: {
- formatter: '{value}%'
- },
- data: [{
- value: 50,
- name: '完成率'
- }],
- axisLine: {
- show: true,
- lineStyle: {
- color: [
- [1, new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
- offset: 0.1,
- color: "#FFC600"
- },
- {
- offset: 0.6,
- color: "#30D27C"
- },
- {
- offset: 1,
- color: "#0B95FF"
- }
- ])]
- ]
- }
- }
- }]
- }
- }
- },
- onLoad() {
- },
- methods: {
- changeOption() {
- const data = this.option.series[0].data
- // 随机更新示例数据
- data.forEach((item, index) => {
- data.splice(index, 1, Math.random() * 40)
- })
- },
- onViewClick(options) {
- console.log(options)
- }
- }
- }
- </script>
- <script module="echarts" lang="renderjs">
- let myChart
- export default {
- mounted() {
- if (typeof window.echarts === 'function') {
- this.initEcharts()
- } else {
- // 动态引入较大类库避免影响页面展示
- const script = document.createElement('script')
- // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
- script.src = 'static/echarts.min.js'
- script.onload = this.initEcharts.bind(this)
- document.head.appendChild(script)
- }
- },
- methods: {
- //串联引入,保证文件加载顺序,在onload 方法内引入下一个js ,在最后一个onload方法内进行图表初始化
- // init() {
- // // 动态引入较大类库避免影响页面展示
- // const script = document.createElement('script')
- // script.src = 'static/china.js'
- // script.onload = this.initEcharts.bind(this)
- // document.head.appendChild(script)
- // },
- initEcharts() {
- myChart = echarts.init(document.getElementById('echarts'))
- // 观测更新的数据在 view 层可以直接访问到
- myChart.setOption(this.option)
- },
- updateEcharts(newValue, oldValue, ownerInstance, instance) {
- // 监听 service 层数据变更
- myChart.setOption(newValue)
- },
- onClick(event, ownerInstance) {
- // 调用 service 层的方法
- ownerInstance.callMethod('onViewClick', {
- test: 'test'
- })
- }
- }
- }
- </script>
- <style>
- .content {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- }
- .echarts {
- width: 100%;
- height: 400rpx;
- }
- </style>
|