gpt4 book ai didi

ios - swift:重新加载 View 以在图表中显示新数据

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

我正在使用图表框架“图表”,但是它没有重新加载带有新数据的图表的功能。它只能在加载 viewController 时显示一次图表。但我想使用表格中的选择重新加载 View /图表。 (表格在其他viewcontroller中)

在我的例子中,我有一个内部 View 的 ViewController和数据在两个数组中:x 和 y

func setChartData(x: [Double], y: [Double])

我试图调用该函数,但它没有显示。如果从 viewWillAppear() 调用该函数图表仅显示,如果我切换到另一个 View Controller 并切换回来

我该如何解决这个问题

函数内的 self.view!.setNeedsDisplay() 不起作用。

我的 View Controller :

override func viewWillAppear(animated: Bool) {
setChartData(resultID_statistics, y: resultWeight_statistics)

}

func setChartData(x: [Double], y: [Double]) {

// 1 - creating an array of data entries
var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for var i = 0; i < x.count; i++ {
yVals1.append(ChartDataEntry(value: y[i], xIndex: i))
}

// 2 - create a data set with our array
let set1: LineChartDataSet = LineChartDataSet(yVals: yVals1, label: "First Set")
set1.axisDependency = .Left // Line will correlate with left axis values
set1.setColor(UIColor.redColor().colorWithAlphaComponent(0.5)) // our line's opacity is 50%
set1.setCircleColor(UIColor.redColor()) // our circle will be dark red
set1.lineWidth = 2.0
set1.circleRadius = 6.0 // the radius of the node circle
set1.fillAlpha = 65 / 255.0
set1.fillColor = UIColor.redColor()
set1.highlightColor = UIColor.whiteColor()
set1.drawCircleHoleEnabled = true

//3 - create an array to store our LineChartDataSets
var dataSets : [LineChartDataSet] = [LineChartDataSet]()
dataSets.append(set1)

//4 - pass our months in for our x-axis label value along with our dataSets
let data: LineChartData = LineChartData(xVals: x, dataSets: dataSets)
data.setValueTextColor(UIColor.whiteColor())

//5 - finally set our data
self.lineChartView.data = data

data.notifyDataChanged()
lineChartView.notifyDataSetChanged()

}

最佳答案

从他们的文档看来,您需要通知数据集的更改:

 // EXAMPLE 1
// add entries to the "data" object
exampleData.addEntry(...);
chart.notifyDataSetChanged(); // let the chart know it's data changed
chart.invalidate(); // refresh

// EXAMPLE 2
// add entries to "dataSet" object
dataSet.addEntry(...);
exampleData.notifyDataChanged(); // let the data know a dataSet changed
chart.notifyDataSetChanged(); // let the chart know it's data changed
chart.invalidate(); // refresh

有关更多信息,请查看他们的 wiki .


注意:来自 repo README:

Currently there's no need for documentation for the iOS/tvOS/OSX version, as the API is 95% the same as on Android. You can read the official MPAndroidChart documentation here: Wiki

所以当您单击链接并看到“MPAndroidChart”时不要 panic :)


你也可以试试:

chart.data.notifyDataChanged()
chart.notifyDataSetChanged()

这取自他们在 objective-c 中的示例项目:

  LineChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
set1.values = values;
[_chartView.data notifyDataChanged];
[_chartView notifyDataSetChanged];
}

关于ios - swift:重新加载 View 以在图表中显示新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39227530/

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