javascript - 我们可以减少图表条之间的差距吗?
问题描述
我使用了chart.js 水平条。下面是我的代码。我想减少图像中 2 条中突出显示的间隙。
让 chartOptions = { responsive: true, responsiveAnimationDuration: 1, maintainApectRatio: true, aspectRatio: 1, legend: false, tooltips: { enabled: false }, gridlines: false, scales: { xAxes: [ { ticks: { display: false } ,堆叠:假,gridLines:{显示:假,drawBorder:假}}],yAxes:[{刻度:{显示:假},堆叠:真,barPercentage:0.7,categoryPercentage:0.5,gridLines:{显示:假, drawBorder: false } } ] }, 动画: { onComplete: this.generateImage, onProgress() { const chartInstance = this.chart; 常量 ctx = chartInstance.ctx; 常量数据集 = this.data.datasets[0]; 常量元 = chartInstance.controller.getDatasetMeta(0);
Chart.helpers.each(
meta.data.forEach((bar, index) => {
const value = this.data.datasets[0].data[index] + "%";
const label = this.data.labels[index];
const labelPositionX = 14;
const labelWidth =
ctx.measureText(label).width + labelPositionX;
ctx.textBaseline = "middle";
ctx.textAlign = "left";
ctx.fillStyle = "#333";
ctx.fillText(label, labelPositionX, bar._model.y);
ctx.fillText(value, 170, bar._model.y);
})
);
}
}
};
let ctx = document.getElementById("myChart_fdsSubmission");
let myChart = new Chart(ctx, {
type: "horizontalBar",
data: {
labels: ["Submitted Courses", "Not Submitted Courses"],
datasets: [
{
label: "%",
data: [
// this.fdSubmittedCoursesPercentageVal,
// this.fdNotSubmittedCoursesPercentageVal
// // , this.fdiaPercentageVal
10, 5
],
backgroundColor: "#6db4e0"
},
{
label: "Complete",
data: [
100 - this.fdSubmittedCoursesPercentageVal,
100 - this.fdNotSubmittedCoursesPercentageVal
// 100 - this.fdiaPercentageVal
],
backgroundColor: "#e7e7e7",
borderColor: "#e7e7e7",
hoverBackgroundColor: "#e7e7e7",
hoverBorderColor: "#e7e7e7"
}
]
},
options: chartOptions
});
this.chart = myChart;
},
还如何在栏上方放置标签,即已提交和未提交的课程
解决方案
推荐阅读
- python-3.x - 如何从 KivyMD 上的 bottombarApp 中删除空白覆盖
- linux - 如何在解析后不丢失其他列/行的内容的情况下解析特定列或数据?
- android - 我们应该在适配器中使用 Koin Inject 吗?或者我们应该尝试把这个责任交给 Activity/Fragment 和它的 ViewModels?
- postgresql - 天气数据仓库 netcdf 或 grib 的最佳解决方案
- angular - 如何获得仅在 redux 状态的对象数组中的属性值更改时才发出的 Observable?
- sonarqube - SonarQube 扫描失败时如何停止 GitHub Action 构建
- python - Pylint 日志检查器无法识别日志模块级功能
- php - 根据购物车总数添加免费产品 - 不工作,需要二次添加到购物车才能正常运行
- angular - RxJS - 如何仅在第一次订阅时触发可观察流
- windows - 清理来自 USB 磁条阅读器的文本输入