2016-09-01 2 views
0

ios-charts (LineChart)와지도간에 사용자 상호 작용을 추가하고 싶습니다. 사용자가 차트 위로 슬라이드하면 현재 차트 위치의 인덱스 또는 값을 가져와 함수에서 사용해야합니다.ios-charts와의 사용자 상호 작용

class MotoChart: UIView { 

    @IBOutlet weak var lineChartView: LineChartView! 

    // 1 - creating an array of data entries 
    var yVals1 : [ChartDataEntry] = [ChartDataEntry]() 
    let months = ["Jan" , "Feb", "Mar", "Apr", "May", "June", "July", "August", "Sept", "Oct", "Nov", "Dec"] 
    let dollars1 = [1453.0,2352,5431,1442,5451,6486,1173,5678,9234,1345,9411,2212] 


    //MARK: overrides 
    override func prepareForInterfaceBuilder() { 
     super.prepareForInterfaceBuilder() 
    } 

    override func awakeFromNib() { 
    } 


    func setupChart(routeMaster: RouteMaster){ 

     self.lineChartView.descriptionText = "Speed" 
     self.lineChartView.descriptionTextColor = UIColor.blackColor() 
     self.lineChartView.gridBackgroundColor = UIColor.darkGrayColor() 
     self.lineChartView.noDataText = "No data provided" 
     self.lineChartView.rightAxis.enabled = false 
     self.lineChartView.drawGridBackgroundEnabled = false 
     self.lineChartView.doubleTapToZoomEnabled = false 
     self.lineChartView.xAxis.drawGridLinesEnabled = false 
     self.lineChartView.xAxis.drawAxisLineEnabled = false 
     self.lineChartView.rightAxis.drawGridLinesEnabled = false 
     self.lineChartView.rightAxis.drawAxisLineEnabled = false 
     self.lineChartView.leftAxis.drawGridLinesEnabled = false 
     self.lineChartView.leftAxis.drawAxisLineEnabled = false 
     self.lineChartView.animate(yAxisDuration: 1.0) 

     setChartData(months, yValsArr: dollars1) 
    } 


    func setChartData(xValsArr: [String], yValsArr: [Double]) { 

     for (index, item) in xValsArr.enumerate() { 
      yVals1.append(ChartDataEntry(value: yValsArr[index], xIndex: index)) 
     } 


     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.drawCirclesEnabled = false 
     set1.lineWidth = 0.5 
     set1.circleRadius = 6.0 // the radius of the node circle 
     set1.fillAlpha = 65/255.0 
     set1.fillColor = UIColor.redColor() 
     set1.drawFilledEnabled = true 
     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: xValsArr, dataSets: dataSets) 
     data.setValueTextColor(UIColor.whiteColor()) 

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

} 

답변

0

대답 here

  1. 자기 self.lineChartView.delegate에 위임 VAR을 설정 차트 위임 프로토콜 ChartViewDelegate
  2. 추가 발견 ... 데모에서 아무것도 발견하지 않았습니다 = 자기
  3. 는 위임 프로토콜 chartValueSelected
에서 옵션 기능 추가

.

class MotoChart: UIView, ChartViewDelegate { 

@IBOutlet weak var lineChartView: LineChartView! 

// 1 - creating an array of data entries 
var yVals1 : [ChartDataEntry] = [ChartDataEntry]() 
let months = ["Jan" , "Feb", "Mar", "Apr", "May", "June", "July", "August", "Sept", "Oct", "Nov", "Dec"] 
let dollars1 = [1453.0,2352,5431,1442,5451,6486,1173,5678,9234,1345,9411,2212] 


//MARK: overrides 
override func prepareForInterfaceBuilder() { 
    super.prepareForInterfaceBuilder() 
} 

override func awakeFromNib() { 
} 


func setupChart(routeMaster: RouteMaster){ 

    self.lineChartView.delegate = self 
    self.lineChartView.descriptionText = "Speed" 
    self.lineChartView.descriptionTextColor = UIColor.blackColor() 
    self.lineChartView.gridBackgroundColor = UIColor.darkGrayColor() 
    self.lineChartView.noDataText = "No data provided" 
    self.lineChartView.rightAxis.enabled = false 
    self.lineChartView.drawGridBackgroundEnabled = false 
    self.lineChartView.doubleTapToZoomEnabled = false 
    self.lineChartView.xAxis.drawGridLinesEnabled = false 
    self.lineChartView.xAxis.drawAxisLineEnabled = false 
    self.lineChartView.rightAxis.drawGridLinesEnabled = false 
    self.lineChartView.rightAxis.drawAxisLineEnabled = false 
    self.lineChartView.leftAxis.drawGridLinesEnabled = false 
    self.lineChartView.leftAxis.drawAxisLineEnabled = false 
    self.lineChartView.animate(yAxisDuration: 1.0) 

    setChartData(months, yValsArr: dollars1) 
} 


func setChartData(xValsArr: [String], yValsArr: [Double]) { 

    for (index, item) in xValsArr.enumerate() { 
     yVals1.append(ChartDataEntry(value: yValsArr[index], xIndex: index)) 
    } 


    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.drawCirclesEnabled = false 
    set1.lineWidth = 0.5 
    set1.circleRadius = 6.0 // the radius of the node circle 
    set1.fillAlpha = 65/255.0 
    set1.fillColor = UIColor.redColor() 
    set1.drawFilledEnabled = true 
    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: xValsArr, dataSets: dataSets) 
    data.setValueTextColor(UIColor.whiteColor()) 

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

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) { 
    print(" Chart Value \(entry.value) \(entry.xIndex)") 
}} 

}