简体   繁体   中英

iOS Charts centering x- and y-axis

For plotting graphs I use the Charts framework from this site https://github.com/danielgindi/Charts . I want to know if it possible to center the x- and y-axis to look like this:

在此处输入图片说明

I tried to move the x-Axis and the left-Axis to center, but it only moves the label values but not the axes, as you can see in the below picture在此处输入图片说明

Here is an example of my code:

var lineChartView: LineChartView!
override func viewDidLoad() {
    super.viewDidLoad()
    
    self.view.backgroundColor = .white
    lineChartView = LineChartView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    lineChartView?.delegate = self
    self.view.addSubview(lineChartView!)
    
    let ys1 = Array(0..<100).map { x in ChartDataEntry(x: Double(x) / 10, y: sin(Double(x) / 10)) }
    let ys2 = Array(0..<100).map { x in ChartDataEntry(x: Double(x) / 10, y: cos(Double(x) / 10)) }
    
    let data = LineChartData()
    let ds1 = LineChartDataSet(entries: ys1, label: "sine")
    ds1.colors = [NSUIColor.red]
    ds1.drawCirclesEnabled = false
    ds1.drawValuesEnabled = false
    ds1.mode = .cubicBezier
    data.addDataSet(ds1)
    
    let ds2 = LineChartDataSet(entries: ys2, label: "cosine")
    ds2.colors = [NSUIColor.blue]
    ds2.drawCirclesEnabled = false
    ds2.drawValuesEnabled = false
    ds2.mode = .cubicBezier
    data.addDataSet(ds2)
    self.lineChartView.data = data
    lineChartView.xAxis.enabled = true
    lineChartView.xAxis.labelPosition = .bottom
    lineChartView.xAxis.yOffset = -self.view.frame.height/2
    
    lineChartView.rightAxis.enabled = false
    
    // left axis
    lineChartView.leftAxis.xOffset = -self.view.frame.width/2
        
}

you can try this code, you might need to set chart.setVisibleRange too

chart.xAxis.granularity = 1
chart.leftAxis.granularity = 1
chart.leftAxis.axisMaximum = maxYValue
chart.xAxis.axisMaximum = maxXValue
chart.xAxis.axisMinimum = -maxXValue
chart.leftAxis.axisMinimum = -maxYValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM