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

时间:2022-12-04 09:58:04

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

with the following code

使用以下代码

barchart.xAxis.labelTextColor = UIColor.white

barchart.xAxis.labelPosition = .topInside

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

Issue: x axis grid get destructed danielgindi / iOSCharts库:水平条形图中的x轴标签

问题:x轴网格被破坏

barchart.xAxis.labelTextColor = UIColor.white

barchart.xAxis.labelPosition = .topInside

Issue: Right side Label gets hidden

问题:右侧标签被隐藏

Solution Required: Need Following to be fixed:

需要解决方案:需要修复以下内容:

*Grid should be aligned

*网格应该对齐

*Label should be viewed

*应该查看标签

my code is :

我的代码是:

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 个解决方案

#1


0  

First of all label position should be barchart.xAxis.labelPosition = .top and I do not understand why grids are not aligned but if you use following code it should be right aligned it worked for me. chartView.legend.enabled = false

首先,标签位置应该是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)

I have removed your first formatter I do not understand why you used it but it gave me following chart.

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

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

#2


0  

as per my exprience you need to add lableCount in your chart configuration like below so that it will calculate proper xAxis and show it on your chart

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

horiBarChartView.xAxis.labelCount = <yourXAxisArray>.count 

If we are not setting this count some time it will not able to generate proper xAxis position on chart.

如果我们没有将此计数设置一段时间,它将无法在图表上生成正确的xAxis位置。

So try this once and let me know its working or not.

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

#1


0  

First of all label position should be barchart.xAxis.labelPosition = .top and I do not understand why grids are not aligned but if you use following code it should be right aligned it worked for me. chartView.legend.enabled = false

首先,标签位置应该是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)

I have removed your first formatter I do not understand why you used it but it gave me following chart.

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

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

#2


0  

as per my exprience you need to add lableCount in your chart configuration like below so that it will calculate proper xAxis and show it on your chart

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

horiBarChartView.xAxis.labelCount = <yourXAxisArray>.count 

If we are not setting this count some time it will not able to generate proper xAxis position on chart.

如果我们没有将此计数设置一段时间,它将无法在图表上生成正确的xAxis位置。

So try this once and let me know its working or not.

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