IOS charts invert the x-axis direction

I have a chart built using iOS Daniel Gindi charts . It presents historical data for a specific period of time. The problem I am facing is that the data is being built from left to right (new data β†’ old data). I need this to be built from right to left (old data β†’ new data). I know that I can change the order of data entry in BarChartData , but then I have a problem with the fact that the chart is still aligned. It must be properly aligned. I found a discussion here , talking about the problem of reversing the x axis and how to solve it (currently this is not an included frame function), but I can’t understand what really needs to be done. Here are examples of what I need:

Here's what my chart looks like:

enter image description here

This is what I need to look like this:

enter image description here

My questions

Is there a way to invert the x axis?

or

Is there a way to align the chart to the right?

Here are some of my codes and attempts to solve the problem:

 class ViewController: UIViewController { @IBOutlet weak var barChartView: BarChartView! //... func plotChart() { // Create history data //... barChartView.data = chartData // 'chartData' is the BarChartData() containing all of the history information // No efect barChartView.legend.direction = .RightToLeft // Chart breaks barChartView.leftAxis.axisMinValue = 30 barChartView.leftAxis.axisMaxValue = 0 // Breaks the ability to zoom barChartView.setVisibleXRangeMinimum(CGFloat(40)) barChartView.setVisibleXRangeMaximum(CGFloat(1)) } } 
+5
source share
2 answers

There is a variable in the chartLegend.swift file:

public var direction = ChartLegendDirection.LeftToRight

Try to make changes to this file, it may solve your problem.

+1
source

This library does not support direct alignment from left to right.

Refer - https://github.com/danielgindi/Charts/issues/738

You need to implement something at the library level to make it smooth the alignment from right to left.

Edited Answer

After I worked on this, I found a solution, hope this helps you.

Download sample code

 import UIKit class ViewController: UIViewController { @IBOutlet weak var barChartView: BarChartView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let months = ["7/23/2016", "7/22/2016", "7/21/2016", "7/20/2016", "7/19/2016", "7/18/2016", "7/17/2016"] let unitsSold = [0.0, 0.0, 0.0, 0.0, 20.0, 50.0, 30.0] setChart(months, values: unitsSold) } func setChart(dataPoints: [String], values: [Double]) { var dataEntries: [BarChartDataEntry] = [] for i in 0..<dataPoints.count { if !values[i].isZero { let dataEntry = BarChartDataEntry(value: values[i], xIndex: i) dataEntries.append(dataEntry) } } let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "") let chartData = BarChartData(xVals: dataPoints, dataSet: chartDataSet) barChartView.data = chartData barChartView.leftAxis.enabled = false barChartView.descriptionText = "" barChartView.xAxis.labelPosition = .Bottom } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

enter image description here

0
source

All Articles