How to add marker to center position in ios charts while scrolling horizontally with increasing scaleX and enabling drag
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:
Set an horizontal scroll to my Barchart in swift
the scroll will only happen if you zoomed in. By default, all data are plotted on the canvas, so nothing to scroll.
You can try increase scaleX
by code, to see if your code works.
How to highlight bar in Bar Chart from custom method using ios charts?
The iOS-Charts
api is identical to the MPAndroidChart
android library. So you can refer to MPAndroidChart
's docs. From this page, you can see that there are a couple methods available to make a selection programmatically :
Highlighting programmatically
highlightValues(Highlight[] highs)
: Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting.
highlightValue(int xIndex, int dataSetIndex)
: Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting.
getHighlighted()
: Returns an Highlight[] array that contains information about all highlighted entries, their x-index and dataset-index.
so on the sliderValueChanged event you could call something like this :
[_chartView highlightValueWithXIndex:2 dataSetIndex:0 callDelegate:NO];
Related Topics
Change Tabview Indicator Swiftui
Show Folder's Contents in Finder Using Swift
How to Convert Delegate to Observable Rxswift
Swift 4 - Notification Center Addobserver Issue
App Crash on Sign in (Xcode 9.3) Exc_Bad_Access (Code=1, Address=0X1)
What Is Other Option Available in Swift Instead of Refactoring and Renaming Class or Attribute Name
Check for Value or Reference Type in Swift
Self.Image.Frame.Width = 20 Give Get Only Property Error
Property Observers for Uiview Bounds and Frame React Differently
Count Number of Decimal Places in a Float (Or Decimal) in Swift
Swiftui Navigation on iPad - How to Show Master List
Is It Possible in Swift to Add Variables to an Object at Runtime
Swift Error: Missing Argument Label 'Name:' in Call
Swift: Extract Float from Byte Data
Firebase Sign Out Not Working in Swift
Resize a Sklabelnode Font Size to Fit
How Does Typecasting/Polymorphism Work with This Nested, Closure Type in Swift