Search code examples
iosswiftios-charts

Showing/Hiding lines with LineChart


I have the following code to display lines on my chart using ChartsRealm. How can I show/hide individual lines on the chart?

let line1 = LineChartDataSet(values: lineChartEntry1, label: "CH4")
    let line2 = LineChartDataSet(values: lineChartEntry2, label: "O2")
    let line3 = LineChartDataSet(values: lineChartEntry3, label: "H2S")
    line1.colors = [NSUIColor.blue]
    line2.colors = [NSUIColor.green]
    line3.colors = [NSUIColor.red]
    let data = LineChartData()
    data.addDataSet(line1)
    data.addDataSet(line2)
    data.addDataSet(line3)
    chtChart.data = (data)

Solution

  • As such there is no methods to hide/show data set in Chart library.

    But we can do it logically without refreshing page.

    Please check below code for the same:

    Create LineChartView Extension:

    extension LineChartView {
        func setLineChartData(xValues: [String], labels: [String],dataSets:[Array<Double>],colors:[UIColor]) {
    
            var dataSetsArray: [LineChartDataSet] = []
            let dataSetCount = self.lineData?.dataSets.count
            if dataSetCount != nil && dataSetCount! > 0 {
                self.data = nil
            }
    
            for i in 0..<dataSets.count {
                let yVals = dataSets[i]
                var dataEntries: [ChartDataEntry] = []
    
                for i in 0..<yVals.count {
                    let dataEntry = ChartDataEntry(x: Double(i), y: yVals[i])
                    dataEntries.append(dataEntry)
                }
    
                let chartDataSet = LineChartDataSet(values: dataEntries, label: labels[i])
                chartDataSet.highlightEnabled = true
                chartDataSet.drawHorizontalHighlightIndicatorEnabled = false
                chartDataSet.highlightColor = .blue
                chartDataSet.highlightLineWidth = 1
                chartDataSet.lineWidth = 2.0
                chartDataSet.mode = .horizontalBezier
                chartDataSet.drawCircleHoleEnabled = true
                chartDataSet.drawValuesEnabled = true
                chartDataSet.axisDependency = .left
                chartDataSet.circleHoleColor = colors[i]
                chartDataSet.highlightColor = colors[i]
                chartDataSet.colors = [colors[i]]
                //Add Data Set Into Sets array
                dataSetsArray.append(chartDataSet)
    
            }
    
            let chartData = LineChartData(dataSets: dataSetsArray)
            self.data = chartData
    
            self.animate(xAxisDuration: 1.0,easingOption: .easeInExpo)
    
        }
    
        func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        }
    
    }
    

    By adding one method into LineChartView extension and call it as per your requirements or user actins.

    I called it from my segment change method like below :

    @IBAction func segmentValueChanged(_ sender: Any) {
    
        switch self.lineSegment.selectedSegmentIndex {
        case 0:
            let colors = [UIColor.red,UIColor.green,UIColor.blue]
            lineChartView.setLineChartData(xValues: months,
                                           labels: ["Monthly Sales","Quarterly Sales","Yearly Sales"],
                                           dataSets: [unitsSold,unitsSold1,unitsSold2],
                                           colors: colors)
            break
        case 1:
            lineChartView.setLineChartData(xValues: months,
                                           labels: ["Monthly Sales"],
                                           dataSets: [unitsSold],
                                           colors: [UIColor.red])
            break
        case 2:
            lineChartView.setLineChartData(xValues: months,
                                           labels: ["Quarterly Sales"],
                                           dataSets: [unitsSold1],
                                           colors: [UIColor.green])
            break
        case 3:
            lineChartView.setLineChartData(xValues: months,
                                           labels: ["Yearly Sales"],
                                           dataSets: [unitsSold2],
                                           colors: [UIColor.blue])
            break
    
        default:
            break
        }
    
    }
    

    you can pass different data what you want in your dataset.

    Check below screens for output reference:

    My Segments are All | Monthly | Quarterly | Yearly

    enter image description here enter image description here enter image description here enter image description here

    Hope this will helps you to achieve your requirements!