Reputation: 265
I am displaying the data I received from the server on the bar chart.
The minimum value is 0, and the maximum value is not set, so the highest value is the reference.
However, zero value is not drawn at all, so I want to give it a little height.
Even if the value is 0 as shown in the picture above, I would like to display it to the user at least a little.
// 기본 문구
self.monthlyBarChart.noDataText = "No Data."
// 기본 문구 폰트
self.monthlyBarChart.noDataFont = .systemFont(ofSize: 20)
// 기본 문구 색상
self.monthlyBarChart.noDataTextColor = .lightGray
// x축 Grid
self.monthlyBarChart.xAxis.drawGridLinesEnabled = false
// 왼쪽 벽 데이터 제거
self.monthlyBarChart.leftAxis.enabled = false
// 오른쪽 벽 데이터 제거
self.monthlyBarChart.rightAxis.enabled = false
// 범주 제거
self.monthlyBarChart.legend.enabled = false
// 그래프 왼쪽 벽 제거
self.monthlyBarChart.leftAxis.drawAxisLineEnabled = false
// 그래프 오른쪽 벽 제거
self.monthlyBarChart.rightAxis.drawAxisLineEnabled = false
// 정보 아래로 내리기
self.monthlyBarChart.xAxis.labelPosition = .bottom
// 최저 값
self.monthlyBarChart.leftAxis.axisMinimum = 0
// 터치 막기
self.monthlyBarChart.isUserInteractionEnabled = false
// 애니메이션
self.monthlyBarChart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
// 데이터 범례 삭제
self.monthlyBarChart.legend.enabled = false
// 구분값 모두 보이기
self.monthlyBarChart.xAxis.setLabelCount(workCount.count, force: false)
My Chart base option use in awakeFromNib of Cell
func initChart() {
let lastCount: Int = Int(self.workCount.last ?? 0)
// 구분값 보이기
self.monthlyBarChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: dayData)
// 생성한 함수 사용해서 데이터 적용
self.setBarData(barChartView: self.monthlyBarChart, barChartDataEntries: self.entryData(values: self.workCount))
}
// 데이터셋 만들고 차트에 적용하기
func setBarData(barChartView: BarChartView, barChartDataEntries: [BarChartDataEntry]) {
// 데이터 셋 만들기
let barChartdataSet = BarChartDataSet(entries: barChartDataEntries, label: "text")
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = Locale.current
let valuesNumberFormatter = ChartValueFormatter(numberFormatter: numberFormatter)
barChartdataSet.valueFormatter = valuesNumberFormatter
// 차트 데이터 만들기
let barChartData = BarChartData(dataSet: barChartdataSet)
barChartData.barWidth = Double(0.3)
// 데이터 차트에 적용
barChartView.data = barChartData
}
// entry 만들기
func entryData(values: [Double]) -> [BarChartDataEntry] {
// 엔트리들 만들기
var barDataEntries: [BarChartDataEntry] = []
// 데이터 값 만큼 엔트리 생성
for i in 0 ..< values.count {
let barDataEntry = BarChartDataEntry(x: Double(i), y: values[i])
barDataEntries.append(barDataEntry)
}
// 엔트리들 반환
return barDataEntries
}
The part where I put the data in the Chart I'm using in Cell
var height: CGFloat = bottom - top
if height == 0 {
height = 3
}
let barRect = CGRect(x: left, y: top,
width: right - left,
height: bottom - top)
height: height)
I tried to apply the code as above in the "prepareBuffer" function of "BarChartRenderer". When applied in this way, there were times when it was seen depending on the maximum value, and sometimes the height of a bar with a value of 0 became too high..
'BarChartRenderer' is responsible for the drawing, animation, style, and representation of Bar Chart in the library.
I understand that 'prepareBuffer' prepares and processes data before rendering graphs. Calculate the location, height, and width of the bar and match the values of the data set to the coordinate system in the chart.
The barRect is primarily used to store the position, height, width, and other properties of each bar when drawing a bar chart. Each bar is drawn based on the values provided in the dataset, and the barRect is used as the basis for actually rendering the bars on the screen.
Examples of data used are as follows.
var dayData: [String] = ["1day", "2day", "3day", "4day", "5day", "6day"]
let workCount = [10, 0, 100, 200, 50, 2]
Upvotes: 0
Views: 100