前提・実現したいこと
chart.js vue-chartjs chartjs-plugin-annotation を使いグラフの作成をしています。
最終的にはAPIで取得したデータのレコード数に応じて、基準線を引きたいと思っています。
(最大値、最小値)
発生している問題・エラーメッセージ
APIの実装の前に、動的に基準線の変更を行うサンプルを作ろうと思っているのですが
options. annotation.annotations[].value に渡す値を変更しても基準線は初期の値から更新されない状態です。
該当のソースコード
Chart.vue
1<template> 2 <div> 3 <div class="small"> 4 <line-chart :chart-data="datacollection" :options="options"></line-chart> 5 <button @click="changevalue">btn</button> 6 </div> 7 </div> 8</template> 9 10<script> 11import LineChart from "./LineChartjs"; 12export default { 13 components: { 14 LineChart, 15 }, 16 data() { 17 return { 18 datacollection: {}, 19 options: {}, 20 anotationValue: "20", //初期値 21 }; 22 }, 23 24 methods:{ 25 changevalue(){ 26 this.anotationValue =10; //ボタンを押して、value更新 27 } 28 }, 29 30 mounted() { 31 (this.datacollection = { 32 labels: ["January", "February", "March", "April", "May", "June"], 33 datasets: [ 34 { 35 label: "SCIOT-F2-track", 36 backgroundColor: "#f67979", 37 data: [10, 20, 30, 40, 50, 30], 38 }, 39 ], 40 }), 41 this.options = { 42 title: { 43 display: true, 44 fontSize: 35, 45 46 }, 47 scales: { 48 yAxes: [ 49 { 50 ticks: { 51 beginAtZero: true, 52 min: 0, 53 max: 100, 54 stepSize: 10, 55 }, 56 id: "y-axis-1", 57 type: "linear", 58 }, 59 ], 60 61 }, 62 annotation: { 63 annotations: [ 64 { 65 type: "line", 66 scaleID: "y-axis-1", 67 mode: "horizontal", 68 value: this.anotationValue, // ここで更新してほしい。 69 borderColor: "black", 70 borderWidth: 2, 71 label: { 72 enabled: true, 73 content: "目標", 74 }, 75 }, 76 ], 77 }, 78 responsive: true, 79 maintainAspectRatio: false, 80 }; 81 }, 82}; 83</script> 84
LineChartjs.js
1import { Line, mixins } from 'vue-chartjs' 2import chartjsPluginAnnotation from 'chartjs-plugin-annotation' 3const { reactiveProp } = mixins 4 5export default { 6 extends: Line, 7 mixins: [reactiveProp], 8 props: ['options'], 9 10 mounted() { 11 this.addPlugin(chartjsPluginAnnotation) 12 // this.chartData is created in the mixin. 13 // If you want to pass options please create a local options object 14 this.renderChart(this.chartData, this.options) 15 }, 16 17}
試したこと
LineChart.jsに追加
1watch: { 2 chartData () { 3 this.$data._chart.update() 4 } 5} //なんの変化もない状態
お力をお貸しください。
補足情報(FW/ツールのバージョンなど)
packegejson
1 "chart.js": "^2.9.3", 2 "chartjs-plugin-annotation": "^0.5.7", 3 "core-js": "^3.6.5", 4 "vue": "^2.6.11", 5 "vue-chartjs": "^3.5.1"
開発環境 VS-CODE
Windows10

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/10 01:36
2022/02/11 10:09