'iOS-charts invert the X axis direction
I have a bar chart built using Daniel Gindi iOS-charts. It represents history data over a period of time. The issue I am having is that the data is being plotted from left-to-right (new data -> old data). I need it to be plotted as right-to-left (old data -> new data). I know that I can reverse the order I input data into BarChartData
, but then I have the issue of the chart still being left aligned. It needs to be right aligned. I found a discussion here talking about the issue of inverting the x-axis and how it could be resolved (currently not an included feature of the framework), however I can't figure out what actually needs to be done. Here are examples of what I need:
This is what my chart currently looks like:
This is what I needs it to look like:
My Questions
Is there a way to invert the x-axis?
or
Is there a way to right align the chart?
Here is some of my code and attempts to resolve the issue:
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))
}
}
Solution 1:[1]
This library doesn't support right to left alignment directly.
Refer - https://github.com/danielgindi/Charts/issues/738
You have to implement something on library level to make it trick right to left alignment.
Edited answer
After working some around, i found the solution, hope this will help you out.
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.
}
}
Solution 2:[2]
In the chartLegend.swift
file there is a variable:
public var direction = ChartLegendDirection.LeftToRight
Try to make the change in that file, it might solve your problem.
Solution 3:[3]
Ios charts to support Right to left alignment
Steps:
- enable right axis & disable left axis
- invert the data
- set yaxis dependency while drawing Yaxis
chartView.getAxis(YAxis.AxisDependency.right)
- set axis dependency for barChartDataSet as
YAxis.AxisDependency.right
You are done
Happy Reading
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | biddulph.r |
Solution 3 | manoj kiran |