Swift Daniel Gindi图表库:仅为高亮显示的条形图绘制值标签

Swift Daniel Gindi图表库:仅为高亮显示的条形图绘制值标签,swift,ios-charts,Swift,Ios Charts,是否可以仅为高亮显示的条目绘制y值标签?像这样: drawValuesEnabled=true绘制所有值标签,它们重叠且难以阅读。谢谢您提出这个问题 已经在折线图中完成了这种类型的自定义,还可以显示 但不幸的是,这个问题已经解决了 在条形图中添加标签的步骤 1) 转到BarChartRenderer 2) 发现 3) 将下面的代码放入for循环中 // Define your label code here // Put more code for label if requi

是否可以仅为高亮显示的条目绘制y值标签?像这样:
drawValuesEnabled=true
绘制所有值标签,它们重叠且难以阅读。

谢谢您提出这个问题

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

但不幸的是,这个问题已经解决了

在条形图中添加标签的步骤

1) 转到
BarChartRenderer

2) 发现

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)
    }
internal func drawDataSet(上下文:CGContext,数据集:BarChartDataSet,索引:Int)
{
guard let dataProvider=dataProvider,barData=dataProvider.barData else{return}
CGContextSaveGState(上下文)
let trans=dataProvider.getTransformer(dataSet.axisDependency)
让牵引杆hadowEnabled:Bool=dataProvider.isDrawBarShadowEnabled
让dataSetOffset=(barData.dataSetCount-1)
让groupSpace=barData.groupSpace
设groupSpaceHalf=groupSpace/2.0
设barSpace=dataSet.barSpace
设barSpaceHalf=barSpace/2.0
让containsStacks=dataSet.isStacked
让isInverted=dataProvider.isInverted(dataSet.axisDependency)
var entries=dataSet.yVals as![BarChartDataEntry]
let barWidth:CGFloat=0.5
让phaseY=\u animator.phaseY
var barRect=CGRect()
var barShadow=CGRect()
变量y:双
//画画
对于(var j=0,count=Int(ceil(CGFloat(dataSet.entryCount)*\u animator.phaseX));j=0.0?CGFloat(y):0):(y 0)
{
顶部*=相位
}
其他的
{
底部*=相位
}
barRect.origin.x=左
barRect.size.width=右-左
barRect.origin.y=顶部
barRect.size.height=底部-顶部
trans.rectValueToPixel(&barRect)
if(!viewPortHandler.isInBoundsLeft(barRect.origin.x+barRect.size.width))
{
持续
}
如果(!viewPortHandler.isInBoundsRight(barRect.origin.x))
{
打破
}
//如果启用了绘制条形阴影
如果(牵引杆已启用)
{
barShadow.origin.x=barRect.origin.x
barShadow.origin.y=viewPortHandler.contentTop
barShadow.size.width=barRect.size.width
barShadow.size.height=viewPortHandler.contentHeight
CGContextSetFillColorWithColor(上下文,dataSet.barShadowColor.CGColor)
CGContextFillRect(上下文,barShadow)
}
//为当前绘制的值设置颜色。如果索引超出范围,请重新使用颜色。
设lbl=UILabel()
lbl.drawTextInRect(barRect)
CGContextSetFillColorWithColor(上下文,dataSet.colorAt(j).CGColor)
CGContextFillRect(上下文,barRect)
}
其他的
{
var posY=0.0
var negY=-e.negativeSum
var yStart=0.0
//如果启用了绘制条形阴影
如果(牵引杆已启用)
{
y=e.值
设left=x-条形宽度+条形半空间
设right=x+barWidth-barSpaceHalf
var top=isInverted?(y=0.0?CGFloat(y):0)
var bottom=isInverted?(y>=0.0?CGFloat(y):0):(y 0)
{
顶部*=相位
}
其他的
{
底部*=相位
}
barRect.origin.x=左
barRect.size.width=右-左
barRect.origin.y=顶部
barRect.size.height=底部-顶部
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(上下文,dataSet.barShadowColor.CGColor)
CGContextFillRect(上下文,barShadow)
}
//堆满
对于(变量k=0;k=0.0
{
y=posY
yStart=posY+value
posY=yStart
}
其他的
{
y=负
yStart=negY+abs(值)
negY+=abs(值)
}
设left=x-条形宽度+条形半空间
设右=x+ba
  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)
    }