我知道在MPAndroidChart
上可以处理图表上的事件。在其documentation上都有详细记录。
然而,我无法在ios-chart
找到有关相同事件的任何文档。我知道它的创建者告诉我们我们应该跟踪MPAndroidChart
的文档库,但是我无法在Swift 3.0上处理这些事件。
我也找不到任何示例处理ios-chart
库的这些事件。
那么,是否可以在ios-chart
库上处理点击事件?
编辑:根据@AdamM
的反馈,我将在此处设置我将数据设置到图表的功能。
func enterData(valuesChart: [BarChartDataEntry]){
let chartDataSet = BarChartDataSet(values: valuesChart, label: "Total Values")
let charData = BarChartData(dataSets: [chartDataSet])
barChartView?.data = charData
barChartView?.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}
提前致谢!
答案 0 :(得分:6)
是的,此功能内置于图表库中。确保您的类符合ChartViewDelegate协议。
修改完整代码示例
import UIKit
import Charts
@objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter
{
var months: [String]! = ["Jan", "Feb", "Mar"]
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return months[Int(value)]
}
}
class ViewController: UIViewController,ChartViewDelegate
{
@IBOutlet var barChart : BarChartView!
let dataArray = ["Jan", "Feb", "Mar"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = "Testing"
let values = [5.2,10.5,15.3]
setBarChart(values: values)
barChart.delegate = self
}
//MARK: Chart delegate methods
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
let pos = NSInteger(entry.x)
print(dataArray[pos])
}
func chartValueNothingSelected(_ chartView: ChartViewBase)
{
}
//MARK: Set chart
func setBarChart(values: [Double])
{
var dataEntries: [BarChartDataEntry] = []
for i in 0..<values.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let dataSet = BarChartDataSet(values: dataEntries, label: "")
var dataSets : [IChartDataSet] = [IChartDataSet]()
dataSets.append(dataSet)
let format:BarChartFormatter = BarChartFormatter()
let xAxis = barChart.xAxis
xAxis.granularity = 1.0
xAxis.labelPosition = .bottom
xAxis.valueFormatter = format
let chartData = BarChartData(dataSets: dataSets)
barChart?.data = chartData
}
}
答案 1 :(得分:1)
对于IOS图表3.省略dataSetIndex!
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight)
{...}