我使用Charts庫創建圖表,在我的應用程序中繪製組合圖表。如何呈現不同xAxis標籤的自定義標籤顏色?
我成功繪製了圖形。現在我想更改xAxis
特定標籤的標籤文字顏色。圖中顯示了這個例子。
如圖所示,我想改變特定值的顏色,例如03/06
& 06/06
。當我在github上提到這個庫時,他們告訴我重寫drawValues()
函數來實現這個功能。由於我是新手快速編程,我不知道如何實現這一點。請指導我如何完成這項任務。提前致謝。
我使用Charts庫創建圖表,在我的應用程序中繪製組合圖表。如何呈現不同xAxis標籤的自定義標籤顏色?
我成功繪製了圖形。現在我想更改xAxis
特定標籤的標籤文字顏色。圖中顯示了這個例子。
如圖所示,我想改變特定值的顏色,例如03/06
& 06/06
。當我在github上提到這個庫時,他們告訴我重寫drawValues()
函數來實現這個功能。由於我是新手快速編程,我不知道如何實現這一點。請指導我如何完成這項任務。提前致謝。
的XAxisRenderer.swift(不是XAxisRendererHorizontalBarChart像其他答案建議)裏面添加這個列表與你的顏色以上的drawLabels功能:
let labelTextColorList = [UIColor.black, UIColor.red, UIColor.blue, UIColor.brown, UIColor.cyan, UIColor.black, UIColor.black, UIColor.black, UIColor.black, UIColor.black, UIColor.black, UIColor.black]
然後用此代碼替換drawLabels功能:
/// draws the x-labels on the specified y-position
open func drawLabels(context: CGContext, pos: CGFloat, anchor: CGPoint)
{
guard
let xAxis = self.axis as? XAxis,
let viewPortHandler = self.viewPortHandler,
let transformer = self.transformer
else { return }
#if os(OSX)
let paraStyle = NSParagraphStyle.default().mutableCopy() as! NSMutableParagraphStyle
#else
let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
#endif
paraStyle.alignment = .center
//label attrs moved from here
/* let labelAttrs = [NSFontAttributeName: xAxis.labelFont,
NSForegroundColorAttributeName: xAxis.labelTextColor,
NSParagraphStyleAttributeName: paraStyle] as [String : NSObject]
*/
let labelRotationAngleRadians = xAxis.labelRotationAngle * ChartUtils.Math.FDEG2RAD
let centeringEnabled = xAxis.isCenterAxisLabelsEnabled
let valueToPixelMatrix = transformer.valueToPixelMatrix
var position = CGPoint(x: 0.0, y: 0.0)
var labelMaxSize = CGSize()
if xAxis.isWordWrapEnabled
{
labelMaxSize.width = xAxis.wordWrapWidthPercent * valueToPixelMatrix.a
}
let entries = xAxis.entries
for i in stride(from: 0, to: entries.count, by: 1)
{
//label attrs moved to here
let labelAttrs: [String: NSObject]!
if(i<labelTextColorList.count) {
labelAttrs = [NSFontAttributeName: xAxis.labelFont,
NSForegroundColorAttributeName: labelTextColorList[i],
NSParagraphStyleAttributeName: paraStyle] as [String : NSObject]
}
else {
labelAttrs = [NSFontAttributeName: xAxis.labelFont,
NSForegroundColorAttributeName: xAxis.labelTextColor,
NSParagraphStyleAttributeName: paraStyle] as [String : NSObject]
}
if centeringEnabled
{
position.x = CGFloat(xAxis.centeredEntries[i])
}
else
{
position.x = CGFloat(entries[i])
}
position.y = 0.0
position = position.applying(valueToPixelMatrix)
if viewPortHandler.isInBoundsX(position.x)
{
let label = xAxis.valueFormatter?.stringForValue(xAxis.entries[i], axis: xAxis) ?? ""
let labelns = label as NSString
if xAxis.isAvoidFirstLastClippingEnabled
{
// avoid clipping of the last
if i == xAxis.entryCount - 1 && xAxis.entryCount > 1
{
let width = labelns.boundingRect(with: labelMaxSize, options: .usesLineFragmentOrigin, attributes: labelAttrs, context: nil).size.width
if width > viewPortHandler.offsetRight * 2.0
&& position.x + width > viewPortHandler.chartWidth
{
position.x -= width/2.0
}
}
else if i == 0
{ // avoid clipping of the first
let width = labelns.boundingRect(with: labelMaxSize, options: .usesLineFragmentOrigin, attributes: labelAttrs, context: nil).size.width
position.x += width/2.0
}
}
drawLabel(context: context,
formattedLabel: label,
x: position.x,
y: pos,
attributes: labelAttrs,
constrainedToSize: labelMaxSize,
anchor: anchor,
angleRadians: labelRotationAngleRadians)
}
}
}
圖表庫的XAxisRendererHorizontalBarChart.swift文件中有以下方法。它將設置xAxis的文本。你可以根據你的用例來定製它。
open override func drawLabels(context: CGContext, pos: CGFloat, anchor: CGPoint)
你能給我一個如何做到這一點的例子嗎? –