Daniel Gindi Charts Library: Draw the value label only for the highlighted bar

时间:2016-10-20 13:22:32

标签: swift ios-charts

Is it possible to draw the y-value label only for the highlighted entry? Like so: Show single value label drawValuesEnabled = true draws all value labels and they are overlapping and hard to read.

1 个答案:

答案 0 :(得分:0)

感谢您提出这个问题。

在折线图中已经完成了这种类型的自定义,您还可以显示here

但不幸的是,这个问题已经结束。

在条形图中添加标签

1)转到BarChartRenderer

2)查找

internal func drawDataSet(context context: CGContext, dataSet: BarChartDataSet, index: Int)

3)将下面的代码放在for循环

    // Define your label code here 
    // Put more code for label if require...
    let lbl = UILabel()
    lbl.text = "\(e.values)"
    lbl.drawTextInRect(barRect)

4)您还可以在

下面显示整个功能
  internal func drawDataSet(context context: CGContext, dataSet: BarChartDataSet, index: Int)
    {
        guard let dataProvider = dataProvider, barData = dataProvider.barData else { return }

        CGContextSaveGState(context)

        let trans = dataProvider.getTransformer(dataSet.axisDependency)

        let drawBarShadowEnabled: Bool = dataProvider.isDrawBarShadowEnabled
        let dataSetOffset = (barData.dataSetCount - 1)
        let groupSpace = barData.groupSpace
        let groupSpaceHalf = groupSpace / 2.0
        let barSpace = dataSet.barSpace
        let barSpaceHalf = barSpace / 2.0
        let containsStacks = dataSet.isStacked
        let isInverted = dataProvider.isInverted(dataSet.axisDependency)
        var entries = dataSet.yVals as! [BarChartDataEntry]
        let barWidth: CGFloat = 0.5
        let phaseY = _animator.phaseY
        var barRect = CGRect()
        var barShadow = CGRect()
        var y: Double

        // do the drawing
        for (var j = 0, count = Int(ceil(CGFloat(dataSet.entryCount) * _animator.phaseX)); j < count; j++)
        {
            let e = entries[j]

            // calculate the x-position, depending on datasetcount
            let x = CGFloat(e.xIndex + e.xIndex * dataSetOffset) + CGFloat(index)
                + groupSpace * CGFloat(e.xIndex) + groupSpaceHalf
            var vals = e.values

            if (!containsStacks || vals == nil)
            {
                y = e.value

                let left = x - barWidth + barSpaceHalf
                let right = x + barWidth - barSpaceHalf
                var top = isInverted ? (y <= 0.0 ? CGFloat(y) : 0) : (y >= 0.0 ? CGFloat(y) : 0)
                var bottom = isInverted ? (y >= 0.0 ? CGFloat(y) : 0) : (y <= 0.0 ? CGFloat(y) : 0)

                // multiply the height of the rect with the phase
                if (top > 0)
                {
                    top *= phaseY
                }
                else
                {
                    bottom *= phaseY
                }

                barRect.origin.x = left
                barRect.size.width = right - left
                barRect.origin.y = top
                barRect.size.height = bottom - top

                trans.rectValueToPixel(&barRect)

                if (!viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width))
                {
                    continue
                }

                if (!viewPortHandler.isInBoundsRight(barRect.origin.x))
                {
                    break
                }

                // if drawing the bar shadow is enabled
                if (drawBarShadowEnabled)
                {
                    barShadow.origin.x = barRect.origin.x
                    barShadow.origin.y = viewPortHandler.contentTop
                    barShadow.size.width = barRect.size.width
                    barShadow.size.height = viewPortHandler.contentHeight

                    CGContextSetFillColorWithColor(context, dataSet.barShadowColor.CGColor)
                    CGContextFillRect(context, barShadow)
                }

                // Set the color for the currently drawn value. If the index is out of bounds, reuse colors.

                let lbl = UILabel()
                lbl.drawTextInRect(barRect)

                CGContextSetFillColorWithColor(context, dataSet.colorAt(j).CGColor)
                CGContextFillRect(context, barRect)
            }
            else
            {
                var posY = 0.0
                var negY = -e.negativeSum
                var yStart = 0.0

                // if drawing the bar shadow is enabled
                if (drawBarShadowEnabled)
                {
                    y = e.value

                    let left = x - barWidth + barSpaceHalf
                    let right = x + barWidth - barSpaceHalf
                    var top = isInverted ? (y <= 0.0 ? CGFloat(y) : 0) : (y >= 0.0 ? CGFloat(y) : 0)
                    var bottom = isInverted ? (y >= 0.0 ? CGFloat(y) : 0) : (y <= 0.0 ? CGFloat(y) : 0)

                    // multiply the height of the rect with the phase
                    if (top > 0)
                    {
                        top *= phaseY
                    }
                    else
                    {
                        bottom *= phaseY
                    }

                    barRect.origin.x = left
                    barRect.size.width = right - left
                    barRect.origin.y = top
                    barRect.size.height = bottom - top

                    trans.rectValueToPixel(&barRect)

                    barShadow.origin.x = barRect.origin.x
                    barShadow.origin.y = viewPortHandler.contentTop
                    barShadow.size.width = barRect.size.width
                    barShadow.size.height = viewPortHandler.contentHeight

                    CGContextSetFillColorWithColor(context, dataSet.barShadowColor.CGColor)
                    CGContextFillRect(context, barShadow)
                }

                // fill the stack
                for (var k = 0; k < vals!.count; k++)
                {
                    let value = vals![k]

                    if value >= 0.0
                    {
                        y = posY
                        yStart = posY + value
                        posY = yStart
                    }
                    else
                    {
                        y = negY
                        yStart = negY + abs(value)
                        negY += abs(value)
                    }

                    let left = x - barWidth + barSpaceHalf
                    let right = x + barWidth - barSpaceHalf
                    var top: CGFloat, bottom: CGFloat
                    if isInverted
                    {
                        bottom = y >= yStart ? CGFloat(y) : CGFloat(yStart)
                        top = y <= yStart ? CGFloat(y) : CGFloat(yStart)
                    }
                    else
                    {
                        top = y >= yStart ? CGFloat(y) : CGFloat(yStart)
                        bottom = y <= yStart ? CGFloat(y) : CGFloat(yStart)
                    }

                    // multiply the height of the rect with the phase
                    top *= phaseY
                    bottom *= phaseY

                    barRect.origin.x = left
                    barRect.size.width = right - left
                    barRect.origin.y = top
                    barRect.size.height = bottom - top

                    trans.rectValueToPixel(&barRect)

                    if (k == 0 && !viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width))
                    {
                        // Skip to next bar
                        break
                    }

                    // avoid drawing outofbounds values
                    if (!viewPortHandler.isInBoundsRight(barRect.origin.x))
                    {
                        break
                    }

                    // Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
                    CGContextSetFillColorWithColor(context, dataSet.colorAt(k).CGColor)
                    CGContextFillRect(context, barRect)
                }
            }
            // Define your label code here
            let lbl = UILabel()
            lbl.text = "\(e.values)"
            lbl.drawTextInRect(barRect)
        }


        CGContextRestoreGState(context)
    }

我不确定代码,但你需要在这里做点什么。如果我的答案需要改变,我会迟到。

快乐的编码......