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

时间:2016-04-28 20:11:43

标签: swift ios-charts

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

Combined Chart Image

我使用的是Charts框架(以前称为ios-charts),下面是设置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

2 个答案:

答案 0 :(得分:0)

请参阅上面的评论,但是如果你没有使用自动布局,这样的话可能会有效:

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

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

答案 1 :(得分:0)

您可以使用

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

并设置一些颜色

barChartDataSet.valueTextColor = UIColor.someColor()

看起来像这样: Image