如何在图表框架中向水平条添加字符串标签(xAxis 标签) [英] How to add string labels( xAxis labels) to horizontal bar in Charts framework

查看:13
本文介绍了如何在图表框架中向水平条添加字符串标签(xAxis 标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

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

 let ys1 = [model.totalIncome, abs(model.totalExpense)]var yse1 = ys1.enumerated().map { x, y ->BarChartDataEntry 中返回 BarChartDataEntry(x: Double(x), y: y)}让计数 = 2让数据 = BarChartData()让 ds1 = BarChartDataSet(values: yse1, label: "")ds1.colors = [UIColor.red, UIColor.blue]//数值的数字格式ds1.valueFormatter = YAxisValueFormatter()ds1.valueFont = UIFont(fontType: .H6)!ds1.valueTextColor = UIColor.dark_text//数据集data.addDataSet(ds1)horizo​​ntalBarChartView.data = 数据//通用设置horizo​​ntalBarChartView.xAxis.drawLabelsEnabled = falsehorizo​​ntalBarChartView.setScaleEnabled(false)//网格horizo​​ntalBarChartView.xAxis.drawGridLinesEnabled = falsehorizo​​ntalBarChartView.leftAxis.gridLineDashLengths = [15.0, 15.0]horizo​​ntalBarChartView.leftAxis.gridColor = UIColor.palette_28horizo​​ntalBarChartView.rightAxis.drawGridLinesEnabled = false//禁用leftAxis和rightAxis的数字格式horizo​​ntalBarChartView.leftAxis.enabled = falsehorizo​​ntalBarChartView.rightAxis.enabled = falsehorizo​​ntalBarChartView.xAxis.valueFormatter =IndexAxisValueFormatter(值:[收入",费用"])horizo​​ntalBarChartView.xAxis.granularityEnabled = true水平BarChartView.xAxis.granularity = 1//setViewPortOffsets让 digitCount = Int(data.yMax).digitCount让 leftOffcet: CGFloat = digitCount >2 ?CGFloat((digitCount - 1) * 10) : 10.0horizo​​ntalBarChartView.setViewPortOffsets(左:leftOffcet,顶部:0,右侧:0,底部:50)水平BarChartView.leftAxis.axisMinimum = 0

我的观点:

 懒惰 var horizo​​ntalBarChartView: Horizo​​ntalBarChartView = {让图表视图 = Horizo​​ntalBarChartView()chartView.contentMode = .scaleAspectFitchartView.drawBordersEnabled = falsechartView.drawGridBackgroundEnabled = falsechartView.gridBackgroundColor = UIColor.clearchartView.legend.enabled = falsechartView.chartDescription = nilchartView.highlighter = nilchartView.clipsToBounds = truechartView.translatesAutoresizingMaskIntoConstraints = false返回图表视图}()

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

 horizo​​ntalBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["Income","Expense"])horizo​​ntalBarChartView.xAxis.granularityEnabled = true水平BarChartView.xAxis.granularity = 1

更新

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

解决方案

Charts 框架是绘制图表的绝佳工具.你应该好好研究它.我在我们的项目中使用了旧代码(复制/粘贴),因此在我的图表中绘制 xAxis 标签时出现了一些问题.

请在下方评论:

 horizo​​ntalBarChartView.xAxis.drawLabelsEnabled = false

I am using charts framework for drawing charts. I need to add some strings in left of every bars. In my code always there are two bars, one for Income and one for Expense. I want to show these string aside of every bar.

Below you can see my codes:

            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.

解决方案

Charts framework is a great tool for drawing charts. You should study it perfectly. I used an old code in our project (copy/paste), so it caused some problems for drawing xAxis labels in my chart.

Just comment below line:

    horizontalBarChartView.xAxis.drawLabelsEnabled = false

这篇关于如何在图表框架中向水平条添加字符串标签(xAxis 标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆