gpt4 book ai didi

ios - 使用图表创建的条形图上仅出现一个标签

转载 作者:行者123 更新时间:2023-11-30 12:13:49 25 4
gpt4 key购买 nike

我想使用图表库创建条形图。除了 x 轴上的标签之外,一切正常。仅第一个标签“Jan”出现在该行上。这是我的代码

override func viewWillAppear(_ animated: Bool) {
doBarChart()
}

func doBarChart(){


barChartView.drawBarShadowEnabled = false
barChartView.drawValueAboveBarEnabled = true
barChartView.chartDescription?.enabled = false

barChartView.maxVisibleCount = 60

let xAxis = barChartView.xAxis
xAxis.axisLineColor = UIColor.black
xAxis.labelPosition = .bottom
xAxis.drawAxisLineEnabled = true
xAxis.drawGridLinesEnabled = false
xAxis.granularity = 1.0
xAxis.labelCount = 1
// xAxis.setLabelCount(7, force: true)
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",]

xAxis.valueFormatter = IndexAxisValueFormatter(values:months)
//Also, you probably want to add:



let leftAxis = barChartView.leftAxis;
leftAxis.enabled = false
leftAxis.drawAxisLineEnabled = false;
leftAxis.drawGridLinesEnabled = false;
leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES

let rightAxis = barChartView.rightAxis
rightAxis.enabled = false;

rightAxis.drawAxisLineEnabled = true;
rightAxis.drawGridLinesEnabled = false;
rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES

let l = barChartView.legend
l.enabled = false

barChartView.fitBars = true;
barChartView.animate(xAxisDuration: 0.2, yAxisDuration: 1.0, easingOptionX: .easeInExpo, easingOptionY: .easeInExpo)

setDataCount(count: 7, range: 50)

}


func setDataCount(count: Int, range: Double){

let barWidth = 7.0
let spaceForBar = 10.0

var yVals = [BarChartDataEntry]()


yVals.append(BarChartDataEntry(x: Double(0) * spaceForBar, y: 44.5))
yVals.append(BarChartDataEntry(x: Double(1) * spaceForBar, y: 78.1))
yVals.append(BarChartDataEntry(x: Double(2) * spaceForBar, y: 50.3))
yVals.append(BarChartDataEntry(x: Double(3) * spaceForBar, y: 56.6))
yVals.append(BarChartDataEntry(x: Double(4) * spaceForBar, y: 20.5))
yVals.append(BarChartDataEntry(x: Double(5) * spaceForBar, y: 44.3))
yVals.append(BarChartDataEntry(x: Double(6) * spaceForBar, y: 54.4))

var set1 : BarChartDataSet!


if let count = barChartView.data?.dataSetCount, count > 0{
set1 = barChartView.data?.dataSets[0] as! BarChartDataSet
set1.values = yVals
set1.colors = [UIColor.black,UIColor.orange,UIColor.red,UIColor.green,UIColor.yellow,UIColor.blue,UIColor.gray]
barChartView.data?.notifyDataChanged()
barChartView.notifyDataSetChanged()

}else{
set1 = BarChartDataSet(values: yVals, label: "DataSet")
set1.colors = [UIColor.black,UIColor.orange,UIColor.red,UIColor.green,UIColor.yellow,UIColor.blue,UIColor.gray]


var dataSets = [BarChartDataSet]()
dataSets.append(set1)

let data = BarChartData(dataSets: dataSets)


data.barWidth = barWidth;

barChartView.data = data

}
}

最佳答案

Add the following delegate method of "IAxisValueFormatter"

And assign these delegate.

class xyz : IAxisValueFormatter{

weak var axisFormatDelegate: IAxisValueFormatter?

//add these line in the func...

func doBarChart{
let xAxisValue = lineChartView.xAxis
xAxisValue.valueFormatter = axisFormatDelegate
lineChartView.xAxis.granularityEnabled = true
lineChartView.xAxis.granularity = 1.0
}

//add these method..

func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return yvlaues[Int(value) % yvlaues.count]
}
}

关于ios - 使用图表创建的条形图上仅出现一个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45678054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com