javascript - VueJs 和 ChartJs - 图表在宽度上响应,但不是高度?
问题描述
我几乎尝试了所有方法,但无法让图表的高度响应。
宽度工作得很好,它是响应式的并相应地调整,但由于某种原因高度保持锁定在 400 像素?
在我的主 Vue 应用程序中,我调用了我的组件(传入图表数据):
<ProjectedCumulativeSavings :datacollection="projectData.projectedCumulativeSavings" />
组件 ProjectedCumulativeSavings.vue 代码为:
<template>
<div class="chart-container" >
<bar-chart
:chart-data="datacollection"
:options="options"
chart-id="projectedCumulativeSavings"
/>
</div>
</template>
<script>
import BarChart from '../mixins/BarChart.js'
export default {
components: {
BarChart
},
props: {
datacollection: ''
},
data() {
return {
options: {
maintainAspectRatio: false,
responsive: true,
title: {
display: true,
text: 'Projected Cumulative Savings',
fontSize: 18
},
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
// Include a dollar sign in the ticks
callback: function (value, index, values) {
return 'R' + value.toLocaleString('en')
}
}
}]
}
}
}
},
created() {
},
computed: {
}
}
</script>
<style >
.chart-container {
min-width: 375px;
max-width: 1024px;
/* margin: 30px; */
position: relative;
width: 100%;
height: 40vw;
}
</style>
BarChart.js 代码是:
import { Bar, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins
export default {
extends: Bar,
mixins: [reactiveProp],
props: ['options'],
mounted() {
// this.chartData is created in the mixin.
// If you want to pass options please create a local options object
this.renderChart(this.chartData, this.options)
},
watch: {
chartData() {
this.$data._chart.update()
}
}
}
解决方案
唯一对我有用的是设置组件的高度和宽度:
<BarChart
:chart-data="chart2Data"
:options="chart2Options"
:key="chart2Trigger"
:height="2"
:width="4"
/>
推荐阅读
- mongodb - 大小为 300kb 的 MongoDB 文档需要 8-15 秒
- sql - 如何不重复计算查询中的行和对象?
- testing - 无法将赛普拉斯与测试管理工具集成
- php - MySQL 未在 Apache 中启动
- python - 如果字符串与列表中的字符串匹配,我如何从句子中删除字符串
- amazon-web-services - 我没有 API Gateway、ALB 或 Cloudfront,我的 AWS 环境中的 WAF 解决方案是什么
- docker - 模拟 SSH 转发到 Docker 容器
- machine-learning - 使用 LSTM 的多变量时间序列上的客户流失
- awk - 将列批量转换为带有文件名的行
- javascript - 尝试从控制台获取我的 javascript 时出现未定义的消息