Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Right Y-Axis values displaying incorrectly in LineChartDemo #665

Closed
adriansoghoian opened this issue Jan 11, 2016 · 5 comments
Closed

Right Y-Axis values displaying incorrectly in LineChartDemo #665

adriansoghoian opened this issue Jan 11, 2016 · 5 comments

Comments

@adriansoghoian
Copy link

Thanks for an awesome library, my app is using it extensively to plot stock market related data. When the app launches, I'll send a note in case you'd like to take a look.

I currently have a strange bug where the Y-axis values are being plotted incorrectly -- it looks like they're being slightly offset.

screen shot 2016-01-11 at 11 02 05 am

I'm not doing any custom scaling with the data. Here are the relevant code snippets below:

func setChartDefaults() {
    // No data view
    self.lineChartView!.noDataText = ""
    self.lineChartView!.descriptionText = ""
    self.lineChartView!.noDataTextDescription = ""

    // Legend
    self.lineChartView!.legend.enabled = false


    // Interaction
    self.lineChartView!.setScaleEnabled(true)
    self.lineChartView!.pinchZoomEnabled = true
    self.lineChartView!.doubleTapToZoomEnabled = true

    // Background
    self.lineChartView!.drawGridBackgroundEnabled = true
    self.lineChartView!.gridBackgroundColor = UIColor.clearColor()
    self.lineChartView!.backgroundColor = UIColor.clearColor()

    // Borders
    self.lineChartView!.borderColor = UIColor.clearColor()
    self.lineChartView!.drawBordersEnabled = false

    // Format x axis
    self.lineChartView!.xAxis.enabled = true
    self.lineChartView!.xAxis.labelPosition = .Bottom
    self.lineChartView!.xAxis.drawAxisLineEnabled = false
    self.lineChartView!.xAxis.drawGridLinesEnabled = false
    self.lineChartView!.xAxis.labelFont = UIFont.TriggerFontSemiBold(11)
    self.lineChartView!.xAxis.axisLineWidth = 1.0
    self.lineChartView!.xAxis.spaceBetweenLabels = 9
    self.lineChartView!.xAxis.avoidFirstLastClippingEnabled = true

    // Remove left axis
    self.lineChartView!.leftAxis.enabled = false
    self.lineChartView!.leftAxis.drawAxisLineEnabled = false
    self.lineChartView!.leftAxis.spaceTop = 0.5
    self.lineChartView!.leftAxis.startAtZeroEnabled = false
    self.lineChartView!.leftAxis.spaceBottom = 0.4

    // Add right axis
    self.lineChartView!.rightAxis.enabled = true
    self.lineChartView!.rightAxis.drawAxisLineEnabled = true
    self.lineChartView!.rightAxis.startAtZeroEnabled = false
    self.lineChartView!.rightAxis.drawGridLinesEnabled = true
    self.lineChartView!.rightAxis.drawAxisLineEnabled = false

    // Format right axis font
    self.lineChartView!.rightAxis.labelFont = UIFont.TriggerFontSemiBold(12)
    self.lineChartView!.rightAxis.valueFormatter = NSNumberFormatter()
    self.lineChartView!.rightAxis.valueFormatter?.maximumFractionDigits = 2
}
func setLineChartDataSetDefaults() { // Sets preferences
    self.lineChartDataSet = LineChartDataSet()
    self.lineChartData = LineChartData()

    // Color of points
    self.lineChartDataSet!.setColor(UIColor.TriggerWhiteColor())

    // Width
    self.lineChartDataSet!.lineWidth = 1.5

    // Plot circles around points
    self.lineChartDataSet!.highlightEnabled = false
    self.lineChartDataSet!.drawCirclesEnabled = false
    self.lineChartDataSet!.drawValuesEnabled = true
    self.lineChartDataSet!.drawFilledEnabled = true
}
func graph(xData: [String], yData: [Double]) {
    for i in 0..<ydata.count { 
        let dataEntry = ChartDataEntry(value: ydata[i], xIndex: i)
        self.lineChartDataSet!.addEntry(dataEntry)
    }

    self.lineChartData!.addDataSet(self.lineChartDataSet)

    for i in 0..<xdata.count {
        self.lineChartData!.addXValue(NSDate.returnCustomFormattedDateString(xdata[i]))
    }

    self.lineChartView!.data = self.lineChartData
}

Thank you in advance for any help you can provide!

@liuxuan30
Copy link
Member

I don't see this issue on my side. And your code seems fine. What I can suggest is, try first setup up the chart, and set chartView.data at last.

If you still has the problem, could you use ChartsDemo to reproduce it and post the data set code?

@AndreasIgelCC
Copy link
Contributor

Have you tried setting the axisDependency? Default ist a left dependency and I have not seen any code in the Framework to change that, if the left axis is disabled.

self.lineChartDataSet!.axisDependency = ChartYAxis.AxisDependency.Right

@liuxuan30
Copy link
Member

Oh yeah, I remember it, @adriansoghoian does not set axis dependency when he turns off left axis and enabled the right one. based on @AndreasIgelCC's comments, I believe it's the reason.
Closing

@adriansoghoian
Copy link
Author

Awesome, many thanks @AndreasIgelCC! Confirming that setting the axis dependency has fixed the issue.

@Rj707
Copy link

Rj707 commented Dec 4, 2017

is there anyway to make the line chart look like IOS Stock App??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants