我正在使用ios-charts在我的视图控制器中绘制图表。我在视图中有 3 个图表,我想根据第一个图表的值选择显示第二个图表,并且我想按顺序显示基于第二个图表的值选择的第三个图表。
由于图表值选择方法,基于第一个图表的值选择显示第二个图表很容易,即
func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
}
我无法对第二到第三张图表使用相同的方法。我需要帮助来解决这个问题。我正在尝试显示从第一个图表中选择值的第二个图表。
// A delegate method from data model
func dataDidLoad(controller: DataModel) {
var chartArray:[PFObject] = [PFObject]()
let formatter = NSDateFormatter()
formatter.dateFormat = "MMM"
dataDict = [:]
for element in chartArray {
let data = od["Data1"] as! String
if self.dataDict.indexForKey(data) != nil {
self.dataDict[value]! += 1.0
}else{
self.dateDict.updateValue(1.0, forKey: data1.desc)
}
}
let dataPointArray = Array(dataDict.keys)
let valuesArray = Array(dataDict.values)
pieChartView.data = nil
setChart1(dataPointArray, values: valuesArray)
}
// This method sets the first chart and working fine
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
var label = ""
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
label += "\(dataPoints[i]) "
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: label)
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartView.data = pieChartData
pieChartView.animate(xAxisDuration: NSTimeInterval(5))
var colors: [UIColor] = []
for _ in 0..<dataPoints.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
// This menthod is the delegate method from ios chart liberary. And gets triggered when any data selected from chart1
func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
data2Dict = [:]
for element in chart2Array {
let data2 = od["Data2"] as! String
if self.data2Dict.indexForKey(data2) != nil {
self.data2Dict[data2]! += 1.0
}else{
self.data2Dict.updateValue(1.0, forKey: data2)
}
}
let valuesArray = Array(data2Dict.values)
let dPointArray = Array(data2Dict.keys)
setChart2(dPointArray, values: valuesArray)
}
// This method sets the 2nd chart and working fine
func setChart2(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
var label = ""
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
label += "\(dataPoints[i]) "
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: label)
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartView.data = pieChartData
pieChartView.animate(xAxisDuration: NSTimeInterval(5))
var colors: [UIColor] = []
for _ in 0..<dataPoints.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
由于数据是动态的,并且第二张和第三张图表的变化是基于从上一张图表中选择的数据,我不能使用相同的事件同时触发第二张和第三张图表。