很遗憾ChartViewBase.swift
充满了私有函数和内部变量,您无法扩展某些方法或获取某些变量来获取您搜索的值。
无论如何,您始终可以改进源代码,添加您想要在内部使用的其他方法public protocol ChartViewDelegate
:
ChartViewBase.swift
下线:
@objc optional func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat)
添加这个:
@objc optional func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat, entry: ChartDataEntry?, highlight: Highlight?, centerIndices:Highlight?)
然后,您可以轻松地对代码的唯一源部分调用这个新的委托方法。chartTranslated
叫做:
BarLineChartViewBase.swift
搜索您看到这些行的代码部分:
if delegate !== nil
{
delegate?.chartTranslated?(self, dX: translation.x, dY: translation.y)
}
并将其更改为:
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)")
}
}
}
现在,使用这个新的委托方法,您可以将标记标记到中心索引。
一个 gif 来展示一下: