Home > OS >  Charts - Swift - get UIColor of a specific point on the LineChartDataSet
Charts - Swift - get UIColor of a specific point on the LineChartDataSet

Time:08-28

So i have a graph with data. I am trying to get the color of the selected value in order to show the same color on my Marker, yet i cant figure it out how to get color on the lineChart:

class GraphTableViewCell: UITableViewCell {
@IBOutlet weak var yieldLabel: UILabel!
@IBOutlet weak var yieldPercentLabel: UILabel!
@IBOutlet weak var lineChart: LineChartView!
@IBOutlet weak var graphButtonView: AssetGraphButtonView!
@IBOutlet weak var endDateLabel: UILabel!
@IBOutlet weak var startDateLabel: UILabel!

var selectionView: AssetGraphSelectionView!

var viewModel: GraphViewModelType!
var set: LineChartDataSet!
var marker = BalloonMarker(color: .red,
                           font: UIFont.systemFont(ofSize: 15),
                           textColor: .white,
                           insets: UIEdgeInsets(top: 5, left: 5, bottom: 10, right: 3))


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

override func layoutSubviews() {
    super.layoutSubviews()
}

func config(with viewModel: GraphViewModelType) {
    self.viewModel = viewModel
    yieldLabel.attributedText = viewModel.titleAttributeText
    yieldPercentLabel.attributedText = viewModel.yielAttributeText
    startDateLabel.attributedText = viewModel.startDateAttributeText
    endDateLabel.attributedText = viewModel.endDateAttributeText

    graphButtonView.confgiureCell(with: self.viewModel.btnData)
    setUpChart()
}

func setUpChart() {
    lineChart.delegate = self
    lineChart.noDataText = "No Data Available"
    lineChart.rightAxis.enabled = false
    lineChart.leftAxis.enabled = false
    lineChart.xAxis.enabled = false
    lineChart.legend.enabled = false
    lineChart.xAxis.drawGridLinesEnabled = false
    lineChart.drawMarkers = true
    lineChart.doubleTapToZoomEnabled = false
    lineChart.pinchZoomEnabled = false
    lineChart.scaleXEnabled = false
    lineChart.scaleYEnabled = false
    

    marker.chartView = lineChart
    marker.minimumSize = CGSize(width: 28, height: 20)
    lineChart.marker = marker
    
    
    let dataSets = [getLineChartDataSet()]
    let data = LineChartData(dataSets: dataSets)
    data.setValueFont(.systemFont(ofSize: 7, weight: .light))
    lineChart.data = data
}

func getChartDataPoints(sessions: [Int], accuracy: [Double]) -> [ChartDataEntry] {
    var dataPoints: [ChartDataEntry] = []
    for count in (0..<sessions.count) {
        dataPoints.append(ChartDataEntry.init(x: Double(sessions[count]), y: accuracy[count]))
    }
    return dataPoints
}

func getLineChartDataSet() -> LineChartDataSet {
    let dataPoints = getChartDataPoints(sessions: [0,1,2,3,4,5,6,7,8,9,10], accuracy: [100, 20.0, 30.0, 50.0, 105.3, 100, 43.8, 100, 82, 57, 122])
    set = LineChartDataSet(entries: dataPoints, label:"")
    setup(set)

    return set
}

func setup(_ dataSet: LineChartDataSet) {
    
    dataSet.drawHorizontalHighlightIndicatorEnabled = false
    dataSet.drawVerticalHighlightIndicatorEnabled = true
    
    dataSet.isDrawLineWithGradientEnabled = true
    dataSet.fillAlpha = 0.15
    dataSet.lineWidth = 2
    dataSet.circleRadius = 0
    dataSet.drawCircleHoleEnabled = false
    dataSet.drawCirclesEnabled = false
    dataSet.drawValuesEnabled = false
    dataSet.highlightColor = .blue
    
    dataSet.setColors(ChartColorTemplates.colorFromString("#FA3A7A"), ChartColorTemplates.colorFromString("#C257B1"),
                      ChartColorTemplates.colorFromString("#8B73E8"))
    dataSet.gradientPositions = [0, 40, 100]
    
    let gradientColors = [ChartColorTemplates.colorFromString("#FC4684").cgColor,
                          ChartColorTemplates.colorFromString("#D8D8D8").cgColor]
    let colorLocations:[CGFloat] = [1.0, 0.0]
    if let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: colorLocations) {
        dataSet.fill = LinearGradientFill(gradient: gradient, angle: 90.0)
    }
    
    dataSet.drawFilledEnabled = true

}
 }

extension GraphTableViewCell: ChartViewDelegate {
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
    
    print("chartValueSelected : x = \(highlight.x) y = \(highlight.y)")
   
}
}

CodePudding user response:

In the method chartValueSelected you need to get the point of the selectedValue by:

let transform = chartView.getTransformer(forAxis: .left)
let point = transform.pixelForValues(x: highlight.x, y: highlight.y)

With the CGPoint of the selected value, go to the pixel and pick the color using the method at https://stackoverflow.com/a/56723477/14087244

Call the method on your lineChart like this

let color = lineChart.colorOfPoint(point: point)

Complete code:


func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
    let transform = chartView.getTransformer(forAxis: .left)
    let point = transform.pixelForValues(x: highlight.x, y: highlight.y)
    let color = lineChart.colorOfPoint(point: point)
}
  • Related