我目前正在使用IOS图表来获取折线图。
这是指向它的链接:Link
记录非常糟糕,我无法将X轴标签集中到我图表上的图表中。
我已启用粒度并将其设置为1. - 这可确保标签不会重复
我知道点的间距不同,但我的主要关注点是如何使标签以正确的数据点为中心。
我的代码:
`import UIKit 导入图表
class ReviewDetailVC:UIViewController,ChartViewDelegate {
@IBOutlet weak var chartView: LineChartView!
var yValues = [Double]()
var months = [String]()
var xValues = [String]()
let red = UIColor(hue: 0.9639, saturation: 0.62, brightness: 0.93, alpha: 1.0)
let black = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
let grey = UIColor(red:0.81, green:0.81, blue:0.81, alpha:1.0)
let avenirDemi = UIFont(name: "AvenirNext-DemiBold", size: 14)
override func viewDidLoad() {
super.viewDidLoad()
chartView.delegate = self
// Chart Ui Settings
chartView.xAxis.axisMinimum = 1
chartView.leftAxis.axisMaximum = 5
chartView.leftAxis.axisMinimum = 0.0
chartView.chartDescription?.text = ""
chartView.xAxis.labelPosition = .bottom
chartView.legend.enabled = false
chartView.scaleYEnabled = false
chartView.scaleXEnabled = true
chartView.doubleTapToZoomEnabled = false
chartView.highlighter = nil
chartView.rightAxis.enabled = false
chartView.xAxis.drawGridLinesEnabled = false
chartView.dragEnabled = true
chartView.scaleXEnabled = false
chartView.scaleYEnabled = false
chartView.zoom(scaleX: 4, scaleY: 1, x: 0, y: CGFloat(AxisDependency.left.rawValue))
chartView.xAxis.labelFont = avenirDemi!
chartView.xAxis.labelTextColor = grey
chartView.leftAxis.labelFont = avenirDemi!
chartView.leftAxis.labelTextColor = black
chartView.xAxis.axisLineWidth = 0
chartView.leftAxis.axisLineWidth = 0
//chartView.xAxis.avoidFirstLastClippingEnabled = true
chartView.xAxis.granularityEnabled = true
chartView.xAxis.granularity = 1
chartView.leftAxis.gridColor = grey
chartView.leftAxis.gridLineDashLengths = [4]
chartView.leftAxis.gridLineWidth = 1.5
chartView.xAxis.centerAxisLabelsEnabled = true
chartView.noDataText = "This person has not reviewed your business."
//**MARK:: DATA SHIT
let sales = DataGenerator.data()
xValues = ["MAR 10"," MAR 15", "APR 7", "APR 8", "APR 15", "APR 30", "MAY 14", "MAY 21","MAY 31", "MAY 31"]
yValues = [4,2,4,5,3,4,2,4,5]
var salesEntries = [ChartDataEntry]()
var salesMonths = [String]()
var i = 0
for sale in sales {
// Create single chart data entry and append it to the array
let saleEntry = ChartDataEntry(x: Double(i), y: sale.value)
salesEntries.append(saleEntry)
// Append the month to the array
salesMonths.append(sale.date)
i += 1
}
// Create bar chart data set containing salesEntries
let chartDataSet = LineChartDataSet(values: salesEntries, label: "Profit")
// Create bar chart data with data set and array with values for x axis
let chartData = LineChartData(dataSets: [chartDataSet])
//setChart(dataPoints: xValues, values: yValues)
//let formatter = CustomLabelsAxisValueFormatter(miniTime: 0.0)
//formatter.labels = xValues
chartView.xAxis.valueFormatter = CustomLabelsAxisValueFormatter(miniTime: 0.0)
//**MARK:: END DATA SHIT
chartDataSet.colors = [red]
chartDataSet.drawCirclesEnabled = true
chartDataSet.circleRadius = 16
chartDataSet.circleColors = [red]
chartDataSet.circleHoleRadius = 12
chartDataSet.circleHoleColor = UIColor.white
chartDataSet.lineWidth = 4
chartDataSet.drawValuesEnabled = false
chartView.animate(yAxisDuration: 1)
chartView.setVisibleXRangeMaximum(4)
// Set bar chart data
chartView.data = chartData
setChart(dataPoints: xValues, values: yValues)
}
func setChart(dataPoints: [String], values: [Double]) {
chartView.noDataText = "You need to provide data for the chart."
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
let chartData = LineChartData(dataSets: [chartDataSet])
chartDataSet.colors = [red]
chartDataSet.drawCirclesEnabled = true
chartDataSet.circleRadius = 16
chartDataSet.circleColors = [red]
chartDataSet.circleHoleRadius = 12
chartDataSet.circleHoleColor = UIColor.white
chartDataSet.lineWidth = 4
chartView.data = chartData
}
}`
答案 0 :(得分:3)
您可以将setChart方法更改为:
func setChart(dataPoints: [String], values: [Double]) {
chartView.noDataText = "You need to provide data for the chart."
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x: Double(i)+0.5, y: values[i])
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
let chartData = LineChartData(dataSets: [chartDataSet])
chartDataSet.colors = [red]
chartDataSet.drawCirclesEnabled = true
chartDataSet.circleRadius = 16
chartDataSet.circleColors = [red]
chartDataSet.circleHoleRadius = 12
chartDataSet.circleHoleColor = UIColor.white
chartDataSet.lineWidth = 4
chartView.data = chartData
}
更新:
chartView.xAxis.axisMinimum = 0.0 //I changed this from 1 to 0
let xvalues = ["MAR 10"," MAR 15", "APR 7", "APR 8", "APR 15", "APR 30", "MAY 14", "MAY 21","MAY 31", "MAY 31"]
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xvalues)
class MyIndexFormatter: IndexAxisValueFormatter {
open override func stringForValue(_ value: Double, axis: AxisBase?) -> String
{
return "\(value)"
}
}