Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

highlightValue is throwing "fatal error: Index out of range" in Combined Chart #2076

Closed
vikramezhil opened this issue Jan 15, 2017 · 12 comments

Comments

@vikramezhil
Copy link

I am using the combined chart (Bar and line) and when I try to highlight the bar programatically using the below code, i get an index out of range error. I am trying to set the first bar in the chart highlighted, but it is not working as expected.

highlightValue(x: 0, dataSetIndex: 0)

There is no chance of count mismatch between the x and y values or empty count in my code as these checks are placed beforehand and only after the condition is satisfied the chart is set. I even tried setting this after notifyDataSetChanged, but still the issue remains the same.

Strange enough I never encountered this issue in v2.2.5 or before and this issue seems to have arisen in v3.0. Any help would be appreciated, Thanks!

Note: All the highlightValue functions throw this error.

@liuxuan30
Copy link
Member

can you give where it crashes? You have to post the back trace so we know more details..

@vikramezhil
Copy link
Author

vikramezhil commented Jan 17, 2017

My bad sorry about that, below is the full back trace of the error.

* thread #1: tid = 0x6d530, 0x01e002e0 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> Swift.Never + 124, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)
    frame #0: 0x01e002e0 libswiftCore.dylib`function signature specialization <preserving fragile attribute, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> Swift.Never + 124
    frame #1: 0x01cbbd3c libswiftCore.dylib`Swift._ContiguousArrayBuffer._checkValidSubscript (Swift.Int) -> () + 164
    frame #2: 0x01cbbde0 libswiftCore.dylib`Swift._ArrayBuffer._checkInoutAndNativeTypeCheckedBounds (Swift.Int, wasNativeTypeChecked : Swift.Bool) -> () + 164
    frame #3: 0x01ccd430 libswiftCore.dylib`Swift.Array.subscript.getter : (Swift.Int) -> A + 112
    frame #4: 0x006a9d04 Charts`CombinedChartData.entryForHighlight(highlight=0x171832f0, self=0x15d04040) -> ChartDataEntry? + 200 at CombinedChartData.swift:278
  * frame #5: 0x0069b138 Charts`ChartViewBase.highlightValue(highlight=0x171832f0, callDelegate=true, self=0x15e35710) -> () + 652 at ChartViewBase.swift:547
    frame #6: 0x0069ac28 Charts`ChartViewBase.highlightValue(x=0, y=NaN, dataSetIndex=0, callDelegate=true, self=0x15e35710) -> () + 536 at ChartViewBase.swift:522
    frame #7: 0x0069a964 Charts`ChartViewBase.highlightValue(x=0, dataSetIndex=0, callDelegate=true, self=0x15e35710) -> () + 136 at ChartViewBase.swift:499
    frame #8: 0x0069a744 Charts`ChartViewBase.highlightValue(x=0, dataSetIndex=0, self=0x15e35710) -> () + 88 at ChartViewBase.swift:478
    frame #9: 0x0005f7b8 UrWtU8`CombinedChartView.setCombinedChart(xValues=Swift.Dictionary<UrWtU8.SegmentKey, Swift.String> @ 0x003f3158, yValues=0 key/value pairs, order=7 values, self=0x15e35710) -> () + 9808 at ViewExtensions.swift:379
    frame #10: 0x0004ac7c UrWtU8`FJViewController.setChart(self=0x16016200) -> () + 2320 at FJViewController.swift:143
    frame #11: 0x00048d68 UrWtU8`FJViewController.viewDidLoad(self=0x16016200) -> () + 1456 at FJViewController.swift:102
    frame #12: 0x00048ebc UrWtU8`@objc FJViewController.viewDidLoad() -> () + 56 at FJViewController.swift:0
    frame #13: 0x28072d8a UIKit`-[UIViewController loadViewIfRequired] + 1026
    frame #14: 0x28130682 UIKit`-[UINavigationController _layoutViewController:] + 38
    frame #15: 0x28130574 UIKit`-[UINavigationController _updateScrollViewFromViewController:toViewController:] + 376
    frame #16: 0x2812f928 UIKit`-[UINavigationController _startTransition:fromViewController:toViewController:] + 96
    frame #17: 0x2812f55e UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 834
    frame #18: 0x2812f1b4 UIKit`-[UINavigationController __viewWillLayoutSubviews] + 52
    frame #19: 0x2812f12a UIKit`-[UILayoutContainerView layoutSubviews] + 214
    frame #20: 0x2806ea72 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 714
    frame #21: 0x26106bcc QuartzCore`-[CALayer layoutSublayers] + 128
    frame #22: 0x26102374 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 348
    frame #23: 0x26102208 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
    frame #24: 0x261016d0 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 368
    frame #25: 0x261013a4 QuartzCore`CA::Transaction::commit() + 520
    frame #26: 0x260fab2a QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 138
    frame #27: 0x23ab96c8 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
    frame #28: 0x23ab79cc CoreFoundation`__CFRunLoopDoObservers + 280
    frame #29: 0x23ab7dfe CoreFoundation`__CFRunLoopRun + 958
    frame #30: 0x23a07228 CoreFoundation`CFRunLoopRunSpecific + 520
    frame #31: 0x23a07014 CoreFoundation`CFRunLoopRunInMode + 108
    frame #32: 0x24ff7ac8 GraphicsServices`GSEventRunModal + 160
    frame #33: 0x280db188 UIKit`UIApplicationMain + 144
    frame #34: 0x000fdac4 UrWtU8`main + 172 at AppDelegate.swift:33
    frame #35: 0x236af872 libdyld.dylib`start + 2

@liuxuan30
Copy link
Member

liuxuan30 commented Jan 18, 2017

It says you crashed at:

frame #4: 0x006a9d04 Charts`CombinedChartData.entryForHighlight(highlight=0x171832f0, self=0x15d04040) -> ChartDataEntry? + 200 at CombinedChartData.swift:278

which is:
let data = dataObjects[highlight.dataIndex]

can you check what's dataObjects count, and what highlight.dataIndex you want to get? What's the layout of your dataObjects? It's easy to debug from here on your side, so you know what's wrong. either you don't add the data correctly or something else, but I have no idea.

I am seeing highlightValue(x=0, y=NaN, dataSetIndex=0, you have y=NaN to highlight, I don't know if it's a valid point to highlight, or something wrong from there.

@Querschlag
Copy link

Same problem after upgrading from Swift 2 to Swift 3 - I try to highlight a value with chartView.highlightValue(x: Double(index), dataSetIndex: 0)

Up the callstack a Highlight objects get created with y=NaN

    /// Highlights any y-value at the given x-value in the given DataSet.
    /// Provide -1 as the dataSetIndex to undo all highlighting.
    /// - parameter x: The x-value to highlight
    /// - parameter dataSetIndex: The dataset index to search in
    /// - parameter callDelegate: Should the delegate be called for this change
    open func highlightValue(x: Double, dataSetIndex: Int, callDelegate: Bool)
    {
        highlightValue(x: x, y: Double.nan, dataSetIndex: dataSetIndex, callDelegate: callDelegate)
    }

...

public init(x: Double, y: Double, dataSetIndex: Int)
    {
        _x = x
        _y = y
        _dataSetIndex = dataSetIndex
    }

dataIndex never gets set and stays at default value of Int(-1)
Finally it crashes in CombinedCartData.swift:278 when accessing dataObjects with index -1. dataObjects had a count of 1 in my case.

Am I using this wrong? It worked with the Charts 2.x ...

@liuxuan30
Copy link
Member

don't hijack other's thread when your issue is not the same. y = NaN do not tell it's the same
Chart 3.0 works different in terms of x axis. You should check

    /// the index of the data object - in case it refers to more than one
    open var dataIndex = Int(-1)

why your dataIndex is -1.

@prajeshpeter
Copy link

prajeshpeter commented Apr 21, 2017

Issue in CombinedChartView.

When i use [self.combinedGraphView highlightValueWithX:1 y:0 dataSetIndex:0]; to set a bar programmatically highlighted it is crashing..

Please help on this ...

combined-chart-crash-on-highlight

@liuxuan30
Copy link
Member

@prajeshpeter double check your highlight object dataIndex. I think you are not using the right API, as combined chart data consists of sub chart data, like bar data, line data. If you don't give a valid dataIndex, it will be -1 and crash.

@prajeshpeter
Copy link

prajeshpeter commented Apr 24, 2017

@liuxuan30, could you please advice me the correct API and how to use it with Few lines of code as sample. It is very much important for me to get fixed this issue.

self.combinedGraphView.drawOrder = @[
                                    @(CombinedChartDrawOrderBar),
                                    @(CombinedChartDrawOrderLine)
                                    ];
barChartDataSet.highlightEnabled =YES; 
lineChartDataSet.highlightEnabled =NO; 
//[self.combinedGraphView highlightValueWithX:1.0 y:0.0 dataSetIndex:0];
[self.combinedGraphView highlightValueWithX:1.0 dataSetIndex:0]

Both are crashing, I have tried more APIs. I can only specify the dataSetIndex .

I am using charts(3.0.1)version

Any help will be much appreciated.

@liuxuan30
Copy link
Member

liuxuan30 commented Apr 25, 2017

It's not dataSetIndex crashing, it's dataIndex
I suppose you can't use [self.combinedGraphView highlightValueWithX:1.0 dataSetIndex:0]. Use others like

open func highlightValue(_ highlight: Highlight?, callDelegate: Bool)

you have to create a highlight object manually, and set dataIndex to the correct one, e.g. 0 for bar or 1 for line data.

Other convenient methods are just wrappers if I remember correctly, they don't touch dataIndex by default.

I am closing this since it's old and I think we have the answer.

@prajeshpeter
Copy link

Thank you very much, I got it...

@fangzhengwang
Copy link

Did you solve this problem? @prajeshpeter ,I still have this problem,If you have solved it, please give me some advice,Thank you very much

@prajeshpeter
Copy link

ChartHighlight *chartHighlight = [[ChartHighlight alloc] initWithX:1.0 y:800.0 xPx:0.0 yPx:0.0 dataIndex:1 dataSetIndex:0 stackIndex:0 axis:0];

[self.barGraphView highlightValue:chartHighlight callDelegate:YES];

Use the above code to solve the issue ... use y value to solve another unexpected crash...

Hope this will be helpful.. .. please let me know if you still face the issue...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants