javascript - Chart.js v2 中水平条形图的圆角
问题描述
我正在尝试在 Chart.js v2 中制作一个带圆角的水平条形图。在这里遇到了这个答案:https ://stackoverflow.com/a/43281198/6935580 ,它显示了如何创建一个带圆角的垂直条形图。
这是我的代码。
Chart.helpers.drawRoundedTopRectangle = function (ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
// top right corner
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
// bottom right corner
ctx.lineTo(x + width, y + height);
// bottom left corner
ctx.lineTo(x, y + height);
// top left
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
};
Chart.elements.RoundedTopRectangle = Chart.elements.Rectangle.extend({
draw: function () {
const ctx = this._chart.ctx;
const vm = this._view;
let left, right, top, bottom, signX, signY, borderSkipped;
let borderWidth = vm.borderWidth;
if (!vm.horizontal) {
// bar
left = vm.x - vm.width / 2;
right = vm.x + vm.width / 2;
top = vm.y;
bottom = vm.base;
signX = 1;
signY = bottom > top ? 1 : -1;
borderSkipped = vm.borderSkipped || 'bottom';
} else {
// horizontal bar
left = vm.base;
right = vm.x;
top = vm.y - vm.height / 2;
bottom = vm.y + vm.height / 2;
signX = right > left ? 1 : -1;
signY = 1;
borderSkipped = vm.borderSkipped || 'left';
}
// Canvas doesn't allow us to stroke inside the width so we can
// adjust the sizes to fit if we're setting a stroke on the line
if (borderWidth) {
// borderWidth shold be less than bar width and bar height.
const barSize = Math.min(Math.abs(left - right), Math.abs(top - bottom));
borderWidth = borderWidth > barSize ? barSize : borderWidth;
const halfStroke = borderWidth / 2;
// Adjust borderWidth when bar top position is near vm.base(zero).
const borderLeft = left + (borderSkipped !== 'left' ? halfStroke * signX : 0);
const borderRight = right + (borderSkipped !== 'right' ? -halfStroke * signX : 0);
const borderTop = top + (borderSkipped !== 'top' ? halfStroke * signY : 0);
const borderBottom = bottom + (borderSkipped !== 'bottom' ? -halfStroke * signY : 0);
// not become a vertical line?
if (borderLeft !== borderRight) {
top = borderTop;
bottom = borderBottom;
}
// not become a horizontal line?
if (borderTop !== borderBottom) {
left = borderLeft;
right = borderRight;
}
}
// calculate the bar width and roundess
const barWidth = Math.abs(left - right);
// const roundness = this._chart.config.options.barRoundness || 0.5;
const roundness = 1.0;
const radius = barWidth * roundness * 0.5;
// keep track of the original top of the bar
const prevTop = top;
// move the top down so there is room to draw the rounded top
top = prevTop + radius;
const barRadius = top - prevTop;
ctx.beginPath();
ctx.fillStyle = vm.backgroundColor;
ctx.strokeStyle = vm.borderColor;
ctx.lineWidth = borderWidth;
// draw the rounded top rectangle
Chart.helpers.drawRoundedTopRectangle(ctx, left, (top - barRadius + 1), barWidth, bottom - prevTop, barRadius);
ctx.fill();
if (borderWidth) {
ctx.stroke();
}
// restore the original top value so tooltips and scales still work
top = prevTop;
},
});
Chart.defaults.roundedBar = Chart.helpers.clone(Chart.defaults.bar);
Chart.controllers.roundedBar = Chart.controllers.bar.extend({
dataElementType: Chart.elements.RoundedTopRectangle
});
这是codepen链接:https ://codepen.io/anon/pen/MGPYPX
我意识到我必须修改 drawRoundedTopRectangle 辅助函数。我不知道二次曲线是如何工作的。关于如何为水平图表修改它的任何想法?谢谢
解决方案
推荐阅读
- bash - 如何在 bash 中的 aws cli 命令中传递变量?
- c# - 不了解如何实现 WPF 导航任务 C#
- python - 如何从 numpy datetime 数组返回到 datetime?
- android - 编辑文本更改时在编辑文本下方显示下拉列表
- javascript - Javascript如何对表求和
- java - JFrame在设计选项卡中隐藏部分
- c++ - 我在 mingw 中收到“权限被拒绝”错误
- sql - 在 Oracle 中将虚拟行显示为按子句标题分组
- python - 代码不会写入 CSV 文件 Python3,为什么?
- reactjs - 在重新验证中收到响应时,是否有任何方法可以触发错误消息?