我正在使用iOS图表库来集成LineChartView,我必须创建以下图表:
到目前为止,我已经实现了这个目标:
我在这里遇到三个问题:
xAxis
中缺少2月和6月的月份文字。 是否可以在图片中显示的月份上方添加小标记,以便在xAxis
中显示月份?
已实施的代码:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [50.0, 25.0, 50.0, 75.0, 100.0, 75.0]
setChart(dataPoints: months, values: unitsSold)
和setChart
方法:
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: nil)
chartDataSet.circleRadius = 5
chartDataSet.circleHoleRadius = 2
chartDataSet.drawValuesEnabled = false
let chartData = LineChartData(dataSets: [chartDataSet])
lineChartView.data = chartData
lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.drawGridLinesEnabled = false
lineChartView.xAxis.avoidFirstLastClippingEnabled = true
lineChartView.rightAxis.drawAxisLineEnabled = false
lineChartView.rightAxis.drawLabelsEnabled = false
lineChartView.leftAxis.drawAxisLineEnabled = false
lineChartView.pinchZoomEnabled = false
lineChartView.doubleTapToZoomEnabled = false
lineChartView.legend.enabled = false
}
如果有人在swift工作过图表,请提供帮助。
答案 0 :(得分:1)
1)为避免人群Chart
通过缩放用户在图表轴上显示有限数据,可以查看整个数据
2)对于左右间距:从界面构建器中提供左右约束
3)要隐藏说明,您可以使用以下代码
lineChartView.chartDescription?.text = " "
希望这能帮到你
答案 1 :(得分:0)
我有同样的问题。我这样做是为了解决这个问题:
ineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
ineChartView.xAxis.setLabelCount(months.count, force: true)
希望有帮助。