问题
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))
}
}
回答1:
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.
回答2:
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.
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.
}
}
来源:https://stackoverflow.com/questions/38474766/ios-charts-invert-the-x-axis-direction