danielgindi / iOSCharts chartValueSelected未调用

时间:2016-10-15 00:52:22

标签: ios delegates ios-charts

我正在使用danielgindi / iOSCharts库来组合下面的组合线条和条形图。

enter image description here

enter image description here

我的图表已正确呈现,并且具有点按和缩放缩放等用户互动功能。图表也能够检测到我的轻击手势。但不知何故

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");
}

}

2 个答案:

答案 0 :(得分:2)

在这里很晚,但是方法签名在Swift 3.0中有所改变(参见this answer),不再包含dataSetIndex。

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

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

答案 1 :(得分:0)

现在是......     func chartValueSelected(_ chartView:ChartViewBase,entry:ChartDataEntry,highlight:Highlight){       print(&#34; Punto:(entry.x)Valore:(entry.y)&#34;)     }