如何为不同的 xAxis 标签呈现自定义标签颜色?
How to render custom label colors for different xAxis labels?
我正在使用 Charts 库创建图表以在我的应用程序中绘制组合图表。
我画图成功了。现在我想更改特定标签的 xAxis
标签文本颜色。示例如图所示
如图所示,我想更改特定值的颜色,例如 03/06
和 06/06
。当我在 github 上引用库时,他们告诉我重写 drawValues()
函数来实现这一点。由于我是 swift 编程的新手,我不知道如何实现这一点。请指导我如何完成这项任务。提前致谢。
there is below method in XAxisRendererHorizontalBarChart.swift file of charts library. It will set the text for xAxis. You can customize it according to your use case.
open override func drawLabels(context: CGContext, pos: CGFloat, anchor: CGPoint)
在 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)
}
}
}
结果:
我正在使用 Charts 库创建图表以在我的应用程序中绘制组合图表。
我画图成功了。现在我想更改特定标签的 xAxis
标签文本颜色。示例如图所示
如图所示,我想更改特定值的颜色,例如 03/06
和 06/06
。当我在 github 上引用库时,他们告诉我重写 drawValues()
函数来实现这一点。由于我是 swift 编程的新手,我不知道如何实现这一点。请指导我如何完成这项任务。提前致谢。
there is below method in XAxisRendererHorizontalBarChart.swift file of charts library. It will set the text for xAxis. You can customize it according to your use case.
open override func drawLabels(context: CGContext, pos: CGFloat, anchor: CGPoint)
在 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)
}
}
}
结果: