2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vue-chartjsでデータの数に応じてグラフサイズを変える

Last updated at Posted at 2020-03-30

やりたいこと

  1. Horizontal Barグラフでデータ数が増えたときに、ラベルが間引かれるのを防ぎたい。
  2. さらに、Barが小さくなるのではなく、グラフの方が大きくなって欲しい。

1.に対する解決策

autoSkip: falseを設定する。

yAxes: [{
  ticks: {
    autoSkip: false,  //ラベルを間引かない
    fontSize: 10,
  },
}]

2.に対する解決策

基本的にresponsive: true, maintainAspectRatio: falseの2つを設定すれば実現可能。

しかし今回は開閉するパネル内にグラフを配置しており、パネルの開閉時にグラフの高さが0から増えないという現象に悩まされたので、明示的に高さを指定するようにする。

data () {
  labels: this.label,
  datasets: [{
    data: this.value,
  }],
  return {
    options: {
      responsive: true,  //レスポンシブ
      maintainAspectRatio: true //縦横は固定
    },
  }
}

mounted () {
    this.$refs.canvas.height = this.value.length*17.5; //データ数に応じて高さ指定
    this.renderChart(this.data, this.options)
  },
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?