我使用的图表框架的绘制图表。我需要在每个栏的左侧添加一些字符串。在我的代码中, 总是有两个小节,一个是收入,一个是支出。我想在每个酒吧旁边显示这些字符串。
在下面可以看到我的代码:
let ys1 = [model.totalIncome, abs(model.totalExpense)] var yse1 = ys1.enumerated().map { x, y -> BarChartDataEntry in return BarChartDataEntry(x: Double(x), y: y) } let count = 2 let data = BarChartData() let ds1 = BarChartDataSet(values: yse1, label: "") ds1.colors = [UIColor.red, UIColor.blue] // Number formatting of Values ds1.valueFormatter = YAxisValueFormatter() ds1.valueFont = UIFont(fontType: .H6)! ds1.valueTextColor = UIColor.dark_text // Data set data.addDataSet(ds1) horizontalBarChartView.data = data // General setting horizontalBarChartView.xAxis.drawLabelsEnabled = false horizontalBarChartView.setScaleEnabled(false) // Grid horizontalBarChartView.xAxis.drawGridLinesEnabled = false horizontalBarChartView.leftAxis.gridLineDashLengths = [15.0, 15.0] horizontalBarChartView.leftAxis.gridColor = UIColor.palette_28 horizontalBarChartView.rightAxis.drawGridLinesEnabled = false // Disable number formatting of leftAxis and rightAxis horizontalBarChartView.leftAxis.enabled = false horizontalBarChartView.rightAxis.enabled = false horizontalBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["Income","Expense"]) horizontalBarChartView.xAxis.granularityEnabled = true horizontalBarChartView.xAxis.granularity = 1 // setViewPortOffsets let digitCount = Int(data.yMax).digitCount let leftOffcet: CGFloat = digitCount > 2 ? CGFloat((digitCount - 1) * 10) : 10.0 horizontalBarChartView.setViewPortOffsets(left: leftOffcet, top: 0, right: 0, bottom: 50) horizontalBarChartView.leftAxis.axisMinimum = 0
And my view:
lazy var horizontalBarChartView: HorizontalBarChartView = { let chartView = HorizontalBarChartView() chartView.contentMode = .scaleAspectFit chartView.drawBordersEnabled = false chartView.drawGridBackgroundEnabled = false chartView.gridBackgroundColor = UIColor.clear chartView.legend.enabled = false chartView.chartDescription = nil chartView.highlighter = nil chartView.clipsToBounds = true chartView.translatesAutoresizingMaskIntoConstraints = false return chartView }()
I guess below code should add these labels, however, it is not working
horizontalBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["Income","Expense"]) horizontalBarChartView.xAxis.granularityEnabled = true horizontalBarChartView.xAxis.granularity = 1
Update
See orange rectangle. I need these labels.
图表框架是绘制图表的绝佳工具。您应该 完美地学习它。我在项目中使用了旧代码(复制/粘贴),因此 在图表中绘制xAxis标签时出现了一些问题。
只需在行下方评论:
horizontalBarChartView.xAxis.drawLabelsEnabled = false