我的图表没有使用此条形图显示任何条形图。我已经成功导入了图表可可豆荚。当前没有运行时错误。图形中唯一显示的是描述标签。
import UIKit
import Charts
class ViewController: UIViewController {
@IBOutlet var lineChartVIew: BarChartView!
var days: [String]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
days = ["Monday","Tuesday","life"]
let task = [1.0,4.0,3.0]
setChart(dataPoints: days, values: task)
}
func setChart(dataPoints : [String], values : [Double]){
lineChartVIew.noDataText = "Nothining to display"
var dataEntries : [BarChartDataEntry] = []
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1
let dataEntery = BarChartDataEntry(x: values[i], y: counter)
dataEntries.append(dataEntery)
}
let ChartDataSet = BarChartDataSet(values: dataEntries, label: "Time")
let chartData = BarChartData()
lineChartVIew.data = chartData
ChartDataSet.colors = ChartColorTemplates.colorful()
lineChartVIew.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}}
答案 0 :(得分:0)
尝试一下它正在起作用(Swift 4代码)。
import UIKit
import Charts
class RootViewController: UIViewController {
@IBOutlet weak var lineChartView: BarChartView!
var days: [String]!
override func viewDidLoad() {
super.viewDidLoad()
days = ["Monday","Tuesday","Wednesday","Thursday"]
let task = [1.0,4.0,3.0,5.0]
setChart(dataPoints: days, values: task)
}
func setChart(dataPoints : [String], values : [Double]){
lineChartView.noDataText = "Nothining to display"
var dataEntries : [BarChartDataEntry] = []
var counter = 0.0
for i in 0..<dataPoints.count {
counter += 1
let dataEntery = BarChartDataEntry(x: counter, y:values[i], data: days as AnyObject)
dataEntries.append(dataEntery)
}
let ChartDataSet = BarChartDataSet(values: dataEntries, label: "Time")
let chartData = BarChartData()
chartData.addDataSet(ChartDataSet)
lineChartView.data = chartData
ChartDataSet.colors = ChartColorTemplates.joyful()
lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}
}
输出为:
修改图形隐藏和显示轴及标签
lineChartView.leftAxis.drawLabelsEnabled = false // Hide Left Axis Label
lineChartView.rightAxis.drawLabelsEnabled = false // Hide Right Axis Label
lineChartView.xAxis.drawLabelsEnabled = false // Hide Top Axis Label
lineChartView.leftAxis.enabled = false // Hide Left Axis Lines
lineChartView.rightAxis.enabled = false // Hide Right Axis Lines
lineChartView.xAxis.enabled = false // Hide Right Axis Lines
lineChartView.legend.enabled = false //Hide Legend of Chart
lineChartView.chartDescription?.text = "" // Hide or Change Chart Description text