ios – Charts – Swift – get UIColor of a selected level on the LineChartDataSet


So i’ve a graph with knowledge.
I’m making an attempt to get the colour of the chosen worth in an effort to present the identical colour on my Marker, but i cant determine it out tips on how to get colour 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(colour: .purple,
                           font: UIFont.systemFont(ofSize: 15),
                           textColor: .white,
                           insets: UIEdgeInsets(high: 5, left: 5, backside: 10, proper: 3))


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

override func setSelected(_ chosen: Bool, animated: Bool) {
    tremendous.setSelected(chosen, animated: animated)

    // Configure the view for the chosen state
}

override func layoutSubviews() {
    tremendous.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 Information Accessible"
    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, top: 20)
    lineChart.marker = marker
    
    
    let dataSets = [getLineChartDataSet()]
    let knowledge = LineChartData(dataSets: dataSets)
    knowledge.setValueFont(.systemFont(ofSize: 7, weight: .mild))
    lineChart.knowledge = knowledge
}

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

func getLineChartDataSet() -> LineChartDataSet {
    let dataPoints = getChartDataPoints(periods: [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, colours: gradientColors as CFArray, places: colorLocations) {
        dataSet.fill = LinearGradientFill(gradient: gradient, angle: 90.0)
    }
    
    dataSet.drawFilledEnabled = true

}
 }

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

Leave a Reply

Your email address will not be published.