iOS 图表未显示 Y 值
iOS Charts not showing Y values
我正在 Swift 4.2 中编写 iOS 应用程序。我正在使用 Charts Library 来显示水平条形图。当我打开示例项目时,它显示 Y 值,但在我的项目中它没有显示 Y 值。我从它的示例中复制了确切的代码。
示例屏幕截图:
观察 8.0, 14.0, 4.0, 10.0
我的截图:
我的代码:
@IBOutlet weak var chartView: HorizontalBarChartView!
func setupCharts(){
chartView.noDataTextColor=UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor)
chartView.noDataText = "Chart_NoDataText".localized
chartView.chartDescription?.enabled = false
chartView.dragEnabled = false
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = false
//chartView.delegate = self
chartView.drawBarShadowEnabled = false
chartView.drawValueAboveBarEnabled = true
chartView.maxVisibleCount = 60
chartView.setVisibleXRangeMaximum(4)
chartView.drawGridBackgroundEnabled=false
chartView.extraRightOffset=CGFloat(24)
chartView.isUserInteractionEnabled = false
chartView.legend.enabled=false
chartView.fitBars = true
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = UIFont(name: "Lato-Regular", size: 11)!
xAxis.drawAxisLineEnabled = false
xAxis.drawGridLinesEnabled = false
xAxis.granularity = 1
xAxis.enabled=true
//
let leftAxis = chartView.leftAxis
leftAxis.labelFont = UIFont(name: "Lato-Regular", size: 10)!
leftAxis.drawAxisLineEnabled = false
leftAxis.drawGridLinesEnabled = false
leftAxis.axisMinimum = 0
leftAxis.enabled=false
let rightAxis = chartView.rightAxis
rightAxis.labelFont = UIFont(name: "Lato-Regular", size: 10)!
rightAxis.drawAxisLineEnabled = false
rightAxis.drawGridLinesEnabled = false
rightAxis.axisMinimum = 0
rightAxis.enabled = false
}
func setChartDataWithValues(_ count: Int, values:[Double], labels:[String]) {
let yVals = (0..<count).map { (i) -> BarChartDataEntry in
let val = values[i]
return BarChartDataEntry(x: Double(i), y: val)
}
let barChartDataSet = BarChartDataSet(values: yVals, label: "MF")
barChartDataSet.drawIconsEnabled = false
barChartDataSet.colors=[UIColor(hex: UIConstants.Graphics.BrandTheme.ThemeColor)]
let data = BarChartData(dataSet: barChartDataSet)
data.setValueFont(UIFont(name:"Lato-Regular", size:10)!)
data.setValueTextColor(UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor))
let xAxis = chartView?.xAxis
xAxis?.setLabelCount(3, force: false)
xAxis?.valueFormatter=IndexAxisValueFormatter(values: labels )
chartView?.data = data
}
通话中:
self.setChartDataWithValues(3,values:values, labels: ["Mutual Funds","Fixed Deposits","Savings Account"].reversed())
创建 BarChartData
时应添加 data.setDrawValues(true)
:
...
let data = BarChartData(dataSet: barChartDataSet)
data.setDrawValues(true)
data.setValueFont(UIFont(name:"Lato-Regular", size:10)!)
data.setValueTextColor(UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor))
...
对于 Charts
的 3.2.1
版本,您还需要删除此行,因为如果 drawIconsEnabled
为 false
,则不会呈现值
barChartDataSet.drawIconsEnabled = false
我正在 Swift 4.2 中编写 iOS 应用程序。我正在使用 Charts Library 来显示水平条形图。当我打开示例项目时,它显示 Y 值,但在我的项目中它没有显示 Y 值。我从它的示例中复制了确切的代码。
示例屏幕截图:
我的截图:
我的代码:
@IBOutlet weak var chartView: HorizontalBarChartView!
func setupCharts(){
chartView.noDataTextColor=UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor)
chartView.noDataText = "Chart_NoDataText".localized
chartView.chartDescription?.enabled = false
chartView.dragEnabled = false
chartView.setScaleEnabled(true)
chartView.pinchZoomEnabled = false
//chartView.delegate = self
chartView.drawBarShadowEnabled = false
chartView.drawValueAboveBarEnabled = true
chartView.maxVisibleCount = 60
chartView.setVisibleXRangeMaximum(4)
chartView.drawGridBackgroundEnabled=false
chartView.extraRightOffset=CGFloat(24)
chartView.isUserInteractionEnabled = false
chartView.legend.enabled=false
chartView.fitBars = true
let xAxis = chartView.xAxis
xAxis.labelPosition = .bottom
xAxis.labelFont = UIFont(name: "Lato-Regular", size: 11)!
xAxis.drawAxisLineEnabled = false
xAxis.drawGridLinesEnabled = false
xAxis.granularity = 1
xAxis.enabled=true
//
let leftAxis = chartView.leftAxis
leftAxis.labelFont = UIFont(name: "Lato-Regular", size: 10)!
leftAxis.drawAxisLineEnabled = false
leftAxis.drawGridLinesEnabled = false
leftAxis.axisMinimum = 0
leftAxis.enabled=false
let rightAxis = chartView.rightAxis
rightAxis.labelFont = UIFont(name: "Lato-Regular", size: 10)!
rightAxis.drawAxisLineEnabled = false
rightAxis.drawGridLinesEnabled = false
rightAxis.axisMinimum = 0
rightAxis.enabled = false
}
func setChartDataWithValues(_ count: Int, values:[Double], labels:[String]) {
let yVals = (0..<count).map { (i) -> BarChartDataEntry in
let val = values[i]
return BarChartDataEntry(x: Double(i), y: val)
}
let barChartDataSet = BarChartDataSet(values: yVals, label: "MF")
barChartDataSet.drawIconsEnabled = false
barChartDataSet.colors=[UIColor(hex: UIConstants.Graphics.BrandTheme.ThemeColor)]
let data = BarChartData(dataSet: barChartDataSet)
data.setValueFont(UIFont(name:"Lato-Regular", size:10)!)
data.setValueTextColor(UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor))
let xAxis = chartView?.xAxis
xAxis?.setLabelCount(3, force: false)
xAxis?.valueFormatter=IndexAxisValueFormatter(values: labels )
chartView?.data = data
}
通话中:
self.setChartDataWithValues(3,values:values, labels: ["Mutual Funds","Fixed Deposits","Savings Account"].reversed())
创建 BarChartData
时应添加 data.setDrawValues(true)
:
...
let data = BarChartData(dataSet: barChartDataSet)
data.setDrawValues(true)
data.setValueFont(UIFont(name:"Lato-Regular", size:10)!)
data.setValueTextColor(UIColor(hex:UIConstants.Graphics.BrandTheme.ThemeColor))
...
对于 Charts
的 3.2.1
版本,您还需要删除此行,因为如果 drawIconsEnabled
为 false
barChartDataSet.drawIconsEnabled = false