如何在iOS图表中添加X轴上的字符串? [英] How to add Strings on X Axis in iOS-charts?

查看:81
本文介绍了如何在iOS图表中添加X轴上的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新版本中我创建了一些图表以前的代码有些麻烦:

With the new release i had some troubles to create some graphs the previous code was:

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

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
    let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
    barChartView.data = chartData
}

您可以使用以下行传递值,例如数月数组:

You can pass the values for example an array of months using the line:

let chartData = BarChartData(xVals: months, dataSet: chartDataSet)

新版本发布后代码为实现相同的图是:

After the new release the code to implement the same graph is:

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

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x: Double(i+2), y:values[i], data: months )
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")

    let chartData = BarChartData()
    chartData.addDataSet(chartDataSet)
    barChartView.data = chartData
}

我正在尝试几个小时但我找不到修改X轴值的方法,我希望有人可以帮助我,谢谢!

I was trying a few hours but i couldn't find a way to modify the X axis values, i hope someone can help me, thanks!!

推荐答案

我找到了解决方案,也许另一个可以更好地解决这个问题,而无需创建新类,这是我找到了什么:

I found the solution, maybe another one can solve this problem in a better way, without create a new class, well this is what I found:

首先你需要创建一个新类,它将是你的格式化程序类并添加IAxisValueFormater接口,然后你使用方法stringForValue来定制ize新值,它需要两个参数,第一个是实际值(你可以看到它像索引),第二个是值所在的轴。

First you nedd to create a new class, which will be your formatter class and add the IAxisValueFormater interface, then you use the method stringForValue to customize the new values, it takes two parameters, the first is the actual value (you can see it like the index) and second is the axis where the value is.

import UIKit
import Foundation
import Charts

@objc(BarChartFormatter)
public class BarChartFormatter: NSObject, IAxisValueFormatter
{
  var months: [String]! = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

  public func stringForValue(value: Double, axis: AxisBase?) -> String 
  {
    return months[Int(value)]
  }   
}

现在在你的setChart()函数开头的文件中,你必须为formatter类创建两个变量,为轴类创建一个变量:

Now in your file at the begining of your setChart() function you have to create two variables one for your formatter class and one for the axis class:

let formato:BarChartFormatter = BarChartFormatter()
let xaxis:XAxis = XAxis()

接下来,在for循环结束时继续并将索引和轴传递给formatter变量:

Next, go ahead at the end of the for in loop and pass the index and axis to your formatter variable:

formato.stringForValue(Double(i), axis: xaxis)

循环后将格式添加到轴变量:

After looping add the format to your axis variable:

xaxis.valueFormatter = formato

最后一步是将新的xaxisformatted添加到barChartView:

The final step is to add the new xaxisformatted to the barChartView:

barChartView.xAxis.valueFormatter = xaxis.valueFormatter

这就是所有其他行setChart()函数只要保持原样,我希望它可以帮到你。

And that's all the other lines in the setChart() function just keep it where they are, I hope it can help you.

这篇关于如何在iOS图表中添加X轴上的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆