Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#791][3.0] Bar Chart - borderRadius 옵션 추가 #795

Merged
merged 4 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/views/barChart/api/barChart.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const chartData = {
| width | String / Number | '100%' | 차트의 너비 | '100%', '150px', 150 |
| height | String / Number | '100%' | 차트의 높이 | '100%', '150px', 150 |
| thickness | Number | 1 | 차트 막대의 너비 | 0.1 ~ 1 |
| borderRadius | Number | 0 | 막대 가장자리 부분의 border-radius 값. | 0 ~ |
kdeun1 marked this conversation as resolved.
Show resolved Hide resolved
| horizontal | Boolean | false | 차트 막대의 방향 - 수평 전환 여부 | true, false |
| axesX | Object | 없음 | X축에 대한 속성 | [상세](#axesx-axesy) |
| axesY | Object | 없음 | Y축에 대한 속성 | [상세](#axesx-axesy) |
Expand Down
1 change: 1 addition & 0 deletions docs/views/barChart/example/Gradient.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

const chartOptions = {
type: 'bar',
borderRadius: 14,
axesX: [{
type: 'step',
}],
Expand Down
3 changes: 2 additions & 1 deletion docs/views/barChart/example/Horizontal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
data: {
series1: [1500, 80, 120, 30, 0],
series1: [1500, 180, 30, 10, 0],
},
};

Expand All @@ -32,6 +32,7 @@
position: 'right',
},
horizontal: true,
borderRadius: 15,
axesX: [{
type: 'linear',
startToZero: true,
Expand Down
4 changes: 2 additions & 2 deletions src/components/chart/chart.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class EvChart {
* @returns {undefined}
*/
drawSeries() {
const thickness = this.options.thickness;
const maxTip = this.options.maxTip;

const opt = {
Expand Down Expand Up @@ -176,7 +175,8 @@ class EvChart {
if (chartType === 'line' || chartType === 'scatter') {
series.draw(opt);
} else if (chartType === 'bar') {
series.draw({ thickness, showSeriesCount, showIndex, ...opt });
const { thickness, borderRadius } = this.options;
series.draw({ thickness, borderRadius, showSeriesCount, showIndex, ...opt });

if (series.show) {
showIndex++;
Expand Down
79 changes: 75 additions & 4 deletions src/components/chart/element/element.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class Bar {
this.size.bPad = bPad;
this.size.w = w;
this.size.ix = barSeriesX;
this.chartRect = chartRect;
this.labelOffset = labelOffset;
this.borderRadius = param.borderRadius;

let categoryPoint = null;

Expand Down Expand Up @@ -138,7 +141,10 @@ class Bar {
ctx.fillStyle = `rgba(${Util.hexToRgb(this.color)},${opacity})` || '';
}

ctx.fillRect(x, y, w, isHorizontal ? -h : h);
this.drawBar({
ctx,
positions: { x, y, w, h },
});

if (showValue.use) {
this.drawValueLabels({
Expand Down Expand Up @@ -169,7 +175,8 @@ class Bar {
* @returns {undefined}
*/
itemHighlight(item, context) {
const { showValue, isHorizontal } = this;
const showValue = this.showValue;

const gdata = item.data;
const ctx = context;

Expand All @@ -184,15 +191,20 @@ class Bar {
ctx.shadowBlur = 4;

if (typeof this.color !== 'string') {
const grd = Canvas.createGradient(ctx, isHorizontal, { x, y, w, h }, this.color);
const grd = Canvas.createGradient(ctx, this.isHorizontal, { x, y, w, h }, this.color);
ctx.fillStyle = grd;
ctx.shadowColor = this.color[this.color.length - 1][1];
} else {
ctx.fillStyle = this.color;
ctx.shadowColor = this.color;
}

ctx.fillRect(x, y, w, h);
ctx.beginPath();

this.drawBar({
ctx,
positions: { x, y, w, h: this.isHorizontal ? -h : h },
});

if (showValue.use) {
this.drawValueLabels({
Expand Down Expand Up @@ -309,6 +321,7 @@ class Bar {
const ctx = context;

ctx.save();
ctx.beginPath();

const value = numberWithComma(isHorizontal ? data.x : data.y);

Expand Down Expand Up @@ -362,6 +375,64 @@ class Bar {

ctx.restore();
}

drawBar({ ctx, positions }) {
const isHorizontal = this.isHorizontal;
const chartRect = this.chartRect;
const labelOffset = this.labelOffset;
const isStackBar = 'stackIndex' in this;
const { x, y } = positions;
let { w, h } = positions;
let r = this.borderRadius;

// Dont's draw bar that has value 0
if (w === 0 || h === 0) {
return;
}

if (r && r > 0 && !isStackBar) {
const squarePath = new Path2D();
squarePath.rect(
chartRect.x1 + labelOffset.left,
chartRect.y1,
chartRect.chartWidth - labelOffset.right,
chartRect.chartHeight - labelOffset.bottom,
);

ctx.clip(squarePath);

ctx.moveTo(x, y);

if (isHorizontal) {
if (h < r * 2) {
r = h / 2;
}

w -= r;
ctx.lineTo(x + w, y);
ctx.arcTo(x + w + r, y, x + w + r, y - r, r);
ctx.arcTo(x + w + r, y - h, x + w, y - h, r);
ctx.lineTo(x, y - h);
ctx.lineTo(x, y);
} else {
if (w < r * 2) {
r = w / 2;
}

h += r;
ctx.lineTo(x + w, y);
ctx.lineTo(x + w, y + h);
ctx.arcTo(x + w, y + h - r, x + w - r, y + h - r, r);
ctx.arcTo(x, y + h - r, x, y + h, r);
ctx.lineTo(x, y);
}

ctx.fill();
ctx.closePath();
} else {
ctx.fillRect(x, y, w, isHorizontal ? -h : h);
}
}
}

export default Bar;
1 change: 1 addition & 0 deletions src/components/chart/uses.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const DEFAULT_OPTIONS = {
width: '100%',
height: '100%',
thickness: 1,
borderRadius: 0,
combo: false,
tooltip: {
use: true,
Expand Down