更改组合图表中的 yValue 标签垂直间距(ios-图表)

Change yValue label vertical spacing in combined chart (ios-charts)

我有一个组合图表,它使用折线图和条形图来显示 y 值。 在某些情况下,折线图和条形图的值会重叠,有没有办法设置 yValues 标签的垂直间距,使它们不会彼此重叠(如图中的一月到十月)?

Combined Chart Image

我正在使用图表框架(以前称为 ios-图表),这里是设置 CombineChartView 的代码:

        let xValues = getXAxisLabelsForYear(year)
        let runningTotalsByMonth = getRunningTotalByMonthForYear(year)!
        var yValsBar = [BarChartDataEntry]()
        var yValsLine = [ChartDataEntry]()

        for i in 0 ..< xValues.count {
            let yBarDataEntry = BarChartDataEntry(value: monthlyWinnings[i], xIndex: i)
            yValsBar.append(yBarDataEntry)
            let yLineDataEntry = ChartDataEntry(value: runningTotalsByMonth[i], xIndex: i)
            yValsLine.append(yLineDataEntry)
        }
        let barChartDataSet = BarChartDataSet(yVals: yValsBar, label: "Monthly Winnings")

        //setup bar chart
        var barChartColors = [UIColor]()
        for i in monthlyWinnings {
            if i >= 0.0 {
                barChartColors.append(myGreen)
            } else {
                barChartColors.append(UIColor.redColor())
            }
        }
        barChartDataSet.colors = barChartColors
        barChartDataSet.barShadowColor = UIColor.clearColor()
        barChartDataSet.valueFont = UIFont.systemFontOfSize(10.0)

        //setup line chart
        let lineChartDataSet = LineChartDataSet(yVals: yValsLine, label: "Cumulative Winnings")
        var lineChartColors = [UIColor]()
        for i in runningTotalsByMonth {
            if i >= 0.0 {
                lineChartColors.append(myGreen)
            } else {
                lineChartColors.append(UIColor.redColor())
            }
        }
        lineChartDataSet.colors = lineChartColors
        lineChartDataSet.circleColors = [UIColor.blueColor()]
        lineChartDataSet.drawCircleHoleEnabled = false
        lineChartDataSet.circleRadius = 5
        lineChartDataSet.lineWidth = 2
        lineChartDataSet.valueFont = UIFont.systemFontOfSize(10.0)

        //combine data
        let data = CombinedChartData(xVals: xValues)
        data.barData = BarChartData(xVals: xValues, dataSet: barChartDataSet)
        data.lineData = LineChartData(xVals: xValues, dataSet: lineChartDataSet)

        combinedChartView.data = data

        //format the chart
        combinedChartView.xAxis.setLabelsToSkip(0)
        combinedChartView.xAxis.labelPosition = .Bottom
        combinedChartView.descriptionText = ""
        combinedChartView.rightAxis.drawLabelsEnabled = false
        combinedChartView.rightAxis.drawGridLinesEnabled = false
        combinedChartView.drawGridBackgroundEnabled = false
        combinedChartView.leftAxis.drawZeroLineEnabled = true
        combinedChartView.xAxis.drawGridLinesEnabled = false
        combinedChartView.xAxis.wordWrapEnabled = true

请参阅我上面的评论,但如果您不使用自动布局,这样的方法可能会起作用:

let labelA = UILabel()
let labelB = UILabel()

let padding: CGFloat = 5.0 // or whatever

if CGRectIntersectsRect(labelA.frame, labelB.frame) {
    // If the minY of labelA is <= labelB's that means labelA is ABOVE labelB
    if labelA.frame.minY <= labelB.frame.minY {
        // Set it above, with some (optional) padding
        labelA.frame.origin.y = labelB.frame.origin.y - padding - labelA.frame.height
    } else {
        labelB.frame.origin.y = labelA.frame.origin.y - padding - labelB.frame.height
    }
}

当然,您需要额外的代码来检查它是否过高以及其他边缘情况。

您可以使用

在条形图顶部下方绘制条形图值
chartView.drawValueAboveBarEnabled = false

并设置一些颜色

barChartDataSet.valueTextColor = UIColor.someColor()

看起来像这样: