Raymond Arteaga
Raymond Arteaga

Reputation: 4673

MPAndroidChart CombinedChart CandleStick real body not showing

I'm implementing a combined chart using MPAndroidChart library. The shadows are displaying correctly but the real body is missing.

This is the code I'm using for the CandleStick DataSet:

val ohlcSet = CandleDataSet(if(values.price.size>1)values.hloc else mutableListOf(CandleEntry(0f,0f,0f,0f,0f)), getString(R.string.price_ohlc))
ohlcSet.axisDependency=YAxis.AxisDependency.LEFT
ohlcSet.decreasingColor = resources.getColor(R.color.colorPrimaryDark)
ohlcSet.increasingColor= resources.getColor(R.color.colorPrimaryDark)
ohlcSet.neutralColor= resources.getColor(R.color.colorPrimaryDark)
ohlcSet.shadowColor = Color.DKGRAY
ohlcSet.barSpace = 0f
ohlcSet.valueTextSize = 10f
ohlcSet.setDrawValues(false)

val cData=CombinedData()
cData.setData(LineData(priceSet,volumeSet))
cData.setData(CandleData(ohlcSet))
chart1.data = cData

But the chart is not rendering correctly. Here's a screenshot: Screenshot of chart behavior

I've tried everything, I even removed the other data sets with no luck. Any help would be appreciated.

Upvotes: 3

Views: 577

Answers (1)

Raymond Arteaga
Raymond Arteaga

Reputation: 4673

I discovered the cause of the bug, turns out that when you use big long values like timestamps as the x value, the real body of the candle stick shrinks horizontally and is not displayed even when you zoom. A hack for solving the situation is scaling the x value, for example:

val ohlc=ArrayList<CandleEntry>()
ohlc.add(longTiestamp/5000000f,high, low, open, close)

and then add custom value formatters to display correctly the scaled value:

chart1.xAxis.valueFormatter = IAxisValueFormatter { value, _ ->
    val millis = (value*5000000f).toLong()
    DateFormat.getInstance().format(Date(millis))
}

Upvotes: 2

Related Questions