3

I am using the Charts framework from Daniel Cohen Gindi. I am scrolling chart horizontally by increasing scaleX value and enabled drag as described in answer of this question

Set an horizontal scroll to my Barchart in swift

I want to add marker at center position when user scrolls horizontally. I know "chartTranslated" delegate is called whenever user drags the chart but it doesn't contain the value of "ChartDataEntry" and "Highlighted" like in "chartValueSelected" delegate method.

Hannan Riaz
  • 33
  • 1
  • 3

1 Answers1

9

Unfortunately ChartViewBase.swift is full of private function and internal vars and you can't extend some method or obtain some var to get the values you searching for.

Anyway, you can always improve sources adding some other method you want to use inside the public protocol ChartViewDelegate:

ChartViewBase.swift

under the line:

@objc optional func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat)

add this:

@objc optional func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat, entry: ChartDataEntry?, highlight: Highlight?, centerIndices:Highlight?)

Then , you can easily call this new delegate method to the only source part of the code where chartTranslated is called:

BarLineChartViewBase.swift

search the part of the code where you see these lines:

if delegate !== nil
        {
            delegate?.chartTranslated?(self, dX: translation.x, dY: translation.y)
        }

and change it with:

if delegate !== nil
        {
            delegate?.chartTranslated?(self, dX: translation.x, dY: translation.y)
            var entry: ChartDataEntry?
            var h = self.lastHighlighted

            if h == nil
            {
                _indicesToHighlight.removeAll(keepingCapacity: false)
            }
            else
            {
                // set the indices to highlight
                entry = _data?.entryForHighlight(h!)
                if entry == nil
                {
                    h = nil
                    _indicesToHighlight.removeAll(keepingCapacity: false)
                }
                else
                {
                    _indicesToHighlight = [h!]
                }
            }
            let centerH = getHighlightByTouchPoint(self.center)
            if centerH === nil || centerH!.isEqual(self.lastHighlighted)
            {
                //self.highlightValue(nil, callDelegate: true)
                //self.lastHighlighted = nil
            }
            else
            {
                print("\n in center we have: \(centerH!)")
                self.highlightValue(centerH, callDelegate: true)
                self.lastHighlighted = centerH
                // please comment these lines if you don't want to automatic highlight the center indices.. 
            }
            delegate?.chartTranslated?(self, dX: translation.x, dY: translation.y, entry: entry, highlight: h, centerIndices:centerH) 
     }

Usage:

import Foundation
import UIKit
import Charts

class test: UIViewController, ChartViewDelegate {

    func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat, entry: ChartDataEntry?, highlight: Highlight?, centerIndices:Highlight?) {
        if let entry = entry, let highlight = highlight {
            print("chartTranslated info:\n\(self)\ndX and dY:\(dX)-\(dY)\nentry:\(entry)\nhightlight:\(highlight)")
        if let centerIndices = centerIndices {
            print("\n center indices is:\n\(centerIndices)")
        }
    }
}

Now, with this new delegate method you can make your marker to the center indices.

A gif to show a little demonstration:

enter image description here

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • Thanks for the answer but self.lastHighlighted will give last highlighted position. How do i create highlighted position at center of visible screen during scroll ? – abh Aug 07 '17 at 05:47
  • 1
    If I've understand you want also to know during the scrolling/zooming the current center indices, I've update my answer to obtain also this indices, so you can add to him a marker if you want. In my code I've added also the possibility to have the center indices auto-highlighted, if you don't want it comment the relative lines – Alessandro Ornano Aug 07 '17 at 19:00
  • It worked with a little modification in your code. Thanks a lot Alessandro – abh Aug 08 '17 at 05:47
  • I also need slightly different functionality for my charts. While scrolling, it should highlight bars but with above approach my bar highlights when coming at the center only. Could you please suggest? – Sharma Sep 18 '18 at 04:50
  • Can You give me code for how you used chart view object, i mean initialization of Chart view ? – puja Mar 20 '19 at 10:09