gpt4 book ai didi

swift - Daniel Gindi 图表库 : Draw the value label only for the highlighted bar

转载 作者:行者123 更新时间:2023-11-28 08:25:29 25 4
gpt4 key购买 nike

是否可以只为突出显示的条目绘制 y 值标签?像这样: Show single value labeldrawValuesEnabled = true 绘制所有值标签,它们重叠且难以阅读。

最佳答案

感谢您提出这个问题。

已经在折线图中完成了此类自定义,您还可以显示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)
}

我不确定代码,但你需要在这里做一些事情。如果我的答案需要任何更改,请迟到。

快乐编码...

关于swift - Daniel Gindi 图表库 : Draw the value label only for the highlighted bar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40155704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com