Skip to content

Commit c6f544f

Browse files
committed
refactor candlestick code and add types
1 parent c7dbc6a commit c6f544f

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/charts/BoxCandleStick.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,29 +216,26 @@ class BoxCandleStick extends Bar {
216216
let i = indexes.i
217217
let j = indexes.j
218218

219-
const candleColors = w.config.plotOptions.candlestick.colors
220-
let isPositive = true
221-
let realIndex = indexes.realIndex
222-
let colorPos = Array.isArray(candleColors.upward)
223-
? candleColors.upward[realIndex]
224-
: candleColors.upward
225-
let colorNeg = Array.isArray(candleColors.downward)
226-
? candleColors.downward[realIndex]
227-
: candleColors.downward
228-
let color = ''
219+
const { colors: candleColors } = w.config.plotOptions.candlestick
220+
const { colors: boxColors } = this.boxOptions
221+
const realIndex = indexes.realIndex
229222

230-
if (this.isBoxPlot) {
231-
color = [this.boxOptions.colors.lower, this.boxOptions.colors.upper]
232-
}
223+
const getColor = (color) =>
224+
Array.isArray(color) ? color[realIndex] : color
225+
226+
const colorPos = getColor(candleColors.upward)
227+
const colorNeg = getColor(candleColors.downward)
233228

234229
const yRatio = this.yRatio[indexes.translationsIndex]
235230

236231
const ohlc = this.getOHLCValue(realIndex, j)
237232
let l1 = zeroH
238233
let l2 = zeroH
239234

240-
if (ohlc.o > ohlc.c) {
241-
isPositive = false
235+
let color = ohlc.o < ohlc.c ? [colorPos] : [colorNeg]
236+
237+
if (this.isBoxPlot) {
238+
color = [getColor(boxColors.lower), getColor(boxColors.upper)]
242239
}
243240

244241
let y1 = Math.min(ohlc.o, ohlc.c)
@@ -336,7 +333,7 @@ class BoxCandleStick extends Bar {
336333
indexes.translationsIndex
337334
),
338335
barXPosition,
339-
color: this.isBoxPlot ? color : isPositive ? [colorPos] : [colorNeg],
336+
color,
340337
}
341338
}
342339

types/apexcharts.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,17 +574,17 @@ type ApexPlotOptions = {
574574
}
575575
candlestick?: {
576576
colors?: {
577-
upward?: string
578-
downward?: string
577+
upward?: string | string[]
578+
downward?: string | string[]
579579
}
580580
wick?: {
581581
useFillColor?: boolean
582582
}
583583
}
584584
boxPlot?: {
585585
colors?: {
586-
upper?: string,
587-
lower?: string
586+
upper?: string | string[]
587+
lower?: string | string[]
588588
}
589589
}
590590
heatmap?: {

0 commit comments

Comments
 (0)