目录
基本概念
如下的例子,刚开始运行:
点击按钮:
数据库修改下数据:
再点击按钮刷新下:
下面给出请求的json数据:
刷新有2个方式
第一种是强制刷新,这样是有问题的,数据和图表不能同步:
this.$forceUpdate()
本人推荐下面这种方式,通过绑定组建的key值
<barChartItem ref="barItem" v-bind:count="numCount" v-bind:valueList="valueList" v-bind:key="valueList.toString()"></barChartItem>
把key绑定成变动的值,就可以实时刷新了
代码
程序结构如下:
关键代码
BarCharts.vue
- <template>
- <div>
- <div id="main" style="width: 500px; height:500px"></div>
- </div>
- </template>
-
- <script>
- export default {
- props: ['count', 'valueList'],
- watch:{
- count:val => {
- alert(this.count)
- }
- },
- mounted(){
- this.initGra();
- },
- methods: {
- //开始画图了
- initGra(){
-
- // 基于准备好的dom,初始化echarts实例
- let myChart = this.$echarts.init(document.getElementById('main'));
- this.getData();
- },
- getData(){
-
- if (myChart != null && myChart != "" && myChart != undefined) {
- myChart.dispose();
- }
-
- // 基于准备好的dom,初始化echarts实例
- let myChart = this.$echarts.init(document.getElementById('main'));
-
- //初始化数据
- let dataList = [];
- for(let i = 0; i < this.count; i++){
-
- dataList.push(this.valueList[i]);
- }
-
- console.log(this.count)
- console.log(this.valueList)
- //alert(this.count)
- //alert(this.valueList)
-
- // 指定图表的配置项和数据
- let option = {
- title: {
- text: 'ECharts 入门示例'
- },
- tooltip: {},
- legend: {
- data:['销量']
- },
- xAxis: {
- data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
- },
- yAxis: {},
- series: [{
- name: '销量',
- type: 'bar',
- data: dataList
- }]
- };
-
- // 使用刚指定的配置项和数据显示图表。
- myChart.setOption(option);
- }
- }
- }
- </script>
-
- <style scoped>
- .item{
- color: green;
- }
- </style>
main.js
- // The Vue build version to load with the `import` command
- // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
- import Vue from 'vue'
- import Axios from 'axios'
- import echarts from 'echarts'
- import TestEChart from './TestEChart'
-
- Vue.config.productionTip = false
- Vue.prototype.$axios=Axios
- Vue.prototype.$echarts = echarts
-
- /* eslint-disable no-new */
- new Vue({
- el: '#app',
- components: { TestEChart },
- template: '<TestEChart/>',
- })
TestEChart.vue
- <template>
- <div>
- <barChartItem ref="barItem" v-bind:count="numCount" v-bind:valueList="valueList" v-bind:key="valueList.toString()"></barChartItem>
- <div>
- <button @click="handleSubmit">获取</button>
- </div>
- </div>
- </template>
-
- <script>
-
- import BarChartItem from './components/BarCharts'
-
- export default {
- data() {
- return {
- numCount: 0,
- valueList: []
- }
- },
- components: {
- 'barChartItem' : BarChartItem
- },
- methods: {
- handleSubmit(){
- this.$axios({
- type: 'get',
- url: '/gra/list'
- }).then(res => {
-
- //console.log(res.data.data)
-
- let jsonTest = res.data.data.content
-
- //目前只有1条数据 展示这么写
- this.numCount = jsonTest[0].count
- this.valueList = jsonTest[0].value;
-
- //console.log(this.numCount)
- //console.log(this.valueList)
-
- this.$refs.barItem.getData()
- //this.$set()
- //this.$forceUpdate()
- }).catch(err=>{
-
- alert(err)
- })
- },
- }
- }
- </script>
-
- <style>
-
- </style>