Я пытаюсь создать комбинированный график, используя danielgindi / Charts .
Но когда я создаю гистограммы, я не могу добавить правильные поля с левой и правой стороны графика, и поэтому он обрезается с обеих сторон.
Это то, что я получаю. На графике значения оси X не выровнены по графику, а левая и правая части обрезаются. Мне нужно правильно высветить X ZXIS и добавить поля слева и справа.
Код, который я использую, выглядит следующим образом
override func viewDidLoad() {
super.viewDidLoad()
thirdHolderView.delegate = self
thirdHolderView.chartDescription?.enabled = false
thirdHolderView.drawBarShadowEnabled = false
thirdHolderView.highlightFullBarEnabled = false
thirdHolderView.drawOrder = [DrawOrder.line.rawValue, DrawOrder.bar.rawValue]
let legendObj = thirdHolderView.legend
legendObj.wordWrapEnabled = false
legendObj.horizontalAlignment = .center
legendObj.verticalAlignment = .bottom
legendObj.orientation = .horizontal
legendObj.drawInside = false
//Right Y axis
let rightAxis = thirdHolderView.rightAxis
rightAxis.resetCustomAxisMin() //By calling this method, any custom minimum value that has been previously set is reseted, and the calculation is done automatically.
rightAxis.resetCustomAxisMax() //By calling this method, any custom maximum value that has been previously set is reseted, and the calculation is done automatically.
//Left Y axis
let leftAxis = thirdHolderView.leftAxis
leftAxis.resetCustomAxisMin() //By calling this method, any custom minimum value that has been previously set is reseted, and the calculation is done automatically.
leftAxis.resetCustomAxisMax() //By calling this method, any custom maximum value that has been previously set is reseted, and the calculation is done automatically.
let xAxis = thirdHolderView.xAxis
xAxis.labelPosition = .bottom
xAxis.resetCustomAxisMax() //By calling this method, any custom maximum value that has been previously set is reseted, and the calculation is done automatically.
xAxis.resetCustomAxisMin() //By calling this method, any custom minimum value that has been previously set is reseted, and the calculation is done automatically.
xAxis.granularity = 1.0
xAxis.labelRotationAngle = 315.0
xAxis.centerAxisLabelsEnabled = true
xAxis.valueFormatter = self
self.updateChartData()
}
override func updateChartData() {
if self.shouldHideData {
thirdHolderView.data = nil
return
}
self.setChartData()
}
func setChartData() {
let dataChartsMaster = CombinedChartData()
dataChartsMaster.barData = generateBarData()
thirdHolderView.doubleTapToZoomEnabled = false
thirdHolderView.pinchZoomEnabled = true
thirdHolderView.data = dataChartsMaster
}
func generateBarData() -> BarChartData {
var salesEntries : [BarChartDataEntry] = []
var marginEntries : [BarChartDataEntry] = []
for i in 0..<yearsXAxis.count
{
let barEntry1 = BarChartDataEntry(x: (Double(yearsXAxis[i])!), y: Double(unitsSoldJ[i]))
salesEntries.append(barEntry1)
let barEntry2 = BarChartDataEntry(x: (Double(yearsXAxis[i])!), y: Double(unitsBoughtJ[i]))
marginEntries.append(barEntry2)
}
//Barchart 1
let chartDataSet1 = BarChartDataSet(entries: salesEntries, label: "Sales")
chartDataSet1.setColor(UIColor(red: 60/255, green: 220/255, blue: 78/255, alpha: 1))
chartDataSet1.valueTextColor = UIColor(red: 60/255, green: 220/255, blue: 78/255, alpha: 1)
//Barchart 2
let chartDataSet2 = BarChartDataSet(entries: marginEntries, label: "Margin")
chartDataSet2.setColor(UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1))
chartDataSet2.valueTextColor = UIColor(red: 60/255, green: 220/255, blue: 78/255, alpha: 1)
let groupSpace = 0.1
let barSpace = 1.05
let barWidth = 0.25
let data = BarChartData(dataSets: [chartDataSet1, chartDataSet2])
data.barWidth = barWidth;
data.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
return data
}
Может ли кто-нибудь помочь мне понять, где я нахожусьидет не так?
Заранее спасибо.