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

X-Axis Date values not displaying properly? #2680

Closed
seenoevo opened this issue Aug 5, 2017 · 4 comments
Closed

X-Axis Date values not displaying properly? #2680

seenoevo opened this issue Aug 5, 2017 · 4 comments

Comments

@seenoevo
Copy link

seenoevo commented Aug 5, 2017

I'm trying to display the x-label values with date strings, but it doesn't look like it's displaying properly.

My array consists of the dates "01/02/16" and "02/17/16", but it seems to display "01/05/16" and "01/28/16" on the x-axis.

Also, my bar data disappears, the x-labels are not aligned along the axis correctly, and the y-data, not y-label values, are positioned weird and do not align with the x-data date labels.

Here's what I mean:

simulator screen shot aug 5 2017 4 08 25 pm


extension BarChartViewController: IAxisValueFormatter
{
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String
    {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd/yy"
        
        print("value = \(value)")
        let date = Date(timeIntervalSince1970: value)
        
        print("date = \(date)")
        return dateFormatter.string(from: date)
    }
}

override func viewDidLoad()
    {
        super.viewDidLoad()
        months = ["01/02/16", "02/17/16"]
        
        let formatter = DateFormatter()
        
        formatter.dateFormat = "MM/dd/yy"
        
        for theDate in months
        {
            let date = formatter.date(from: theDate)
            dates.append(date!)
        }
        
        let unitsSold = [20.0, 4.0]
        setChart(dataPoints: dates, values: unitsSold)
        barChartView.delegate = self

    }

func setChart(dataPoints: [Date], values: [Double])
    {
         var dataEntries: [BarChartDataEntry] = []

        for index in 0..<dataPoints.count
        {
            let date = dataPoints[index].timeIntervalSince1970
            let cost = values[index]

            let dataEntry = BarChartDataEntry(x: Double(date),
                                              y: cost)

            dataEntries.append(dataEntry)
        }
        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
        let chartData = BarChartData(dataSet: chartDataSet)
        
        barChartView.data = chartData

        barChartView.xAxis.valueFormatter = self

        barChartView.xAxis.granularity = 1
        barChartView.barData?.barWidth = 0.1

        chartDataSet.colors = ChartColorTemplates.material()
        
        barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
        
        barChartView.xAxis.setLabelCount(dates.count, force: false)
        
        barChartView.leftAxis.axisMinimum = 0.0
        barChartView.leftAxis.axisMaximum = 21.0

        barChartView.backgroundColor = UIColor.white
        barChartView.animate(yAxisDuration: 1.0, easingOption: .easeInOutQuad)

        barChartView.legend.enabled = false
        
        barChartView.scaleYEnabled = false
        barChartView.scaleXEnabled = false
        barChartView.pinchZoomEnabled = false
        barChartView.doubleTapToZoomEnabled = false
        
        barChartView.rightAxis.enabled = false

        barChartView.xAxis.drawAxisLineEnabled = true

        barChartView.zoom(scaleX: 0.0, scaleY: 0.0, x: 0, y: 0)
    }

Observations: The double value of date is 1451721600.0 and 1455696000.0, which corresponds to the dates 01/02/16 and 02/17/16 respectively.

However, the values printed out from the function stringForValue are 1452000000.0 and 1454000000.0, which corresponds to the dates 01/05/16 and 01/28/16, which is wrong.

Why is this happening? What am I doing wrong or missing? Thanks!

@seenoevo seenoevo changed the title xAxis Date values not displaying properly? X-Axis Date values not displaying properly? Aug 6, 2017
@seenoevo seenoevo closed this as completed Aug 6, 2017
@acegreen
Copy link

acegreen commented Aug 6, 2017

@seenoevo did you figure this out? In my case when I give x: TimeInterval, the bars are tiny and transparent (barely visible)

@danielgindi if you got any input on this, it would be highly appreciated

screen shot 2017-08-06 at 1 16 51 pm

@seenoevo
Copy link
Author

seenoevo commented Aug 7, 2017

@acegreen the problem for me was that the value in the stringForValue function was an index, so I should have used the value to reference my Date type array...i.e. date_ARRAY[ Int(value) ] instead of doing Date(timeIntervalSince1970: value).

I'm not sure about your problem as I dont have code to reference, but maybe try changing the bar's width or look at your stringForValue function to make sure it's returning the right string because stringForValue is what displays the labels on the x-axis.

@acegreen
Copy link

acegreen commented Aug 9, 2017

@seenoevo appreciate the response. But as you can see I was passing the correct value into my stringForValue and converting correctly. The problem is when I'm setting up my dataEntry, I was trying to pass in the timeInterval rather than an index.

That ended up breaking the graphs. When I do pass x: i (ie an index) it works fine. I was hoping to plot them directly and the chart would add gaps if the intervals have gaps.

let dataEntry = BarChartDataEntry(x: dates[i], y: values[i])

@fabio90h
Copy link

seenoevo could you post your code that works? I am having the same issue.

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

3 participants