ios - 如何将字符串标签(xAxis 标签)添加到图表框架中的水平条

标签 ios swift charts

我正在使用 charts framework用于绘制图表。我需要在每个小节的左侧添加一些字符串。在我的代码中总是有两个栏,一个用于收入,一个用于支出。我想在每个小节旁边显示这些字符串。

Horizontal bar

下面你可以看到我的代码:

            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

我的观点:

   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
    }()

我想下面的代码应该添加这些标签,但是,它不起作用

    horizontalBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["Income","Expense"])
    horizontalBarChartView.xAxis.granularityEnabled = true
    horizontalBarChartView.xAxis.granularity = 1

更新

见橙色矩形。我需要这些标签。

enter image description here

最佳答案

我正在为此使用扩展

extension BarChartView {

    private class BarChartFormatter: NSObject, IAxisValueFormatter {

        var labels: [String] = []

        func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            return labels[Int(value)]
        }

        init(labels: [String]) {
            super.init()
            self.labels = labels
        }
    }

    func setBarChartData(xValues: [String], yValues: [Double], label: String) {

        var dataEntries: [BarChartDataEntry] = []

        for i in 0..<yValues.count {
            let dataEntry = BarChartDataEntry(x: Double(i), y: yValues[i])
            dataEntries.append(dataEntry)
        }

        let chartDataSet = BarChartDataSet(values: dataEntries, label: label)
        let chartData = BarChartData(dataSet: chartDataSet)

        let chartFormatter = BarChartFormatter(labels: xValues)
        let xAxis = XAxis()
        xAxis.valueFormatter = chartFormatter
        self.xAxis.valueFormatter = xAxis.valueFormatter

        self.data = chartData
    }
}

你可以像这样在代码中使用它

chartView.setBarChartData(xValues: xEntries, yValues: yEntries, label: "Income")

在哪里,

//You need to add values dynamically
var yEntries: [Double] = [1000, 200, 500]
var xEntries: [String] = ["Income1", "Income2", "Income3"]

希望这能奏效。

关于ios - 如何将字符串标签(xAxis 标签)添加到图表框架中的水平条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46618636/

相关文章:

javascript - 我如何更改 highcharts highstock 中输入的位置?

ios - 如何使用 Swift iOS 向 UIAlertView 按钮添加操作

ios - 在 Swift 中向下转换 NSObject

ios - Xcode Swift 将 TextField 内容传递到网站 textField

swift - 计时器不会停止在 segue 上

java - Java 中的 3d 折线图

javascript - 爆炸谷歌饼图中的 'other' 切片

ios - iOS自定义键盘扩展功能

ios - navigationBar出现在statusBar下面

macos - 使用 Swift 的套接字服务器示例