I created a scatter plot in graph CPTXYGraph, and then passed this graph to CPTGraphHostingView, here's the code:
func makeGraphHostingView() -> CPTGraphHostingView {
let hostingView = CPTGraphHostingView()
let graph = CPTXYGraph()
graph.axisSet!.isHidden = true
graph.plotAreaFrame!.paddingTop = 0.0
graph.plotAreaFrame!.paddingRight = 0.0
graph.plotAreaFrame!.paddingBottom = 0.0
graph.plotAreaFrame!.paddingLeft = 0.0
hostingView.hostedGraph = graph
let lineStyle = CPTMutableLineStyle()
lineStyle.lineColor = CPTColor.white()
lineStyle.lineWidth = 3.0
let xAxisMin = 0.0
let xAxisMax = 100.0
let yAxisMin = 0.0
let yAxisMax = 100.0
let plotSpace = graph.defaultPlotSpace as! CPTXYPlotSpace
plotSpace.xRange = CPTPlotRange(locationDecimal: Decimal(xAxisMin), lengthDecimal: Decimal(xAxisMax - xAxisMin))
plotSpace.yRange = CPTPlotRange(locationDecimal: Decimal(yAxisMin), lengthDecimal: Decimal(yAxisMax - yAxisMin))
let plot = CPTScatterPlot()
plot.dataSource = self
plot.dataLineStyle = lineStyle
let areaFillColor = UIColor.white.withAlphaComponent(0.05)
let fill = CPTFill(color: CPTColor(cgColor: areaFillColor.cgColor))
plot.areaFill = fill
plot.areaBaseValue = NSNumber(0.0)
graph.add(plot)
return hostingView
}
hostingView is pinned to the superview's leading, trailing, top and bottom anchors with 0.0 constant (watch pic. 1):
I want the graph to resize exactly to the same bounds as hostingView, but I've got no idea how to do that, graph paddings didn't make any help for me here. Any ideas?
You need to set the padding on the graph
, too.
graph.paddingTop = 0.0
graph.paddingRight = 0.0
graph.paddingBottom = 0.0
graph.paddingLeft = 0.0