带圆角的 iso-chart 条形图
iso-chart BarChart with rounded corners
BarChartView 中是否有启用条形圆角的设置?
我想实现与这个其他库类似的效果 iosbarchart
我做了一个 ios-charts 的分支来大致完成这个:https://github.com/mcconkiee/ios-charts
var yVals: [BarChartDataEntry] = []
var xVals: [String] = []
var dataCount = sections.count
for i in 0...(dataCount - 1) {
var dataObjectInstance = sections[i] as SomeCustomDataObject
var entry = BarChartDataEntry(value: dataObjectInstance.numeric!, xIndex: i)
entry.rounded = true //make the end caps round!
if let tintClr = dataObjectInstance.tint as String?{
if let clr = UIColor(hexString: tintClr) as UIColor?{
entry.tint = clr
}
}
yVals.append(entry)
var time = i % 12
if(time == 0) {
time = 12
}
if let objVal = dataObjectInstance.objectValue as String?{
xVals.append(objVal)
}
}
这是在 swift 中使用 UIView 的示例圆形条形图。在您的 .swift 文件中,添加以下代码。
@IBOutlet weak var barView: UIView!
var BAR_WIDTH_CONSTANT: CGFloat = 9
var BAR_DISTANCE: CGFloat = 14 // distance between your bars
var BAR_MAX_HEIGHT: CGFloat = 50 // maximum height of a bar
let MAX_VALUE: CGFloat = 10 //maximum value of a bar
let MIN_VALUE: CGFloat = 0 // minimum value of a bar
然后在你的viewDidLoad()中添加如下代码。
var xPos: CGFloat = 0
var yPos: CGFloat = 0
let values: [Int] = [1, 2, 3, 4, 5, 6, 7] //values of a Bar chart
for i in 0..< values.count {
// draw bar
let barHeight = getBarHeight(height: CGFloat(values[i]), maxHeight: BAR_MAX_HEIGHT, maxValue: MAX_VALUE, minValue: MIN_VALUE)
yPos = BAR_MAX_HEIGHT - barHeight
let frame = CGRect(x: xPos, y: yPos, width: BAR_WIDTH_CONSTANT, height: barHeight)
let bar = UIView(frame: frame)
bar.layer.cornerRadius = BAR_WIDTH_CONSTANT < barHeight ? BAR_WIDTH_CONSTANT / 2 : barHeight / 2
bar.layer.masksToBounds = true
self.barView.addSubview(bar)
xPos = xPos + BAR_WIDTH_CONSTANT + BAR_DISTANCE
// bar drawing complete
}
func getBarHeight(height: CGFloat, maxHeight: CGFloat, maxValue: CGFloat, minValue: CGFloat) -> CGFloat {
let barHeight = (maxHeight * height ) / (maxValue - minValue)
return barHeight
}
希望对您有所帮助。
BarChartView 中是否有启用条形圆角的设置?
我想实现与这个其他库类似的效果 iosbarchart
我做了一个 ios-charts 的分支来大致完成这个:https://github.com/mcconkiee/ios-charts
var yVals: [BarChartDataEntry] = []
var xVals: [String] = []
var dataCount = sections.count
for i in 0...(dataCount - 1) {
var dataObjectInstance = sections[i] as SomeCustomDataObject
var entry = BarChartDataEntry(value: dataObjectInstance.numeric!, xIndex: i)
entry.rounded = true //make the end caps round!
if let tintClr = dataObjectInstance.tint as String?{
if let clr = UIColor(hexString: tintClr) as UIColor?{
entry.tint = clr
}
}
yVals.append(entry)
var time = i % 12
if(time == 0) {
time = 12
}
if let objVal = dataObjectInstance.objectValue as String?{
xVals.append(objVal)
}
}
这是在 swift 中使用 UIView 的示例圆形条形图。在您的 .swift 文件中,添加以下代码。
@IBOutlet weak var barView: UIView!
var BAR_WIDTH_CONSTANT: CGFloat = 9
var BAR_DISTANCE: CGFloat = 14 // distance between your bars
var BAR_MAX_HEIGHT: CGFloat = 50 // maximum height of a bar
let MAX_VALUE: CGFloat = 10 //maximum value of a bar
let MIN_VALUE: CGFloat = 0 // minimum value of a bar
然后在你的viewDidLoad()中添加如下代码。
var xPos: CGFloat = 0
var yPos: CGFloat = 0
let values: [Int] = [1, 2, 3, 4, 5, 6, 7] //values of a Bar chart
for i in 0..< values.count {
// draw bar
let barHeight = getBarHeight(height: CGFloat(values[i]), maxHeight: BAR_MAX_HEIGHT, maxValue: MAX_VALUE, minValue: MIN_VALUE)
yPos = BAR_MAX_HEIGHT - barHeight
let frame = CGRect(x: xPos, y: yPos, width: BAR_WIDTH_CONSTANT, height: barHeight)
let bar = UIView(frame: frame)
bar.layer.cornerRadius = BAR_WIDTH_CONSTANT < barHeight ? BAR_WIDTH_CONSTANT / 2 : barHeight / 2
bar.layer.masksToBounds = true
self.barView.addSubview(bar)
xPos = xPos + BAR_WIDTH_CONSTANT + BAR_DISTANCE
// bar drawing complete
}
func getBarHeight(height: CGFloat, maxHeight: CGFloat, maxValue: CGFloat, minValue: CGFloat) -> CGFloat {
let barHeight = (maxHeight * height ) / (maxValue - minValue)
return barHeight
}
希望对您有所帮助。