gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:00:10 31 4
gpt4 key购买 nike

我正在使用 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com