我正在使用SwiftChart库,但无法在图表视图中显示条形图的值。
有人可以帮助我吗?
// MARK: Variables
var barChart: BarsChart?
func updateCell(with arrDataSourceEnergyGraph: [EnergyGraph]) {
if arrDataSourceEnergyGraph.count > 0 {
if barChart != nil {
barChart!.view.removeFromSuperview()
}
let arrKilowatt = (arrDataSourceEnergyGraph.map {$0.kilowatt.doubleValue})
var chartSetting = ChartSettings()
chartSetting.top = 5
var from: Double = 0
var to: Double = 1
var by: Double = 0.5
if arrKilowatt.min() != arrKilowatt.max() {
from = arrKilowatt.min()!.rounded()
if arrKilowatt.max()!.rounded() == 0.0 {
if arrKilowatt.max()! < 0.5 {
to = arrKilowatt.max()! + arrKilowatt.max()!
by = ((from + to) / 6)
}else {
to = arrKilowatt.max()! + arrKilowatt.max()!
by = ((from + to) / 4)
}
}else {
to = arrKilowatt.max()!.rounded() + 2
by = ((from + to) / 4).rounded()
}
}
let chartAxisConfig = ChartAxisConfig(from: from, to: to, by: by)
let guidelinesConfig = GuidelinesConfig(dotted: false, lineWidth: 0.1, lineColor: UIColor.white)
let xAxisLabelSettings: ChartLabelSettings = ChartLabelSettings(font: UIFont(name: Constant.Font.Regular, size: 10.proportionalFontSize)!, fontColor: UIColor.black)
let yAxisLabelSettings: ChartLabelSettings = ChartLabelSettings()
let chartConfig = BarsChartConfig(chartSettings: chartSetting,
valsAxisConfig: chartAxisConfig,
xAxisLabelSettings: xAxisLabelSettings,
yAxisLabelSettings: yAxisLabelSettings,
guidelinesConfig: guidelinesConfig
)
var graphList: [(String, Double)] = []
for model in arrDataSourceEnergyGraph {
graphList.append((model.dayMonth, model.kilowatt.doubleValue))
print("chartValue", model.kilowatt.doubleValue)
}
let frame = CGRect(x: -15, y: 0, width: self.width + 5, height: self.height - 15)
barChart = BarsChart(
frame: frame,
chartConfig: chartConfig, xTitle: "", yTitle: "", bars: graphList,
color: UIColor.mOrangeColor(), barWidth: GET_PROPORTIONAL_WIDTH(20),
animDuration: 0.5
)
self.chartView.addSubview(barChart!.view)
guard let firstDate = arrDataSourceEnergyGraph.first?.dayAndMonth, let lastDate = arrDataSourceEnergyGraph.last?.dayAndMonth else { return }
self.labelDuration.text = "Energy Used (\(firstDate) to \(lastDate))"
}
}
我的代码正确显示了图形,但条形图的顶部没有显示值。