我正在我的项目中实现水平条形图的ios-charts。我需要将单个条的值显示在条形图上方(因为图表是水平的,标签应显示在条形图的右侧)。但是,我无法使其发挥作用。代码如下:
func setupHorizontalChartView() {
self.chartView.chartDescription?.enabled = false
chartView.dragEnabled = false
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = false
let chartXAxis = chartView.xAxis
chartXAxis.labelPosition = .bottom
let chartLeftAxis = chartView.leftAxis
chartLeftAxis.labelFont = NSUIFont(name: "Helvetica", size: 12)!
chartLeftAxis.labelTextColor = UIColor.darkGray
chartView.rightAxis.enabled = false
chartView.drawValueAboveBarEnabled = true
let formatter = BarChartFormatter()
formatter.setValues(values: self.months)
chartView.xAxis.valueFormatter = formatter
chartView.xAxis.setLabelCount(self.months.count - 1, force: false)
chartView.xAxis.drawGridLinesEnabled = false
chartView.leftAxis.drawGridLinesEnabled = false
chartView.xAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawAxisLineEnabled = false
chartView.leftAxis.drawLabelsEnabled = false
}
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
chartDataSet.drawValuesEnabled = true
let chartData = BarChartData(dataSet: chartDataSet)
chartData.setDrawValues(true)
chartView.data = chartData
}
对于chartView, drawValueAboveBarEnabled
已设置为true。当我为BarChartView而不是HorizontalBarChartView使用相同的代码时,条形图的值会显示在条形图上方。但是,它不适用于水平条形图视图。