简体   繁体   中英

How to reduce gap between CPTXYGraph and CPTGraphHostingView?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM