ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.58k stars 6k forks source link

Issue while setting LinearDataSet #5086

Open SmritiGangele1 opened 1 year ago

SmritiGangele1 commented 1 year ago

ℹ Please fill out this template when filing an issue. All lines beginning with an ℹ symbol instruct you with what info we expect. You can delete those lines once you've filled in the info.

Per our *CONTRIBUTING guidelines, we use GitHub for bugs and feature requests, not general support. Other issues should be opened on Stack Overflow with the tag ios-charts.

Please remove this line and everything above it before submitting.

What did you do?

func configure() {

    chartView.clear()
    guard let rows = self.rows, let rowDataItem = self.rowDataItem else { return }

    if(/rows.count == 1){
        var dataPoints  = [String]()
        var max : Int = 0
        rows.forEach({ model in
            if(Int(/Double(model.value ?? "")) > max){
                max = Int(/Double(model.value ?? ""))
            }
            dataPoints.append(/model.date)
        })

        setChartViewProperties(max)

        var values = (0 ..< /dataPoints.count).map { (i) -> ChartDataEntry in
            if(i == /rows.count){
                return(ChartDataEntry(x: /Double(i), y: /Double(0)))
            }
            return ChartDataEntry(x: /Double(i), y: /Double(rows[i].value ?? ""))
        }

        if(rows.count == 1){
            values.removeAll()
            dataPoints.removeAll()
            values.append(ChartDataEntry(x: /Double(0), y: /Double(0)))
            values.append(ChartDataEntry(x: /Double(1), y: /Double(rows[0].value ?? "")))
            values.append(ChartDataEntry(x: /Double(2), y: /Double(0)))
            dataPoints.append("")
            dataPoints.append(rows[0].date ?? "")
            dataPoints.append("")
        }

        let set1 = LineChartDataSet(entries: values, label:"")
        set1.drawIconsEnabled = false
        setup(set1)

        let gradientColors = [UIColor.AppThemeGreenColor().cgColor,
                              #colorLiteral(red: 0.3019607843, green: 0.662745098, blue: 0.8156862745, alpha: 1).cgColor]
        let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)!

        set1.fillAlpha = 1
        set1.drawFilledEnabled = false
        let data = LineChartData(dataSet: set1)
        chartView.data = data

    }
    else if(/rows.count > 0) {
        var dataPoints  = [String]()
        var max : Int = 0
        rows.forEach({ model in
            if(Int(/Double(model.value ?? "")) > max){
                max = Int(/Double(model.value ?? ""))
            }
            dataPoints.append(/model.date)
        })

        self.setChartViewProperties(max)

        var values = (0 ..< /dataPoints.count).map { (i) -> ChartDataEntry in
            if(i == /rows.count){
                return(ChartDataEntry(x: /Double(i), y: /Double(0)))
            }
            return ChartDataEntry(x: /Double(i), y: /Double(rows[i].value ?? ""))
        }

        if(rows.count == 1){
            values.removeAll()
            dataPoints.removeAll()
            values.append(ChartDataEntry(x: /Double(0), y: /Double(0)))
            values.append(ChartDataEntry(x: /Double(1), y: /Double(rows[0].value ?? "")))
            values.append(ChartDataEntry(x: /Double(2), y: /Double(0)))
            dataPoints.append("")
            dataPoints.append(rowDataItem.date ?? "")
            dataPoints.append("")
        }

        let set1 = LineChartDataSet(entries: values, label:"")
        set1.drawIconsEnabled = false

        setup(set1)

        let gradientColors = [UIColor.AppThemeGreenColor().cgColor,
                              #colorLiteral(red: 0.3019607843, green: 0.662745098, blue: 0.8156862745, alpha: 1).cgColor]
        let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)!

        set1.fillAlpha = 1
        set1.fill = LinearGradientFill(gradient: gradient, angle: 90)
        set1.drawFilledEnabled = true

        let data = LineChartData(dataSet: set1)

        chartView.data = data

    }
    else {
        chartView.clear()
        chartView.data = nil
    }
}

/*
  Chart view to show the actual graph according to dataSet and the range
 */
func setChartViewProperties(_ max: Int) {

    guard let rows = self.rows, let rowDataSet = self.rowDataItem else { return }
    var numericMaxValue = "", numericMinValue = "", maxValue = 0.0, minRange = "", maxRange = ""

    if !(/rowDataSet.minrange?.isEmpty) {
        if let numericMinValueFromString = rowDataSet.minrange?.replacingOccurrences(of: "<", with: "") {
            numericMaxValue = numericMinValueFromString
            numericMinValue = "0.0"
        } else {
            numericMinValue = /rowDataSet.minrange
        }
    }
    if !(/rowDataSet.maxrange?.isEmpty) {
        if let numericMaxValueFromStrig = rowDataSet.maxrange?.replacingOccurrences(of: ">", with: "") {
            numericMaxValue = "\(/Double(numericMaxValueFromStrig) * 2.0)"
            numericMinValue = numericMaxValueFromStrig
        } else {
            numericMaxValue = /rowDataSet.maxrange
        }
    }

    chartView.minOffset = 20
    chartView.isUserInteractionEnabled = false
    chartView.drawGridBackgroundEnabled = false
    chartView.xAxis.drawGridLinesEnabled = false
    chartView.chartDescription.enabled = false
    chartView.dragEnabled = true
    chartView.setScaleEnabled(true)
    chartView.pinchZoomEnabled = true
    chartView.noDataTextAlignment = .center

    let leftAxis = chartView.leftAxis
    leftAxis.removeAllLimitLines()
    leftAxis.axisMaximum = /Double(max + 10)//Double(numericMaxValue) //Double(max + 10)
    leftAxis.axisMinimum =  0 ///Double(numericMinValue)
    let xAxis = chartView.xAxis
    xAxis.labelPosition = .bottom
    chartView.xAxis.centerAxisLabelsEnabled = true
    chartView.xAxis.granularityEnabled = true

    if(/rows.count > 1) {
        xAxis.axisMinimum = 0
    } else {  xAxis.axisMinimum = 0 }

    xAxis.granularity = 1
    xAxis.labelRotationAngle = CGFloat(-25)
    xAxis.valueFormatter = self

    chartView.xAxis.valueFormatter = self

// chartView.xAxis.enabled = true chartView.rightAxis.enabled = false chartView.xAxis.gridColor = .clear chartView.leftAxis.gridColor = .clear chartView.rightAxis.gridColor = .clear chartView.legend.enabled = false chartView.animate(xAxisDuration: 0.0) // chartView.animate(yAxisDuration: 1)

    chartView.xAxis.drawAxisLineEnabled = true
}

func setup(_ dataSet: LineChartDataSet) {

    dataSet.lineDashLengths = nil
    dataSet.highlightLineDashLengths = nil
    if(/rows?.count == 1){
        dataSet.setColors(.black)
        dataSet.setCircleColors(.black)
    }
    else {
        dataSet.setColors(.black)
        dataSet.setCircleColors(.black)
    }
    dataSet.gradientPositions = [0, 40, 100]
    dataSet.lineWidth =  (/rows?.count == 1) ? 0 : 2
    dataSet.circleRadius = 4
    dataSet.drawCircleHoleEnabled = false
    dataSet.valueFont = .systemFont(ofSize: 9)
    dataSet.formLineDashLengths = nil
    dataSet.formLineWidth =  1
    dataSet.formSize = 15
    dataSet.valueFormatter = CustomIntFormatter1()
}

} using the above i am getting the graph like this

Screenshot 2023-07-22 at 3 44 54 PM

What did you expect to happen?

i want the graph like this NOTE:- Do not go with the values inside the image The green color area i want to show between the range the data points lie on

Screenshot 2023-07-22 at 3 05 42 PM

What happened instead?

ℹ Please replace this with of what happened instead.

DGCharts Environment

DGCharts version/Branch/Commit Number: Xcode version: Swift version: Platform(s) running DGCharts: macOS version running Xcode:

Demo Project

ℹ Please link to or upload a project we can download that reproduces the issue.

Screenshot 2023-07-22 at 3 05 42 PM Screenshot 2023-07-22 at 3 44 50 PM

Please provide the solution for this.