我正在使用Daniel的IOS Chart框架和Xcode7.3.1。我完成了基础教程并尝试设置第一个示例。
问题是我无法分配X轴字符串标签
我在教程中看到,当我们将数据分配给图表时,我们应该使用
let chartData = BarChartData(xVals: dataPoints, dataSet: chartDataSet)
完成最后一步,但下面是我在这里看到的。
没有参数作为“xVals”,但只有“数据集”可用。
答案 0 :(得分:3)
好的,这里是折线图的示例函数:
func setLineChart(dataPoints: [Double], chartInfo: String, chartDescription: String) {
let formatter:ChartFormatter = ChartFormatter()
let xaxis:XAxis = XAxis()
var dataEntries: [ChartDataEntry] = Array()
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: dataPoints[i])
dataEntries.append(dataEntry)
formatter.stringForValue(Double(i), axis: xaxis)
}
let lineChartDataSet = LineChartDataSet(values: dataEntries, label: chartInfo)
let lineChartData = LineChartData(dataSet: lineChartDataSet)
xaxis.valueFormatter = formatter
lineChartView.xAxis.valueFormatter = xaxis.valueFormatter
lineChartDataSet.mode = .CubicBezier
lineChartDataSet.drawCirclesEnabled = false
lineChartView.descriptionText = chartDescription
lineChartView.data = lineChartData
}
这是格式化程序:
@objc(ChartFormatter)
public class ChartFormatter: NSObject, IAxisValueFormatter{
var days: [String] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
public func stringForValue(value: Double, axis: AxisBase?) -> String{
return days[Int(value)]
}
}
最后是功能调用:
override func viewDidLoad() {
super.viewDidLoad()
let values: [Double] = [1.0, 4.0, 1.5, 1.4, 1.3, 1.0, 3.0]
let chartInfo = "chart info"
let chartDescription = "chart description"
self.setLineChart(values, chartInfo: chartInfo, chartDescription: chartDescription
}
所以最后你会得到这样的东西:
答案 1 :(得分:1)
好像你正在使用旧教程!因为swift 3即将到来,这些图表的开发人员改变了库..根据它们,我们将不得不使用特殊的格式化程序来设置例如值类型为X轴的字符串。所以现在你的方法只接受一个参数而不是两个:
let barChartData = BarChartData(dataSet: chartDataSet)
let lineChartData = LineChartData(dataSet: lineChartDataSet)
您可以在此处找到更多信息: https://github.com/danielgindi/Charts/issues/1340