danielgindi/iOSCharts 未调用 chartValueSelected

danielgindi/iOSCharts chartValueSelected not called

我正在使用 danielgindi/iOSCharts 库来创建如下所示的组合折线图和条形图。

我的图表呈现正确,并且具有点击和捏合缩放等用户交互。图表还能够检测到我的点击手势。但不知何故

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight){
        print("###################")
    }

无法调用。这里真的需要一些帮助。

我想要实现的是,如果我单击线上的条形图或点,则两个具有相同 X 值的气球​​标记都会显示。 (当我点击一个条或一个点时,上面两张图片中的标记将同时显示)

下面是我的整个控制器代码:

class FirstViewController: UIViewController,ChartViewDelegate {
@objc(ChartFormatter)
public class ChartFormatter: NSObject, IAxisValueFormatter{

    var months: [String]! = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]


    public func stringForValue(_ value: Double, axis: AxisBase?) -> String {

        return months[Int(value)]
    }
}

@IBOutlet weak var chartView: CombinedChartView!
var lineChartData = LineChartData()
var barChartData = BarChartData()

override func viewDidLoad() {
    super.viewDidLoad()

    chartView.delegate = self

    chartView.chartDescription?.enabled = true

    chartView.dragEnabled = true
    chartView.setScaleEnabled(true)
    chartView.pinchZoomEnabled = true
    chartView.drawGridBackgroundEnabled = false

    let xAxis:XAxis = chartView.xAxis
    xAxis.labelPosition = .bottom

    xAxis.labelFont = UIFont.systemFont(ofSize: 11.0)
    xAxis.labelTextColor = UIColor.black
    xAxis.drawGridLinesEnabled = true
    xAxis.drawAxisLineEnabled = false
    xAxis.drawLabelsEnabled = false

    let leftAxis:YAxis = chartView.leftAxis;
    leftAxis.labelTextColor = UIColor(red: 1, green: 165/255, blue: 0, alpha: 1)
    leftAxis.axisMaximum = 5.0
    leftAxis.axisMinimum = -5.0
    leftAxis.drawGridLinesEnabled = false
    leftAxis.drawZeroLineEnabled = true
    leftAxis.granularityEnabled = true
    leftAxis.drawLabelsEnabled = false

    let rightAxis:YAxis = chartView.rightAxis
    rightAxis.enabled = false

    let credit: [Double] = [1.0, 4.0, 2.0, 3.0, 3.0, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0]
    let gpa: [Double] = [-1.0, -4.0, -1.5, -1.4, -1.3, -1.0, -3.0, 0.0, 0.0, 0.0, 0.0]
    let chartDescription = ""


    let marker = BalloonMarker(color: UIColor(white: 180/255, alpha: 1.0), font: UIFont.systemFont(ofSize:12.0), textColor: UIColor.white, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))

    marker.chartView = chartView
    marker.minimumSize = CGSize(width:80.0, height:40.0)

    chartView.marker = marker

    self.setLineChart(dataPoints: gpa, chartDescription: chartDescription)
    self.setBarChart(dataPoints: credit)

    self.setChartData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func setLineChart(dataPoints: [Double], chartDescription: String) {

    let formatter:ChartFormatter = ChartFormatter()
    let xaxis:XAxis = XAxis()

    var dataEntries: [ChartDataEntry] = Array()

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(x: Double(i), y: dataPoints[i])
        dataEntries.append(dataEntry)
        formatter.stringForValue(Double(i), axis: xaxis)
    }

    let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Credit")
    self.lineChartData = LineChartData(dataSet: lineChartDataSet)

    xaxis.valueFormatter = formatter
    chartView.xAxis.valueFormatter = xaxis.valueFormatter

    xaxis.drawGridLinesEnabled = false

    lineChartDataSet.mode = .linear
    lineChartDataSet.drawCirclesEnabled = false
    lineChartDataSet.drawValuesEnabled = false

    lineChartDataSet.colors = [UIColor.brown]

    chartView.descriptionText = chartDescription


}

func setBarChart(dataPoints: [Double]) {
    let formatter:ChartFormatter = ChartFormatter()
    let xaxis:XAxis = XAxis()

    var dataEntries: [BarChartDataEntry] = Array()

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y: dataPoints[i])
        dataEntries.append(dataEntry)
        formatter.stringForValue(Double(i), axis: xaxis)
    }
    let barChartDataSet = BarChartDataSet(values: dataEntries, label: "GPA")
    self.barChartData = BarChartData(dataSet: barChartDataSet)


    xaxis.valueFormatter = formatter
    chartView.xAxis.valueFormatter = xaxis.valueFormatter

    xaxis.drawGridLinesEnabled = false

    barChartDataSet.drawValuesEnabled = false
}

func setChartData() {
    let data:CombinedChartData = CombinedChartData()

    data.lineData = self.lineChartData
    data.barData = self.barChartData
    chartView.data = data
    chartView.data?.highlightEnabled = true
    //chartView.autoScaleMinMaxEnabled = true
    //chartView.setNeedsDisplay()
}

@objc func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight){
    print("###################")
}

@objc func chartValueNothingSelected(chartView: ChartViewBase){
    NSLog("chartValueNothingSelected");
}

}

这里很晚才加入,但是方法签名在 Swift 3.0 中发生了变化(参见 ),不再包括 dataSetIndex。

通过将函数签名(和实现)更改为以下内容,您应该能够在控制台中看到此触发:

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight){
    print("###################")
}

现在

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight){
  print("Point: \(entry.x) Value: \(entry.y)")
}