核心图未正确显示

问题描述

我是iOS上使用核心绘图的新手,我能够毫无问题地设置图表。但是,当我将xaxis和yaxis值添加到图形中时,就会根据下面的图片获得

我尝试更改,添加自定义标签将无法正常工作。

与此同时,我尝试使用CPTScaleTypeLog,但没有任何运气

override func viewDidLoad() {
    super.viewDidLoad()

    print(arrayPressure);
    print(array2)
    
     initPlot()
}


    func initPlot() {
        configurehostingView()
        configureGraph()
        configureChart()
        configureAxes()
    }

    func configurehostingView() {
        hostingView.allowPinchScaling = false
    }

    func configureGraph() {
        // 1 - Create the graph
        let graph = CPTXYGraph(frame: hostingView.bounds)
        graph.plotAreaFrame?.masksToBorder = false
        hostingView.hostedGraph = graph

        // 2 - Configure the graph
        //graph.apply(CPTTheme(named: CPTThemeName.plainWhiteTheme))
        //graph.fill = CPTFill(color: CPTColor.clear())
        graph.paddingBottom = 30.0
        graph.paddingLeft = 30.0
        graph.paddingTop = 0.0
        graph.paddingRight = 0.0
        

        // 3 - Set up styles
        let titleStyle = CPTMutableTextStyle()
        titleStyle.color = CPTColor.black()
        titleStyle.fontName = "HelveticaNeue-Bold"
        titleStyle.fontSize = 16.0
        titleStyle.textAlignment = .center
        graph.titleTextStyle = titleStyle

        let title = "Pressure Buildup Chart"
        graph.title = title
        graph.titlePlotAreaFrameAnchor = .top
        graph.titledisplacement = CGPoint(x: 0.0,y: 0.0)

        // 4 - Set up plot space
        let xMin = 1.0
        let xMax = Double(array2.max()!) + 100
        let yMin = Double(minY) - 100
        let yMax = Double(maxY) + 100
        guard let plotSpace = graph.defaultPlotSpace as? CPTXYPlotSpace else { return }
        plotSpace.xrange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(xMin),lengthDecimal: CPTDecimalFromDouble(xMax - xMin))
        plotSpace.yRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(yMin),lengthDecimal: CPTDecimalFromDouble(yMax - yMin))



           
//            let axisSet = graph.axisSet as! CPTXYAxisSet
//
//            if let x = axisSet.xAxis {
//                x.axisLinestyle       = nil
//                x.majorTickLinestyle  = nil
//                x.minorTickLinestyle  = nil
//                x.majorIntervalLength = 5.0
//                x.orthogonalPosition  = 0.0
//                x.title               = "X Axis"
//                x.titleLocation       = 7.5
//                x.titleOffset         = 55.0
//
//                // Custom labels
//                x.labelRotation  = CGFloat(.pi / 4.0)
//                x.labelingPolicy = .none
//
//                let customTickLocations = [1,10,100,1000]
//                let xAxisLabels         = ["1","10","100","1000"]
//
//                var labelLocation = 0
//                var customLabels = Set<CPTAxisLabel>()
//                for tickLocation in customTickLocations {
//                    let newLabel = CPTAxisLabel(text:xAxisLabels[labelLocation],textStyle:x.labelTextStyle)
//                    labelLocation += 1
//                    newLabel.tickLocation = tickLocation as NSNumber
//                    newLabel.offset       = x.labelOffset + x.majorTickLength
//                    newLabel.rotation     = CGFloat(.pi / 4.0)
//                    customLabels.insert(newLabel)
//                }
//
//                x.axisLabels = customLabels
//            }

    }

    func configureChart() {
        // 1 - Set up the plot
        plot = CPTScatterPlot()

        // 2 - Set up style
        let plotLinestile = CPTMutableLinestyle()
        plotLinestile.linewidth = 1
        plotLinestile.lineColor = CPTColor.black()
        plot.dataLinestyle = plotLinestile

        // 3- Add plots to graph
        guard let graph = hostingView.hostedGraph else { return }
        plot.dataSource = self
        plot.delegate = self
        graph.add(plot,to: graph.defaultPlotSpace)
    }

    func configureAxes() {
                }
    }



extension buildupTestViewController: CPTScatterPlotDataSource,CPTScatterPlotDelegate {
    func numberOfRecords(for plot: CPTPlot) -> UInt {
        // number of points
        return UInt(array2.count)
    }

  
    func number(for plot: CPTPlot,field: UInt,record: UInt) -> Any? {
        switch CPTScatterPlotField(rawValue: Int(field))! {
        case .X:
            let xVal = self.array2[Int(record)]
            return xVal
        case .Y:
            let yVal = self.arrayPressure[Int(record)]
            return yVal
        }
    }

}

以下是xaxis和yaxis对应

[3553.0,3572.0,3607.0,3632.0,3652.0,3684.0,3707.0,3724.0,3737.0,3768.0,3777.0,3781.0,3782.0,3786.0,3787.0]
[961.01465,385.0,193.0,129.0,97.0,65.0,49.0,39.4,33.0,20.2,10.6,6.3333335,5.0,3.0,2.3333333]

enter image description here

这就是我想要达到的目标

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)