我想创建条形图。我正在使用Charts库,这是我在viewDidLoad()
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"]
xAxis.valueFormatter = IndexAxisValueFormatter(values:months)
我的问题是只有第一个酒吧有一个标签' Jan'在X轴上。
这是setData代码
func setDataCount(count: Int, range: Double){
let barWidth = 8.5
let spaceForBar = 10.0;
var yVals = [BarChartDataEntry]()
yVals.append(BarChartDataEntry(x: Double(0) * spaceForBar, y: 9.1))
yVals.append(BarChartDataEntry(x: Double(1) * spaceForBar, y: 5.4))
yVals.append(BarChartDataEntry(x: Double(2) * spaceForBar, y: 3.9))
var set1 : BarChartDataSet!
if let count = barChartView.data?.dataSetCount, count > 0{
set1 = barChartView.data?.dataSets[0] as! BarChartDataSet
set1.values = yVals
barChartView.data?.notifyDataChanged()
barChartView.notifyDataSetChanged()
}else{
set1 = BarChartDataSet(values: yVals, label: "")
var dataSets = [BarChartDataSet]()
dataSets.append(set1)
let data = BarChartData(dataSets: dataSets)
data.barWidth = barWidth;
barChartView.data = data
}
}
答案 0 :(得分:0)
对于Above Issue,您只需将3个dataEntry添加到DataSet中,因此在动态代码下面只显示3个条形
var dataEntries: [BarChartDataEntry] = []
//这首声音不是动态的
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
barChartView.data = chartData
希望您能理解条形图数据集的问题。
如果您对上述代码有任何帮助,请与我们联系。