iOS swift3 Chart xaxis重复值问题
<p><p>我在使用 iOS 图表库为 xAxis 创建字符串值时遇到问题</p>
<p>x值总是有重复值,看下图,你可以看到值总是<code>JAN JAN JAN JAN FEB FEB FEB</code></p>
<p>我如何设置图表的 x 值,因为它将显示 <code>JAN FEB MAR</code>?</p>
<pre><code>import UIKit
import Charts
class ViewController: UIViewController {
var months:!
@IBOutlet var lineChartView: LineChartView!
override func viewDidLoad() {
super.viewDidLoad()
let unitsSold =
var months = ["Jan", "Feb", "Mar"]
let formato:LineChartFormatter = LineChartFormatter(labels: months)
let xaxis:XAxis = XAxis()
var dataEntries: = []
for i in 0..<unitsSold.count {
let dataEntry = ChartDataEntry(x: Double(i), y: unitsSold)
print("double \(Double(i))")
dataEntries.append(dataEntry)
}
xaxis.valueFormatter = formato
let data = LineChartData()
let dataset = LineChartDataSet(values: dataEntries, label: "Hello")
dataset.colors =
data.addDataSet(dataset)
self.lineChartView.gridBackgroundColor = NSUIColor.white
self.lineChartView.xAxis.drawGridLinesEnabled = true;
self.lineChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
self.lineChartView.xAxis.centerAxisLabelsEnabled = true
self.lineChartView.chartDescription?.text = "LineChartView Example"
self.lineChartView.xAxis.valueFormatter = xaxis.valueFormatter
self.lineChartView.data = data
}
override open func viewWillAppear(_ animated: Bool) {
self.lineChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
}
}
@objc(LineChartFormatter)
public class LineChartFormatter: NSObject, IAxisValueFormatter{
var labels: = []
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return labels
}
init(labels: ) {
super.init()
self.labels = labels
}
}
</code></pre>
<p> <a href="/image/lpgLym.png" rel="noreferrer noopener nofollow"><img src="/image/lpgLym.png" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,使用<code>粒度</code> 来避免重复值。
其次,如果您发现它们的值非常不同,请检查您的 <code>valueFormatter</code> 不要返回相同的字符串。例如<code>int(1.5)</code> 和 <code>int(1.9)</code> 会给你相同的 <code>Jan</code> 但你可能想让 <code>1.9</code> 返回<code>二月</code></p>
<p>同样,如果要处理小数,则必须仔细实现自己的 <code>valueFormatter</code></p></p>
<p style="font-size: 20px;">关于iOS swift3 Chart xaxis重复值问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/45665965/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/45665965/
</a>
</p>
页:
[1]