Skip to content

Commit d29d9b8

Browse files
committed
Fix empty candlesticks on ios
1 parent e739a46 commit d29d9b8

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

example/utils/generateData.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ export const generateMockData = () => {
3030
close: parseFloat(close.toFixed(2)),
3131
vol: parseFloat(volume.toFixed(2)) // Native code expects 'vol' not 'volume'
3232
})
33+
34+
// TO TEST EMPTY CANDLESTICKS UNCOMMENT THIS
35+
// if (i % 5 === 0) {
36+
// data[data.length - 1].vol = 0 // Simulate occasional volume spikes
37+
// data[data.length - 1].close = data[data.length - 1].open // Simulate occasional price stability
38+
// data[data.length - 1].high = data[data.length - 1].open // Simulate occasional price stability
39+
// data[data.length - 1].low = data[data.length - 1].open // Simulate occasional price stability
40+
// }
3341

3442
lastClose = close
3543
}

ios/Classes/HTMainDraw.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,43 @@ class HTMainDraw: NSObject, HTKLineDrawProtocol {
4949
} else {
5050
// Draw wick first (behind body)
5151
drawCandle(high: model.high, low: model.low, maxValue: maxValue, minValue: minValue, baseY: baseY, height: height, index: index, width: configManager.candleLineWidth, color: wickColor, verticalAlignBottom: false, context: context, configManager: configManager)
52+
5253
// Draw body second (on top)
53-
drawCandle(high: findValue(true), low: findValue(false), maxValue: maxValue, minValue: minValue, baseY: baseY, height: height, index: index, width: configManager.candleWidth, color: color, verticalAlignBottom: false, context: context, configManager: configManager)
54+
let candleHigh = findValue(true)
55+
let candleLow = findValue(false)
56+
57+
// Handle case when open == close (doji candlestick)
58+
if candleHigh == candleLow {
59+
// Draw a thin horizontal line to make the doji visible, similar to Android behavior
60+
drawCandleWithMinHeight(high: candleHigh, low: candleLow, maxValue: maxValue, minValue: minValue, baseY: baseY, height: height, index: index, width: configManager.candleWidth, color: color, context: context, configManager: configManager)
61+
} else {
62+
drawCandle(high: candleHigh, low: candleLow, maxValue: maxValue, minValue: minValue, baseY: baseY, height: height, index: index, width: configManager.candleWidth, color: color, verticalAlignBottom: false, context: context, configManager: configManager)
63+
}
64+
}
65+
}
66+
67+
func drawCandleWithMinHeight(high: CGFloat, low: CGFloat, maxValue: CGFloat, minValue: CGFloat, baseY: CGFloat, height: CGFloat, index: Int, width: CGFloat, color: UIColor, context: CGContext, configManager: HTKLineConfigManager) {
68+
let itemWidth = configManager.itemWidth
69+
let scale = (maxValue - minValue) / height
70+
let paddingHorizontal = (itemWidth - width) / 2.0
71+
let x = CGFloat(index) * itemWidth + paddingHorizontal
72+
let y = baseY + (maxValue - high) / scale
73+
74+
// Ensure minimum height of 1 pixel for doji candles (open == close)
75+
let minHeightInPixels: CGFloat = 1.0
76+
let candleHeight = max(minHeightInPixels, (high - low) / scale)
77+
78+
context.setFillColor(color.cgColor)
79+
80+
if configManager.candleCornerRadius > 0 {
81+
// Draw rounded rectangle
82+
let rect = CGRect(x: x, y: y, width: width, height: candleHeight)
83+
let path = UIBezierPath(roundedRect: rect, cornerRadius: configManager.candleCornerRadius)
84+
context.addPath(path.cgPath)
85+
context.fillPath()
86+
} else {
87+
// Draw regular rectangle with minimum height
88+
context.fill(CGRect.init(x: x, y: y, width: width, height: candleHeight))
5489
}
5590
}
5691

0 commit comments

Comments
 (0)