简体   繁体   English

在Vuejs app上重绘Highcharts

[英]Redraw Highcharts on Vuejs app

I'm building a little pension calculator where I want to show the value of the pension pot depending on the users current and retirement age. 我正在建立一个小养老金计算器,我想根据用户当前和退休年龄显示养老金罐的价值。

I'm looking to take the user's data, number fields bound to a Vue model and then computing the value of the pot based on that data. 我希望获取用户的数据,绑定到Vue模型的数字字段,然后根据该数据计算底池的值。

That works just fine but having an issue with not being able to have Highcharts redraw the chart when the models change. 这样可以正常运行,但是当模型发生变化时,无法让Highcharts重绘图表。

The redrawChart method fires but the chart stays the same with the console telling me Chart.redraw is not a function . redrawChart方法触发,但图表保持不变,控制台告诉我Chart.redraw is not a function

Redraw is an option for the Highcharts API so not sure where I'm failing. Redraw是Highcharts API的一个选项,因此不确定我失败的地方。

Here's the markup: 这是标记:

<div id="app">
 <input type="number" min="55" v-model.number="age"  v-on:change="redrawChart">{{ age }}
 <br>
<input type="number" min="1" max="1000000" v-model.number="currentPensionValue" v-on:change="redrawChart">{{ currentPensionValue }}
<br>
<input type="number" min="56" v-model.number="retireAge"  v-on:change="redrawChart">{{ retireAge }}
<br>

<Chart :data="data" :steven="steven" :age-pot-value="agePotValue"></Chart>

and the associated Vue code 以及相关的Vue代码

const Chart = Vue.component('Chart', {
    props: ['data', 'steven', 'agePotValue'],
    template: `
    <div>
        <p>{{ data }}</p>

        <h1>{{ steven }}</h1>
        <h1>{{ agePotValue }}</h1>
        <div id="thechart"></div>
    </div>
    `,
    mounted() {
        var highchartsOptions = {
            chart: {
                type: 'area',
                renderTo: 'thechart'
            },
            credits: {
                enabled: false
            },
            tooltip: {
                enabled: false
            },
            title: {
                text: ''
            },
            xAxis: {
                allowDecimals: false,
                title: {
                    text: 'Age'
                }
            },
            yAxis: {
                title: {
                  text: 'Pot Value'
                },
                labels: {
                    formatter: function () {
                    return '£' + this.value / 1000 + 'k';
                  }
                },
                opposite: true,
            },
            plotOptions: {},
            series: [{
            name: '',
            data: this.agePotValue
        }],
            credits: false
        }
            Highcharts.chart(highchartsOptions)
    }
});

new Vue({
    el: '#app',
    data: {
        age: 55,
        currentPensionValue: 22000,
        retireAge: 87
    },
    computed: {
        data() { return (this.currentPensionValue) / (this.retireAge - this.age) },
        steven() { return this.data * 1.4 },
        agePotValue() {
      var vm = this;
      var agePotValue = [[vm.age, (vm.data)], [vm.retireAge, vm.currentPensionValue]];

      return agePotValue;
    }
    },
    components: { Chart },
    methods: {
        redrawChart() {
      Chart.redraw();
        }
    }
})

Here's the the fiddle https://jsfiddle.net/stevieg_83/naLqzunz/11/ 这是小提琴https://jsfiddle.net/stevieg_83/naLqzunz/11/

Any help appreciated. 任何帮助赞赏。

Please see this working fiddle 请看这个工作小提琴

https://jsfiddle.net/naLqzunz/12/ https://jsfiddle.net/naLqzunz/12/

I make a little change on your approach, component will be watching for changes 我对你的方法做了一些改变,组件将关注变化

  watch:{
    data(){this.redraw()},
    steven(){this.redraw()},
    agePotValue(){this.redraw()},
  },

and redraws method just update chart data (that fires redraw automatically) 和重绘方法只是更新图表数据(自动触发重绘)

  methods:{
    redraw(){
        this.chart.series[0].setData(this.agePotValue,true);
    }
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM