我正在使用Charts库创建一个图表,以便在我的应用程序中绘制组合图表。图形绘制成功,现在我想更改特定标签的xAxis标签文本颜色,示例如pic.所示如图所示,我想改变特定值的颜色,例如03/06和06/06。当我引用github上的库时,他们告诉我重写drawValues()函数来实现这一点。由于我是swift编程的新手,我不知道如何实现这一点。请指导我如何实现这一任务。提前感谢。
xAxis
03/06
06/06
drawValues()
oyjwcjzk1#
在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) } } }
结果:
uqdfh47h2#
在图表库的XAxisRendererHorizontalBarChart.swift文件中有一个方法。2它将设置x轴的文本。3你可以根据你的使用情况定制它。
open override func drawLabels(context: CGContext, pos: CGFloat, anchor: CGPoint)
vlju58qv3#
我是这样做的:先计算xAxis的宽度,然后求出标注之间的距离,再在标注之间相加
let dateAr = ["3개월 전","2개월 전","1개월 전","현재","1개월 후"] // array,i want to add let xAixsLabelView = UIView() // view, it will contain labels beneath let xAxisLabelList = [UILabel(),UILabel(),UILabel(),UILabel(),UILabel()] // array, I will add ..... func drawXLabel(){ let color1 = UIColor(red: 0.875, green: 0.875, blue: 0.875, alpha: 1) let color2 = BaseViewController().hexStringToUIColor(hex: "#3246FA") xAixsLabelView.backgroundColor = .clear xAixsLabelView.frame = CGRect(x: lineChartView.frame.minX + 13, y: lineChartView.frame.maxY - 30, width: lineChartView.frame.width , height: 50) let chartViewWidth = lineChartView.frame.width let labelWidth = lineChartView.xAxis.labelWidth let paddingWidth = CGFloat((chartViewWidth - (labelWidth * 5))/6) // 5 is the number of labels, and 6 is the number of padding that wraps labels( you need to add 1 to the number of labels for i in 0...4{ var padding = paddingWidth * CGFloat(Double(i) + 0.95) + labelWidth * CGFloat(i) if i == 0 { xAxisLabelList[i].text = "3개월 전" xAxisLabelList[i].textColor = color1 padding = paddingWidth * 1 }else if i == 1{ xAxisLabelList[i].text = "2개월 전" xAxisLabelList[i].textColor = color1 }else if i == 2{ xAxisLabelList[i].text = "1개월 전" xAxisLabelList[i].textColor = color1 }else if i == 3{ xAxisLabelList[i].text = "현재" xAxisLabelList[i].textColor = color2 }else if i == 4{ xAxisLabelList[i].text = "1개월 후" xAxisLabelList[i].textColor = color1 padding = paddingWidth * CGFloat(4.7) + labelWidth * CGFloat(i) } xAxisLabelList[i].font = UIFont(name: "PretendardVariable-Medium", size: 10) xAxisLabelList[i].frame = CGRect(x:padding , y:10 , width: labelWidth, height: 18) xAixsLabelView.addSubview(xAxisLabelList[i]) } chartView.addSubview(xAixsLabelView) chartView.bringSubviewToFront(xAixsLabelView) }
确保这些
3条答案
按热度按时间oyjwcjzk1#
在XAxisRenderer.swift(不是像另一个答案建议的那样在XAxisRendererHorizontalBarChart中)中,在drawLabels函数上方添加下面的颜色列表:
然后将drawLabels函数替换为以下代码:
结果:
uqdfh47h2#
在图表库的XAxisRendererHorizontalBarChart.swift文件中有一个方法。2它将设置x轴的文本。3你可以根据你的使用情况定制它。
vlju58qv3#
我是这样做的:先计算xAxis的宽度,然后求出标注之间的距离,再在标注之间相加
确保这些