Search code examples
iosswiftlinechartios-charts

Line Chart xAxis label with multiple colour using (iOS-charts) and LineChartData setValueTextColor


Here i'm using iOS-charts to implement line chart. I'm stuck with using multiple colours for xAxis labels and value at each data point.

https://github.com/danielgindi/Charts

For xAxis i need NSAttributedString with two different colours, this is the code is used. Unable to figure it out how to do this.

    xChart.drawLabelsEnabled = true
    xChart.labelCount = 20
    xChart.labelFont = SomeFont
    xChart.labelTextColor = UIColor.white        
    xChart.labelPosition = .bottom
    xChart.labelHeight = 52.0
    xChart.granularityEnabled = true
    xChart.granularity = 1
    xChart.valueFormatter = IndexAxisValueFormatter(values: self.weeks)

similarly for valueTextColor at each data-point

    let lineChartData = LineChartData(dataSets: dataSets)
    self.lineChart.data = lineChartData

    // Enables Text above Circle - Color and Font
    lineChartData.setDrawValues(true)
    lineChartData.setValueTextColor(.white)        
    lineChartData.setValueFont(some font)

And used value formatter to be in Int

This is what I want to implement the one with green circles with two different colors

This is what I want to implement the one with green circles with two different colors

This is what I got

This is what I got

Can anyone provide a solution to this??

Answered by @CodeChanger has fixed one issue, looking forward for another one at bottom of the image 19 Jun


Solution

  • For your requirements there is one property in ChartDataSet named "valueColors".

    Please check below code to change your value color based on custom condition you can change color of your value.

    let chartDataSet = LineChartDataSet(values: dataEntries, label: label)
    chartDataSet.valueColors = [UIColor(red: 0.192, green: 0.686, blue: 0.980, alpha: 1.00),
                                        UIColor(red: 0.212, green: 0.863, blue: 0.318, alpha: 1.00),
                                        UIColor(red: 0.996, green: 0.867, blue: 0.275, alpha: 1.00),
                                        UIColor(red: 0.980, green: 0.090, blue: 0.157, alpha: 1.00)]
    

    By This you can change your color of your value text.