How can I get `highlight.xPx` & `highlight.yPx` of the other lines which have same x point in iOS-Chart?

大城市里の小女人 提交于 2020-04-30 09:16:33

问题


I was able to achieve the plotting of lines on the graph using iOS-Charts. The x points are common for all the lines but the y points are different.

When I move the cursor around the graph I want all the other circles also to move the with the single drag.

So when I select any one of the x points amongst any 3 lines I want the highlight.xPx & highlight.yPx of other 2 lines as well.

Here is an example

let dysLineEntries: [ChartDataEntry] = [ChartDataEntry(x: 20.0, y: 120.0),
                                        ChartDataEntry(x: 30.0, y: 100.0),
                                        ChartDataEntry(x: 40.0, y: 130.0)]
let sysLineEntries: [ChartDataEntry] = [ChartDataEntry(x: 20.0, y: 70.0),
                                        ChartDataEntry(x: 30.0, y: 80.0),
                                        ChartDataEntry(x: 40.0, y: 90.0)]
let pulseLineEntries: [ChartDataEntry] = [ChartDataEntry(x: 20.0, y: 90.0),
                                          ChartDataEntry(x: 30.0, y: 80.0),
                                          ChartDataEntry(x: 40.0, y: 70.0)]

So here is how I am trying to get the pixel point by using the Transformer but it returns the same coordinate which I pass to it.

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  let index = Int(entry.x)
  if transformer == nil {
      transformer = Transformer(viewPortHandler: chartView.viewPortHandler)
  }

  for (dataSetIndex, dataSet) in chartView.data!.dataSets.enumerated() {
    let item = (dataSet as! LineChartDataSet).entries[index]
    switch dataSetIndex {
    case 0: // dysLineEntries
      var point = CGPoint(x: item.xPx, y: item.yPx)
      transformer!.pointValueToPixel(&point)
    case 1: // sysLineEntries
      var point = CGPoint(x: item.x, y: item.y)
      transformer!.pointValueToPixel(&point)
    case 2: // pulseLineEntries
      var point = CGPoint(x: item.x, y: item.y)
      transformer!.pointValueToPixel(&point)
    default:
      break
    }
  }
}

I want to move yellow and red circle along with the blue circle on their respected line at same x position.

Basically I want the pixel points because I need to position the imageView at center position on all the 3 lines.


回答1:


I found a solution by creating my own Transformer and transforming the pointValue to pixels.

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  let index = Int(entry.x)
  if transformer == nil {
      transformer = Transformer(viewPortHandler: chartView.viewPortHandler)
      transformer?.prepareMatrixOffset(inverted: false)
      transformer?.prepareMatrixValuePx(chartXMin: chartView.chartXMin,
                                        deltaX: CGFloat(chartView.chartXMax-chartView.chartXMin),
                                        deltaY: CGFloat(chartView.chartYMax-chartView.chartYMin),
                                        chartYMin: chartView.chartYMin)
  }

  for (dataSetIndex, dataSet) in chartView.data!.dataSets.enumerated() {
    let item = (dataSet as! LineChartDataSet).entries[index]
    switch dataSetIndex {
    case 0: // dysLine
      var point = CGPoint(x: item.x, y: item.y)
      transformer!.pointValueToPixel(&point)
      dysImageView.center = point
    case 1: // sys
      var point = CGPoint(x: item.x, y: item.y)
      transformer!.pointValueToPixel(&point)
      sysImageView.center = point
    case 2: // pulse
      var point = CGPoint(x: item.x, y: item.y)
      transformer!.pointValueToPixel(&point)
      pulseImageView.center = point
    default:
      break
    }
  }
}


来源:https://stackoverflow.com/questions/60551080/how-can-i-get-highlight-xpx-highlight-ypx-of-the-other-lines-which-have-sa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!