0
나는 아래의 그래프에서 정수로이 두 배로 변환 할 : 나는 다른 게시물의 조언을 따라하고 YAxisValueFormatter을 (만든iOS 막대 차트에서이 두 배수를 정수로 변환하려면 어떻게해야합니까?
) IAxisValueFormatter를 구현하지만이의 값에 영향을주지 않습니다 막대, 단지 축.
이 내 차트 코드 :
func setChart(dataPoints: [String], values: [Double]){
let formato:BarChartFormatter = BarChartFormatter()
formato.setValues(values: dataPoints)
let xaxis:XAxis = XAxis()
let xAxis : XAxis = self.barChartView.xAxis;
barChartView.noDataText = "you need to provide some data for the chart."
var dataEntries: [BarChartDataEntry] = Array()
for i in 0..<dataPoints.count
{
let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
xaxis.valueFormatter = formato
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
xAxis.labelFont = UIFont(name: "Avenir", size: 14.0)!
xAxis.labelTextColor = UIColor.white
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
let chartData = BarChartData(dataSets: [chartDataSet])
chartDataSet.colors = ChartColorTemplates.material()
barChartView.xAxis.labelPosition = .bottom
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.valueFormatter = xaxis.valueFormatter
barChartView.chartDescription?.enabled = false
barChartView.legend.enabled = true
barChartView.rightAxis.enabled = false
barChartView.data = chartData
barChartView.leftAxis.enabled = true
barChartView.legend.enabled = false
barChartView.chartDescription?.text = ""
chartData.addDataSet(chartDataSet)
barChartView.data = chartData
chartData.setDrawValues(true)
chartDataSet.drawValuesEnabled = true
barChartView.barData?.setValueFont(UIFont(name: "Avenir", size: 12.0))
barChartView.barData?.setValueTextColor(UIColor.white)
barChartView.xAxis.granularity = 1.0
barChartView.leftAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.leftAxis.labelFont = UIFont(name: "Avenir", size: 12.0)!
barChartView.leftAxis.labelTextColor = UIColor.white
barChartView.leftAxis.axisMinimum = 0
barChartView.leftAxis.valueFormatter = YAxisValueFormatter()
self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom
}
그리고 이것은 내 YAxisValueFormatter 클래스입니다 :
class YAxisValueFormatter: NSObject, IAxisValueFormatter {
let numFormatter: NumberFormatter
override init() {
numFormatter = NumberFormatter()
numFormatter.minimumFractionDigits = 0
numFormatter.maximumFractionDigits = 0
// if number is less than 1 add 0 before decimal
numFormatter.minimumIntegerDigits = 0 // how many digits do want before decimal
numFormatter.paddingPosition = .beforePrefix
numFormatter.paddingCharacter = "0"
}
/// Called when a value from an axis is formatted before being drawn.
///
/// For performance reasons, avoid excessive calculations and memory allocations inside this method.
///
/// - returns: The customized label that is drawn on the axis.
/// - parameter value: the value that is currently being drawn
/// - parameter axis: the axis that the value belongs to
///
public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return numFormatter.string(from: NSNumber(floatLiteral: value))!
}
}
내가 입력 유형의 'YAxisValueFormatter'값을 할당 할 수 없습니다 에러 -'얻을 수 할 경우 'IValueFormatter을?' – RDowns
열쇠는'chartDataSet'의'valueFormatter'을 설정하고 설정하는 것입니다. 나는 그것이 축과 동일한 포맷터를 사용할 수 있다고 추측했지만 분명히 사실이 아닙니다. 'chartDataSet.valueFormatter = YAxisValueFormatter(). numFormatter'라고하면 어떻게됩니까? – vacawama
불행히도 같은 오류가 발생했습니다. – RDowns