ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Candlestick Chart Issue with Version 4.0.2 (Pod was updated from 3.6.0)

Spencer-Villarreal opened this issue · comments

I run 'pod update' about every single day just to make sure I am up-to-date on the latest releases. Today the Charts pod got updated from version 3.6.0 to 4.0.2 and now my candlestick chart is unusable.

When I was using the 3.6.0 version I followed the swift demo to get everything set up, but once I updated to 4.0.2 the whole chart does not show data correctly anymore. As of right now, only one candlestick is showing on the far left side and no other candles are shown on the screen. I cannot tell if all of the candles are being rendered on the far left side or if only one candle is being shown.

There are 19 chart values but only 1 value is being shown on the far left side. I set the x-axis to be the same amount of chart values + 2 for some spacing.

I have downgraded back to 3.6.0 and it goes back to normal/works completely fine. Does anyone know what could possibly be happening from the update?

If it is possible could you create a new candlestick chart demo for the new 4.0.0 update?

I have the same situation with a multiple-set line chart. Debugging shows multiple datasets in the LineChartData object, but only the first will be displayed. There was some sort of breaking change that's not apparent to me in the 4.x update. The demo version of the multiple line charts still works correctly, but it's the same example code I used in my original implementation.

@Spencer-Villarreal, use the fix that's in this PR: #4687

I realized that I'd manually fixed this back in the 3.x days and pulling the new 4.0.2 on a new machine doesn't my manual change.

Same issue. When will we see a fix for this?

Here's the issue and (not thoroughly tested) fix. Sorry, I'm not a Swift guy, so I'm not comfortable pulling/posting this fix...

The drawDataset method in CandleStickRenderer.swift sets the min, max and range values for _xBounds

_xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)

The XBounds setter is found in BarLineScatterCandleBubbleRenderer.swift. The entryTo value is what's not getting set correctly.

let entryTo = dataSet.entryForXValue(high, closestToY: .nan, rounding: .up)

This method calls the entryIndex method in ChartDataSet.swift that tries to find the closest X value.

open override func entryForXValue(
        _ xValue: Double,
        closestToY yValue: Double,
        rounding: ChartDataSetRounding) -> ChartDataEntry?
    {
        let index = entryIndex(x: xValue, closestToY: yValue, rounding: rounding)
        if index > -1
        {
            return self[index]
        }
        return nil
    }

The problem is that it's greater than the number of dataset elements, so it returns -1.
I changed the logic to use the last element when the closest value is beyond the number of elements.
//The fix:

open override func entryIndex(
    x xValue: Double,
    closestToY yValue: Double,
    rounding: ChartDataSetRounding) -> Int
{
   var closest = partitioningIndex { $0.x >= xValue }
   //fix begin
   if closest >= endIndex
   {
     closest = endIndex-1
   }
   //fix end
   guard closest < endIndex else { return -1 }
...
...

Again, sorry for my lack of Swift experience. I'm sure there's a more elegant solution to this. I think I'm the last Objective-C holdout...