import UIKit
import Charts
class ZGAcitivityBarChartsCell: UITableViewCell {
//使用xib 方式拖入一个UIView ,类为BarChartView
@IBOutlet weak var chartView: BarChartView!
var titles: [String]? {
didSet {
setData(datas: datas)
}
}
var datas: [Double] = [] {
didSet {
setData(datas: datas)
}
}
var colors = [UIColor.init(red: 142/255.0, green: 189/255.0, blue: 255/255.0, alpha: 1.0),
UIColor.init(red: 255/255.0, green: 86/255.0, blue: 101/255.0, alpha: 1.0),
UIColor.init(red: 0/255.0, green: 211/255.0, blue: 189/255.0, alpha: 1.0)] {
didSet {
setData(datas: datas)
}
}
var preStr: String = "" {
didSet {
setData(datas: datas)
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
setupUI()
}
}
extension ZGAcitivityBarChartsCell {
//设置chartView的属性
fileprivate func setupUI() {
chartView.chartDescription?.enabled = false
chartView.pinchZoomEnabled = true
chartView.setScaleEnabled(false)
chartView.drawBarShadowEnabled = false
chartView.drawGridBackgroundEnabled = false
//设置类型试图的对齐方式,右上角
let legend = chartView.legend
legend.enabled = true
legend.horizontalAlignment = .right
legend.verticalAlignment = .top
legend.orientation = .horizontal
legend.textColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
legend.font = UIFont.systemFont(ofSize: 11.0)
//设置x轴的属性,颜色,字体大小等
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.drawGridLinesEnabled = false
xAxis.gridColor = UIColor.init(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)
xAxis.axisLineColor = UIColor.init(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)
xAxis.labelTextColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
xAxis.labelFont = UIFont.systemFont(ofSize: 11.0)
//设置y轴的属性,颜色,字体大小等
chartView.leftAxis.drawGridLinesEnabled = true
chartView.leftAxis.gridColor = UIColor.init(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)
chartView.leftAxis.axisLineColor = UIColor.clear
chartView.leftAxis.labelTextColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
chartView.leftAxis.valueFormatter = self
chartView.rightAxis.drawGridLinesEnabled = false
chartView.rightAxis.enabled = false
chartView.leftAxis.axisMinimum = 0.0
}
//设置数据
fileprivate func setData(datas: [Double]) {
var values = [[BarChartDataEntry]]()
for i in 0..<datas.count {
let val = datas[i]
var value1 = [BarChartDataEntry]()
value1.append(BarChartDataEntry.init(x: Double(i), yValues: [val], label: ""))
values.append(value1)
}
if values.count == 0 {
chartView.data = nil
chartView.data?.notifyDataChanged()
chartView.notifyDataSetChanged()
chartView.noDataText = "暂无数据,请重新刷新"
return
}
var sets = [BarChartDataSet]()
for i in 0..<values.count {
var label: String = ""
if let titles = titles {
label = titles[i%titles.count]
}
let set1:BarChartDataSet = BarChartDataSet.init(values: values[i], label: label)
var colors = [NSUIColor]()
colors.append(self.colors[i%(self.colors.count)])
set1.colors = colors
set1.formSize = 8.0
set1.highlightEnabled = false
set1.drawValuesEnabled = true
set1.valueFormatter = self
set1.valueTextColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
set1.valueFont = UIFont.systemFont(ofSize: 11.0)
sets.append(set1)
}
let data = BarChartData.init(dataSets: sets)
chartView.data = data
chartView.xAxis.valueFormatter = self
chartView.xAxis.labelCount = datas.count
chartView.fitBars = true
chartView.barData?.barWidth = 0.3
chartView.setNeedsLayout()
}
}
extension ZGAcitivityBarChartsCell: IAxisValueFormatter, IValueFormatter{
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
if axis == chartView.leftAxis { //y轴
if value == Double(Int(value)) {
if value >= 1000 {
var y_value = String(format: "%.3f", value/1000)
if y_value.hasSuffix("0") {
y_value.remove(at: y_value.index(before: y_value.endIndex))
}
if y_value.hasSuffix("0") {
y_value.remove(at: y_value.index(before: y_value.endIndex))
}
if y_value.hasSuffix("0") {
y_value.remove(at: y_value.index(before: y_value.endIndex))
}
if y_value.hasSuffix(".") {
y_value.remove(at: y_value.index(before: y_value.endIndex))
}
return y_value+"k"
}else {
return "\(Int(value))"
}
}else {
return ""
}
}else { //x轴
if let titles = titles {
if Int(value) <= titles.count-1 {
return titles[Int(value)]
}
}
return ""
}
}
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
return "\(preStr)\(value)"
}
}
使用
let cell = tableView.dequeueReusableCell(withIdentifier: "ZGAcitivityBarChartsCell") as! ZGAcitivityBarChartsCell
cell.titles = ["活动前","活动后","提升额"]
cell.datas = [1400,1500,1300]
cell.preStr = "¥"
return cell
效果图