swift - 如何在增加 scaleX 水平滚动并启用拖动的同时将标记添加到 ios 图表的中心位置

标签 swift ios-charts swiftcharts

我正在使用 Daniel Cohen Gindi 的图表框架。我通过增加 scaleX 值并启用拖动来水平滚动图表,如这个问题的答案中所述

Set an horizontal scroll to my Barchart in swift

我想在用户水平滚动时在中心位置添加标记。我知道每当用户拖动图表时都会调用“chartTranslated”委托(delegate),但它不像“chartValueSelected”委托(delegate)方法中那样包含“ChartDataEntry”和“Highlighted”的值。

最佳答案

不幸的是,ChartViewBase.swift 充满了私有(private)函数和内部变量,您无法扩展某些方法或获取某些变量来获取您搜索的值。

无论如何,您始终可以改进源代码,在 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?)

然后,您可以轻松地将这个新的委托(delegate)方法调用到调用 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) 
     }

用法:

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)")
        }
    }
}

现在,使用这个新的委托(delegate)方法,您可以将标记指向中心索引。

展示一个小演示的 gif:

enter image description here

关于swift - 如何在增加 scaleX 水平滚动并启用拖动的同时将标记添加到 ios 图表的中心位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430461/

相关文章:

ios - 如何给UIButton添加属性?

ios - 如何设置自定义 X 轴标签间隔?

swift - iOS 图表的 X 轴标签

ios - 如何在 iOS 图表的左 Axis 上添加字符串?

ios - 当用户滚动条形图时,我可以调用 SwiftCharts 中的委托(delegate)吗?

swift - 使用具有未设置或设置为 NSKeyedUnarchiveFromDataTransformerName 的转换器名称的可转换属性

ios - 在 MessageKit 中点击时放大 ImageView - Swift

ios - 从 ScrollView 连续滚动到嵌套 Collection View

swift - 将图例文本添加到饼图 Swift Charts

swift - 使用 SwiftCharts 创建图表