danielgindi / iOSCharts库:水平条形图中的x轴标签

时间:2018-05-14 14:21:04

标签: ios swift charts ios-charts

Image with following Code with SetLabel Count

使用以下代码

barchart.xAxis.labelTextColor = UIColor.white

barchart.xAxis.labelPosition = .topInside

barChart.xAxis.setLabelCount(7, force: true)

问题:x轴网格被破坏 Image with following Code without setLabelCount

barchart.xAxis.labelTextColor = UIColor.white

barchart.xAxis.labelPosition = .topInside

问题:右侧标签隐藏

需要解决方案: 需要修理:

*网格应该对齐

*应该查看标签

我的代码是:

barChart.legend.enabled = false

barChart.xAxis.labelTextColor = UIColor.white
barChart.xAxis.labelPosition = .topInside

barChart.leftAxis.drawAxisLineEnabled = true
barChart.leftAxis.enabled = true
barChart.leftAxis.drawGridLinesEnabled = false
barChart.rightAxis.drawAxisLineEnabled = false

barChart.rightAxis.drawGridLinesEnabled = true
barChart.rightAxis.drawLabelsEnabled = true
barChart.rightAxis.labelFont =  NSUIFont.systemFont(ofSize: 13.0)
barChart.xAxis.labelFont = NSUIFont.systemFont(ofSize: 16.0)
barChart.rightAxis.labelTextColor =  UIColor.white

barChart.leftAxis.axisMinimum = 0.0
barChart.rightAxis.axisMinimum = 0.0
barChart.rightAxis.drawLabelsEnabled = false
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(x: Double(i),yValues: [values[i]])
        dataEntries.append(dataEntry)
    }
   let chartDataSet = BarChartDataSet(values: dataEntries, label: "HB2")
    let chartData = BarChartData(dataSet: chartDataSet)
    chartData.setValueFormatter(DefaultValueFormatter(formatter: formatter))
    barChart.xAxis.valueFormatter = IndexAxisValueFormatter(values:dataPoints)
    barChart.data = chartData

2 个答案:

答案 0 :(得分:0)

首先,标签位置应该是barchart.xAxis.labelPosition = .top,我不明白为什么网格没有对齐,但是如果使用下面的代码,它应该是正确对齐的,它对我有效。         chartView.legend.enabled = false

   // chartView.xAxis.labelTextColor = UIColor.white
    chartView.xAxis.labelPosition = .top

    chartView.leftAxis.drawAxisLineEnabled = true
    chartView.leftAxis.enabled = true
    chartView.leftAxis.drawGridLinesEnabled = false
    chartView.rightAxis.drawAxisLineEnabled = false

    chartView.rightAxis.drawGridLinesEnabled = true
    chartView.rightAxis.drawLabelsEnabled = true
    chartView.rightAxis.labelFont =  NSUIFont.systemFont(ofSize: 13.0)
    chartView.xAxis.labelFont = NSUIFont.systemFont(ofSize: 16.0)
    chartView.rightAxis.labelTextColor =  UIColor.white

    chartView.leftAxis.axisMinimum = 0.0
    chartView.rightAxis.axisMinimum = 0.0
    chartView.rightAxis.drawLabelsEnabled = false
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<unitsSold.count {
        let dataEntry = BarChartDataEntry(x: Double(i),yValues: [unitsSold[i]])
        dataEntries.append(dataEntry)
    }
    let chartDataSet = BarChartDataSet(values: dataEntries, label: "HB2")
    let chartData = BarChartData(dataSet: chartDataSet)
    chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
    chartView.data = chartData

    chartView.animate(yAxisDuration: 2.5)

我删除了你的第一个格式化程序,我不明白你为什么使用它,但它给了我下面的图表。

Horizontal bar chart

答案 1 :(得分:0)

根据我的经验,您需要在图表配置中添加 lableCount ,如下所示,以便计算正确的 xAxis 并在您的图表上显示

horiBarChartView.xAxis.labelCount = <yourXAxisArray>.count 

如果我们不在某个时间设置此计数,则无法在图表上生成正确的 xAxis 位置。

所以尝试一下,让我知道它的工作与否。