Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line chart fill color is faded

I am trying to setup a line chart with one fill colour but for some reason, the fill colour is faded.

Example

enter image description here

Both the random view I have added to middle of screen and the fill colour of the line chart are set to be red, but for some reason the fill colour of the chart is faded.

Can see code here

@IBOutlet var liveChart : LineChartView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "Chart Tests"

        configureChart(chart: liveChart)

        var xAxis = [String]()
        var yAxis = [Double]()
        for _ in 0..<10
        {
            xAxis.append("")
            let yVal = Double(randomBetweenNumbers(firstNum: 1.0, secondNum: 100.0))
            yAxis.append(yVal)
        }
        setData(xAxisArray: xAxis, yAxisArray: yAxis, chart: liveChart)

        let testView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        testView.center = view.center
        testView.backgroundColor = UIColor.red
        view.addSubview(testView)
    }

    func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat{
        return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
    }

    func configureChart(chart : LineChartView)
    {
        chart.chartDescription?.text = ""
        chart.noDataText = "Loading Data"
        chart.backgroundColor = UIColor.clear

        chart.drawGridBackgroundEnabled = false
        chart.dragEnabled = true
        chart.rightAxis.enabled = false
        chart.leftAxis.enabled = true
        chart.doubleTapToZoomEnabled = false
        chart.legend.enabled = false
        chart.pinchZoomEnabled = true
        chart.highlightPerTapEnabled = false
        chart.highlightPerDragEnabled = false

        chart.xAxis.enabled = false
        chart.leftAxis.drawAxisLineEnabled = false
        chart.leftAxis.drawGridLinesEnabled = false
        chart.leftAxis.labelCount = 5
        chart.leftAxis.forceLabelsEnabled = true
    }

    func setData(xAxisArray : [String], yAxisArray : [Double], chart : LineChartView)
    {
        var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
        if(xAxisArray.count > 0)
        {
            for i in 0 ..< xAxisArray.count
            {
                let chartEntry = ChartDataEntry(x: Double(i), y: yAxisArray[i], data: nil)
                yVals1.append(chartEntry)
            }
        }
        let set1: LineChartDataSet = LineChartDataSet(values: yVals1, label: "")
        set1.fillColor = UIColor.red
        set1.drawFilledEnabled = true
        set1.drawCirclesEnabled = false

        let data = LineChartData()
        data.addDataSet(set1)

        liveChart.data = data
    }

Is there a way to fix this? Or is this just the way the fill colour of the chart works?

Edit:

I am using

https://github.com/danielgindi/Charts

like image 807
AdamM Avatar asked Mar 09 '23 08:03

AdamM


1 Answers

I assume you use this library: https://github.com/kevinbrewster/SwiftCharts

So the LineChartView automatically set alpha for the fill color: https://github.com/kevinbrewster/SwiftCharts/blob/master/SwiftCharts/LineChart.swift#L758

try to set fillAlpha property of the data set

like image 166
Andrey Gershengoren Avatar answered Apr 01 '23 09:04

Andrey Gershengoren