ChartsOrg / Charts

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

LineCharView get flat #5064

Open HafizMuhammadMurtaza opened 1 year ago

HafizMuhammadMurtaza commented 1 year ago

I am giving the correct data for x and y axis, but sometimes line charts get flat, then after refresh it becomes in it normal position. Screenshot 2023-05-29 at 3 38 21 PM Screenshot 2023-05-29 at 3 39 02 PM

Following is my code of creating LineChartView

`import Charts import SwiftUI

struct MiniChartView: UIViewRepresentable { var entries: [ChartDataEntry]

typealias UIViewType = LineChartView

func makeUIView(context: Context) -> LineChartView {
    let chart = LineChartView()
    chart.data = generateData()
    chart.legend.enabled = false
    chart.xAxis.drawGridLinesEnabled = false
    chart.leftAxis.drawGridLinesEnabled = false
    chart.chartDescription.enabled = false
    chart.xAxis.drawGridLinesEnabled = false
    chart.xAxis.drawLabelsEnabled = false
    chart.xAxis.drawAxisLineEnabled = false
    chart.rightAxis.enabled = false
    chart.leftAxis.enabled = false
    chart.drawBordersEnabled = false
    chart.legend.form = .none
    chart.isUserInteractionEnabled = false
    return chart
}

func updateUIView(_ uiView: LineChartView, context: Context) {
    uiView.data = generateData()
}

func generateData() -> LineChartData {
    let dataSet = LineChartDataSet(entries: entries)
    dataSet.lineDashLengths = nil
    dataSet.highlightLineDashLengths = nil
    dataSet.gradientPositions = [0, 40, 100]
    dataSet.lineWidth = 2
    dataSet.circleRadius = 0
    dataSet.drawCircleHoleEnabled = false
    dataSet.valueFont = .systemFont(ofSize: 0)
    dataSet.formLineDashLengths = nil
    dataSet.formLineWidth = 1
    dataSet.formSize = 2
    dataSet.mode = .cubicBezier

    let colorTop = UIColor(red: 50/255, green: 200/255, blue: 255/255, alpha: 1.0).cgColor
    let colorBottom = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.0).cgColor
    let gradientColors = [colorTop, colorBottom] as CFArray // Colors of the gradient
    let colorLocations: [CGFloat] = [0.6, 0.0] // Positioning of the gradient

    if let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) {
        dataSet.fill = LinearGradientFill(gradient: gradient, angle: 90.0) // Set the Gradient
        dataSet.drawFilledEnabled = true
    }

    let data = LineChartData(dataSet: dataSet)
    return data
}

}`