在Swift中更新Highcharts系列实时数据



我在Swift/iOS中尝试用高图绘制实时数据时遇到了困难。这就是目前的情况:

我为从内部麦克风接收到的每个帧附加一个值到一个数组。数组有一个固定的大小(它像FIFO一样工作(。

现在我想要一个Highcharts线Plot来显示我的Array内的所有点,每次收到新的音频帧时都会更新。我认为.redraw函数应该这样做。

这就是代码目前的样子:Audioprocessing类内的折线图设置:

let options = HIOptions()
public var chartView = HIChartView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
let chart = HIChart()
let series = HISeries()
public func initChartView(view: UIView){
// initialize Highcharts object
chartView = HIChartView(frame: CGRect(x: view.bounds.origin.x, y: view.bounds.origin.y, width: view.bounds.size.width, height: (view.bounds.size.height - 50)))

chart.type = "spline"
options.chart = chart

let title = HITitle()
title.text = "Tonhoehe"
options.title = title

let xAxis = HIXAxis()
xAxis.tickInterval = 1
xAxis.type = "linear"
xAxis.title = HITitle()
xAxis.title.text = "Zeit [s]"
options.xAxis = [xAxis]

let yAxis = HIYAxis()
yAxis.type = "logarithmic"
yAxis.minorTickInterval = 0.1
yAxis.title = HITitle()
yAxis.title.text = "Frequenz [Hz]"
options.yAxis = [yAxis]

series.data = [1,2,3,4,5,4,3,2,1]
series.showInLegend = false
options.series = [series]

chartView.options = options

}

在函数processData()中,我更新数组(称为data(,但无法更新Highcharts图:

...
// updating the array
data.append(maxFrequency ?? 0)
if(data.count >= 500){
data.removeFirst()
}
let newSeries = HISeries()
newSeries.data = data
self.chartView.options.series = [newSeries]// this was recommended on github but does not work at all

这将成功编译,但当我运行程序时,它会崩溃,并出现以下错误:

=================================================================
Main Thread Checker: UI API called on a background thread: -[WKWebView evaluateJavaScript:completionHandler:]
PID: 18985, TID: 1972086, Thread name: (none), Queue name: captureQueue, QoS: 25
Backtrace:
4   Highcharts                          0x0000000102859258 -[HIChartView updateOptions] + 344
5   Highcharts                          0x0000000102859830 -[HIChartView observeValueForKeyPath:ofObject:change:context:] + 148
6   Foundation                          0x00000001a47d9320 A0089247-6F38-3097-B144-F5E2C90ADD3A + 373536
7   Foundation                          0x00000001a487b0a4 A0089247-6F38-3097-B144-F5E2C90ADD3A + 1036452
8   Foundation                          0x00000001a487d4d8 A0089247-6F38-3097-B144-F5E2C90ADD3A + 1045720
9   Foundation                          0x00000001a487a8d0 A0089247-6F38-3097-B144-F5E2C90ADD3A + 1034448
10  Highcharts                          0x000000010282e674 -[HIChartsJSONSerializable updateArrayObject:newValue:propertyName:] + 452
11  Highcharts                          0x00000001027eae60 -[HIOptions setSeries:] + 108

我还尝试了.addPoint(HIDataPoint)chartView.options.series[0].data = data,它们也不起作用。

TL;博士:如何在Swift中访问与javascript中的.addPoint()方法相同的功能,将数据动态添加到Highcharts图表中?

中提出的方法https://github.com/highcharts/highcharts-ios/issues/116对我不起作用。

无法在sessionQueue.async{}线程内使用chartView.addpoint(y)方法。这是我从音频界面更新inputData的地方,也是我尝试更新图表的地方。

相反,我在Viewcontroller中创建了一个Timer,它周期性地调用chartView.addPoint(y)方法。这很好用!获取音频数据和更新图表现在是单独完成的。

最新更新