iOS 中 Highchart 动态数据实现

问题描述 投票:0回答:1

我目前正在 iOS Swift 中使用 Highcharts 10.3.3,并已使用

initWithJSFunction:(NSString *) jsFunction
方法成功实现了向下钻取功能。然而,我面临着性能方面的挑战,因为在初始时间发送所有事件系列需要相当长的时间,特别是对于具有多个嵌套钻取的图表。

我正在寻找一种解决方案来在 iOS Swift 上实现类似于 JavaScript 的功能。在 JavaScript 中,我们可以接收第一级钻取图表单击事件,然后动态附加下一组数据。我很好奇在 iOS 上使用 Swift 是否可以实现这样的无缝实现。

任何关于在 Swift 中实现这一目标的见解或指导将不胜感激。

ios swift highcharts drilldown
1个回答
0
投票

在 Swift 中,您可以通过处理图表点击事件并动态更新图表数据来实现类似的动态钻取功能。 Highcharts 提供了一种在 Swift 中处理图表事件的机制,允许您响应用户交互,例如钻取。

以下是如何使用 Highcharts 在 Swift 中实现动态钻取功能的简化示例:

import UIKit
import Highcharts

class ViewController: UIViewController, HIChartViewDelegate {

    @IBOutlet weak var chartView: HIChartView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set up initial chart configuration
        let options = HIOptions()

        // Set up your initial series data
        let initialSeries = HISeries(name: "First Level", data: [
            ["Category 1", 10],
            ["Category 2", 20],
            // Add more initial data as needed
        ])
        options.series = [initialSeries]

        // Set up the chart view
        chartView.options = options
        chartView.delegate = self
    }

    // Implement the chart view delegate method to handle drilldown events
    func chartView(_ chartView: HIChartView, drilldownEvent event: HIDrilldownEvent) {
        // Check the drilldown level or category clicked
        if event.category == "Category 1" {
            // Load the next set of data for the drilldown
            let nextLevelData = [
                ["Subcategory 1", 5],
                ["Subcategory 2", 15],
                // Add more data for the next level as needed
            ]

            // Create a new series for the drilldown level
            let nextLevelSeries = HISeries(name: "Subcategory", data: nextLevelData)

            // Update the options with the new series
            var options = chartView.options
            options.series = [nextLevelSeries]
            chartView.options = options

            // Redraw the chart to reflect the changes
            chartView.redraw()
        }
    }
}

在此示例中,实现了

chartView(_:drilldownEvent:)
方法来处理向下钻取事件。它检查单击的类别,并根据该类别加载下一组数据以进行向下钻取。然后,它为下一个钻取级别创建一个新系列,并相应地更新图表选项。

这是一个基本示例,您可能需要根据您的具体图表结构和数据处理要求进行调整。确保根据需要处理每个级别的深入事件,动态加载适当的数据。

© www.soinside.com 2019 - 2024. All rights reserved.