import UIKit
import DGCharts
class GradientBarChartRenderer: BarChartRenderer {
override func drawDataSet(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int) {
guard let dataProvider = dataProvider else { return }
let transformer = dataProvider.getTransformer(forAxis: dataSet.axisDependency)
let phaseY = animator.phaseY
// Determine bar width
let entryCount = dataSet.entryCount
let barWidth: CGFloat
if entryCount <= 5 {
let totalSpace = viewPortHandler.contentWidth / CGFloat(entryCount + 2) // Leave one space for padding
barWidth = 36.0 / totalSpace
} else {
barWidth = 0.67//CGFloat(dataProvider.barData?.barWidth ?? 0.85)
}
dataProvider.barData?.barWidth = Double(barWidth)
// Prepare the buffer
var buffer = [CGRect](repeating: CGRect(), count: entryCount)
for i in 0 ..< entryCount {
guard let e = dataSet.entryForIndex(i) as? BarChartDataEntry else { continue }
let left = e.x - barWidth / 2.0
let right = e.x + barWidth / 2.0
let top = e.y * Double(phaseY)
let bottom = 0.0
buffer[i] = CGRect(x: left, y: bottom, width: right - left, height: top)
}
transformer.rectValuesToPixel(&buffer)
// Draw the bars
for i in 0 ..< buffer.count {
context.saveGState()
let gradientColors = [ChartColorTemplates.colorFromString("#ff0000").cgColor,
ChartColorTemplates.colorFromString("#00ff00").cgColor] as CFArray
let colorLocations:[CGFloat] = [0.0, 1.0]
let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations)
if let gradient = gradient {
context.clip(to: buffer[i])
context.drawLinearGradient(gradient, start: CGPoint(x: buffer[i].minX, y: buffer[i].minY), end: CGPoint(x: buffer[i].minX, y: buffer[i].maxY), options: [])
}
context.restoreGState()
}
}
DGCharts barView GradientColor
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 Google Play应用市场对于应用的targetSdkVersion有了更为严格的要求。从 2018 年...