带有 ios-图表的特定柱上方的文本属性
Attributes of text above specific bar with ios-charts
如何更改条形图中特定栏上方文本的属性(例如字体大小、文本颜色等...)?
在这个例子中,我想要 "-$5,000.00" 红色,并增加横条上方每个文本的字体大小。
这是一些代码:
@IBOutlet weak var barChartView: BarChartView!
// init barChartView --------------------------------------
barChartView.descriptionText = ""
barChartView.legend.enabled = false
// grid lines
barChartView.xAxis.drawAxisLineEnabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.leftAxis.drawAxisLineEnabled = false
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
// X-axis line
barChartView.xAxis.drawAxisLineEnabled = true
barChartView.xAxis.axisLineColor = axisGridsAndLabelsColor
// X-axis labels
barChartView.xAxis.labelTextColor = axisGridsAndLabelsColor
barChartView.xAxis.labelPosition = .Bottom
// Y-axis labels
accountsBarChartView.leftAxis.labelTextColor = axisGridsAndLabelsColor
accountsBarChartView.rightAxis.drawLabelsEnabled = false
//---------------------------------------------------------
// bar chart's data
var dataPoints = [String]()
var values = [Double]()
var colors = [UIColor]()
// build bar chart's data...
// dataEntries and barChartDataSet
var dataEntries = [ChartDataEntry]()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let barChartDataSet = BarChartDataSet(yVals: dataEntries, label: "")
barChartDataSet.colors = colors
// valueFormatter
let currencyNumberFormatter = NSNumberFormatter()
currencyNumberFormatter.numberStyle = .CurrencyStyle
currencyNumberFormatter.minimumFractionDigits = 2
currencyNumberFormatter.maximumFractionDigits = 2
barChartDataSet.valueFormatter = currencyNumberFormatter
// barChartData
let barChartData = BarChartData(xVals: dataPoints, dataSet: barChartDataSet)
barChartView.data = barChartData
要设置您自己的 colors/font,您可以使用 BarChartDataSet
class
的属性 valueColors
和 'valueFont'
所以它会是这样的
...
var valueColors = [UIColor]()
// dataEntries and barChartDataSet
var dataEntries = [ChartDataEntry]()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
if values[i] < 0 {
valueColors.append(UIColor.redColor())
}
else {
valueColors.append(UIColor.greenColor())
}
}
let barChartDataSet = BarChartDataSet(yVals: dataEntries, label: "")
barChartDataSet.colors = colors
barChartDataSet.valueColors = valueColors
barChartDataSet.valueFont = *font you want*
如果要更改 下方 条标签的文本属性,您可以使用:
barChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 5)
barChartView.xAxis.labelTextColor = UIColor.red
如何更改条形图中特定栏上方文本的属性(例如字体大小、文本颜色等...)?
在这个例子中,我想要 "-$5,000.00" 红色,并增加横条上方每个文本的字体大小。
这是一些代码:
@IBOutlet weak var barChartView: BarChartView!
// init barChartView --------------------------------------
barChartView.descriptionText = ""
barChartView.legend.enabled = false
// grid lines
barChartView.xAxis.drawAxisLineEnabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.leftAxis.drawAxisLineEnabled = false
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
// X-axis line
barChartView.xAxis.drawAxisLineEnabled = true
barChartView.xAxis.axisLineColor = axisGridsAndLabelsColor
// X-axis labels
barChartView.xAxis.labelTextColor = axisGridsAndLabelsColor
barChartView.xAxis.labelPosition = .Bottom
// Y-axis labels
accountsBarChartView.leftAxis.labelTextColor = axisGridsAndLabelsColor
accountsBarChartView.rightAxis.drawLabelsEnabled = false
//---------------------------------------------------------
// bar chart's data
var dataPoints = [String]()
var values = [Double]()
var colors = [UIColor]()
// build bar chart's data...
// dataEntries and barChartDataSet
var dataEntries = [ChartDataEntry]()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let barChartDataSet = BarChartDataSet(yVals: dataEntries, label: "")
barChartDataSet.colors = colors
// valueFormatter
let currencyNumberFormatter = NSNumberFormatter()
currencyNumberFormatter.numberStyle = .CurrencyStyle
currencyNumberFormatter.minimumFractionDigits = 2
currencyNumberFormatter.maximumFractionDigits = 2
barChartDataSet.valueFormatter = currencyNumberFormatter
// barChartData
let barChartData = BarChartData(xVals: dataPoints, dataSet: barChartDataSet)
barChartView.data = barChartData
要设置您自己的 colors/font,您可以使用 BarChartDataSet
class
valueColors
和 'valueFont'
所以它会是这样的
...
var valueColors = [UIColor]()
// dataEntries and barChartDataSet
var dataEntries = [ChartDataEntry]()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
if values[i] < 0 {
valueColors.append(UIColor.redColor())
}
else {
valueColors.append(UIColor.greenColor())
}
}
let barChartDataSet = BarChartDataSet(yVals: dataEntries, label: "")
barChartDataSet.colors = colors
barChartDataSet.valueColors = valueColors
barChartDataSet.valueFont = *font you want*
如果要更改 下方 条标签的文本属性,您可以使用:
barChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 5)
barChartView.xAxis.labelTextColor = UIColor.red