Skip to content

Commit d5596c8

Browse files
committed
fix: bar chart custom rendering fix
1 parent 68a0ab5 commit d5596c8

File tree

7 files changed

+43
-31
lines changed

7 files changed

+43
-31
lines changed

src/ui-chart/buffer/AbstractBuffer.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export abstract class AbstractBuffer<T> {
88
protected to: number;
99

1010
public buffer;
11+
public entries: T[];
1112

1213
/**
1314
* Initialization with buffer-size.

src/ui-chart/buffer/BarBuffer.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class BarBuffer extends AbstractBuffer<IBarDataSet> {
1919
this.containsStacks = containsStacks;
2020
}
2121

22-
protected addBar(left, top, right, bottom) {
22+
protected addBar(entry, left, top, right, bottom) {
23+
this.entries.push(entry);
2324
this.buffer[this.index++] = left;
2425
this.buffer[this.index++] = top;
2526
this.buffer[this.index++] = right;
@@ -30,6 +31,7 @@ export class BarBuffer extends AbstractBuffer<IBarDataSet> {
3031
const size = data.entryCount * this.phaseX;
3132
const barWidthHalf = this.barWidth / 2;
3233
const yKey = data.yProperty;
34+
this.entries = [];
3335
for (let i = 0; i < size; i++) {
3436
const e = data.getEntryForIndex(i);
3537
if (!e) {
@@ -63,7 +65,7 @@ export class BarBuffer extends AbstractBuffer<IBarDataSet> {
6365
bottom = top + this.phaseY * (bottom - top);
6466
}
6567
if (left !== right && top !== bottom) {
66-
this.addBar(left, top, right, bottom);
68+
this.addBar(e, left, top, right, bottom);
6769
}
6870
} else {
6971
let posY = 0;
@@ -108,12 +110,13 @@ export class BarBuffer extends AbstractBuffer<IBarDataSet> {
108110
}
109111

110112
if (left !== right && top !== bottom) {
111-
this.addBar(left, top, right, bottom);
113+
this.addBar(e, left, top, right, bottom);
112114
}
113115
}
114116
}
115117
}
116-
118+
const addedBars = this.index / 4;
117119
this.reset();
120+
return addedBars;
118121
}
119122
}

src/ui-chart/buffer/HorizontalBarBuffer.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ export class HorizontalBarBuffer extends BarBuffer {
2020
let y = e[yKey];
2121
const vals = e.yVals;
2222

23-
if (!this.containsStacks || !vals) {
23+
if (!this.containsStacks || !vals || vals.length === 0) {
24+
if (y === undefined || y === null) {
25+
continue;
26+
}
2427
const bottom = x - barWidthHalf;
2528
const top = x + barWidthHalf;
2629
let left, right;
@@ -38,7 +41,7 @@ export class HorizontalBarBuffer extends BarBuffer {
3841
} else {
3942
left = right + this.phaseY * (left - right);
4043
}
41-
this.addBar(left, top, right, bottom);
44+
this.addBar(e, left, top, right, bottom);
4245
} else {
4346
let posY = 0;
4447
let negY = -e.negativeSum;
@@ -76,10 +79,12 @@ export class HorizontalBarBuffer extends BarBuffer {
7679
left = right + this.phaseY * (left - right);
7780
}
7881

79-
this.addBar(left, top, right, bottom);
82+
this.addBar(e, left, top, right, bottom);
8083
}
8184
}
8285
}
86+
const barsCount = this.index / 4;
8387
this.reset();
88+
return barsCount;
8489
}
8590
}

src/ui-chart/charts/BarChart.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Highlight } from '../highlight/Highlight';
1010
import { BarChartRenderer } from '../renderer/BarChartRenderer';
1111
import { Canvas, Paint, RectF } from '@nativescript-community/ui-canvas';
1212
import { IBarDataSet } from '../interfaces/datasets/IBarDataSet';
13-
import { Color } from '@nativescript/core';
1413
import { BaseCustomRenderer } from '../renderer/DataRenderer';
1514

1615
const LOG_TAG = 'BarChart';

src/ui-chart/data/BaseDataSet.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ export abstract class BaseDataSet<T extends Entry> implements IDataSet<T> {
148148
}
149149

150150
public getEntryXValue(e: T, entryIndex: number) {
151-
if (!this.xProperty) {
152-
return entryIndex;
153-
}
154-
return e[this.xProperty];
151+
return this.xProperty ? e[this.xProperty] : entryIndex;
155152
}
156153

157154
public getEntryIcon(e: T) {

src/ui-chart/renderer/BarChartRenderer.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
139139
buffer.yAxisMin = chart.getAxis(dataSet.axisDependency).axisMinimum;
140140
buffer.yAxisMax = chart.getAxis(dataSet.axisDependency).axisMaximum;
141141

142-
buffer.feed(dataSet);
142+
const barsCount = buffer.feed(dataSet);
143143

144144
trans.pointValuesToPixel(buffer.buffer);
145145

@@ -156,27 +156,30 @@ export class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
156156
}
157157

158158
const customRender = chart.customRenderer;
159-
for (let j = 0; j < buffer.length; j += 4) {
160-
if (!this.mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2])) {
159+
for (let j = 0; j < barsCount; j += 1) {
160+
const left = buffer.buffer[j * 4];
161+
const top = buffer.buffer[j * 4 + 1];
162+
const right = buffer.buffer[j * 4 + 2];
163+
const bottom = buffer.buffer[j * 4 + 3];
164+
if (!this.mViewPortHandler.isInBoundsLeft(right)) {
161165
continue;
162166
}
163167

164-
if (!this.mViewPortHandler.isInBoundsRight(buffer.buffer[j])) {
168+
if (!this.mViewPortHandler.isInBoundsRight(left)) {
165169
break;
166170
}
167171
if (!isSingleColor) {
168172
// Set the color for the currently drawn value. If the index
169173
// is out of bounds, reuse colors.
170-
renderPaint.setColor(dataSet.getColor(j / 4));
174+
renderPaint.setColor(dataSet.getColor(j));
171175
}
172176
if (customRender && customRender.drawBar) {
173-
const e = dataSet.getEntryForIndex(j / 4);
174-
customRender.drawBar(c, e, dataSet, buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], renderPaint);
177+
const e = buffer.entries[j];
178+
customRender.drawBar(c, e, dataSet, left, top, right, bottom, renderPaint);
175179
} else {
176-
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], renderPaint);
177-
180+
c.drawRect(left, top, right, bottom, renderPaint);
178181
if (drawBorder) {
179-
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], borderPaint);
182+
c.drawRect(left, top, right, bottom, borderPaint);
180183
}
181184
}
182185
}

src/ui-chart/renderer/HorizontalBarChartRenderer.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class HorizontalBarChartRenderer extends BarChartRenderer {
8585
buffer.yAxisMin = this.mChart.getAxis(dataSet.axisDependency).axisMinimum;
8686
buffer.yAxisMax = this.mChart.getAxis(dataSet.axisDependency).axisMaximum;
8787

88-
buffer.feed(dataSet);
88+
const barsCount = buffer.feed(dataSet);
8989

9090
trans.pointValuesToPixel(buffer.buffer);
9191

@@ -102,27 +102,31 @@ export class HorizontalBarChartRenderer extends BarChartRenderer {
102102
}
103103

104104
const customRender = this.mChart.customRenderer;
105-
for (let j = 0, pos = 0; j < buffer.length; j += 4, pos++) {
106-
if (!this.mViewPortHandler.isInBoundsTop(buffer.buffer[j + 3])) {
105+
for (let j = 0, pos = 0; j < barsCount; j += 1, pos++) {
106+
const left = buffer.buffer[j * 4];
107+
const top = buffer.buffer[j * 4 + 1];
108+
const right = buffer.buffer[j * 4 + 2];
109+
const bottom = buffer.buffer[j * 4 + 3];
110+
if (!this.mViewPortHandler.isInBoundsTop(bottom)) {
107111
break;
108112
}
109113

110-
if (!this.mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1])) {
114+
if (!this.mViewPortHandler.isInBoundsBottom(top)) {
111115
continue;
112116
}
113117
if (!isSingleColor) {
114118
// Set the color for the currently drawn value. If the index
115119
// is out of bounds, reuse colors.
116-
renderPaint.setColor(dataSet.getColor(j / 4));
120+
renderPaint.setColor(dataSet.getColor(j));
117121
}
118122
if (customRender && customRender.drawBar) {
119-
const e = dataSet.getEntryForIndex(j / 4);
120-
customRender.drawBar(c, e, dataSet, buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], renderPaint);
123+
const e = buffer.entries[j];
124+
customRender.drawBar(c, e, dataSet, left, top, right, bottom, renderPaint);
121125
} else {
122-
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], renderPaint);
126+
c.drawRect(left, top, right, bottom, renderPaint);
123127

124128
if (drawBorder) {
125-
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2], buffer.buffer[j + 3], borderPaint);
129+
c.drawRect(left, top, right, bottom, borderPaint);
126130
}
127131
}
128132
}

0 commit comments

Comments
 (0)