我正在使用图表显示条形图,但是我无法使x轴显示为星期几,而不是0、1、2、3等。
初始化图表:
var weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
func initializeChart() {
chartView.noDataText = "No Data"
chartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
chartView.xAxis.labelPosition = .bottom
chartView.chartDescription.text = "Revenue"
//chartView.xAxis.setLabelsToSkip(0)
chartView.legend.enabled = false
chartView.scaleYEnabled = false
chartView.scaleXEnabled = false
chartView.pinchZoomEnabled = false
chartView.doubleTapToZoomEnabled = false
chartView.leftAxis.axisMinimum = 0.0
chartView.leftAxis.axisMaximum = 100.00
chartView.highlighter = nil
chartView.rightAxis.enabled = false
chartView.xAxis.drawGridLinesEnabled = false
}
这是我正在填充的数据:
if json != nil {
let week = json["week"]
let month = json["month"]
print("Check", week, month)
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.weekdays.count {
let day = self.weekdays[i]
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [(week[day].double)!])
dataEntries.append(dataEntry)
print("data entry", dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Weekdays")
chartDataSet.colors = ChartColorTemplates.material()
let chartData = BarChartData(dataSet: chartDataSet)
self.chartView.data = chartData
}
我只想在x轴上打印星期几。你能帮忙吗?
答案 0 :(得分:1)
您需要为X轴设置valueFormatter,请尝试以下代码。希望对您有帮助!
if json != nil {
let week = json["week"]
let month = json["month"]
print("Check", week, month)
var dataEntries: [BarChartDataEntry] = []
for i in 0..<self.weekdays.count {
let day = self.weekdays[i]
let dataEntry = BarChartDataEntry(x: Double(i), yValues:
[(week[day].double)!] , data: weekdays as AnyObject?))
dataEntries.append(dataEntry)
print("data entry", dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Weekdays")
chartDataSet.colors = ChartColorTemplates.material()
let chartData = BarChartData(dataSet: chartDataSet)
let xAxisValue = chartView.xAxis
xAxisValue.valueFormatter = axisFormatDelegate
self.chartView.data = chartData
}
extension DemoBaseViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return weekdays[Int(value)]
}
}