ios - Coreplot折线图线不可见

标签 ios swift core-plot

我试图用核心图制作折线图,但我遇到了问题。虽然 number(for:...) 函数(最后几行)有效并且数据已被检索,但模拟器中没有一行。我已经尝试这样做了 60 个小时,但我不知道这里出了什么问题。并且没有更多的细节。

这是我的代码:

import UIKit
import CorePlot

class dottedLine: UIViewController {
    @IBOutlet var hostView: CPTGraphHostingView!

    var plot: CPTScatterPlot!

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        initPlot()
    }

    let xValues: [NSNumber] = [1,2,3,4]
    let yValues: [NSNumber] = [1,5,4,3]

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

    func configureHostView() {
        hostView.allowPinchScaling = false
    }

    func configureGraph() {
        // 1 - Create the graph
        let graph = CPTXYGraph(frame: hostView.bounds)
        graph.plotAreaFrame?.masksToBorder = false
        hostView.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 = "Just title"
        graph.title = title
        graph.titlePlotAreaFrameAnchor = .top
        graph.titleDisplacement = CGPoint(x: 0.0, y: -16.0)

        // 4 - Set up plot space
        let xMin = 0.0
        let xMax = 5.0
        let yMin = 0.0
        let yMax = 15.0
        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))
    }

    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 = hostView.hostedGraph else { return }
        plot.dataSource = self
        plot.delegate = self
        graph.add(plot, to: graph.defaultPlotSpace)
    }

    func configureAxes() {
    }
}

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

    func scatterPlot(_ plot: CPTScatterPlot, plotSymbolWasSelectedAtRecord idx: UInt, with event: UIEvent) {
    }

    func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? {
        switch CPTScatterPlotField(rawValue: Int(field))! {
        case .X:
            return 2 as NSNumber
        case .Y:
            return 3 as NSNumber
        }
    }
}

最佳答案

我检查了你的代码。

func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? {
    switch CPTScatterPlotField(rawValue: Int(field))! {
    case .X:
        return 2 as NSNumber
    case .Y:
        return 3 as NSNumber
    }
}

根据您代码的上述CPTScatterPlotDataSource,您为每条记录提供的数据是相同的数据点,即(X: 2, Y: 3)。鉴于您为所有记录赋予相同的点,散点图的最终数据集将是:

[
  (X: 2, Y: 3),
  (X: 2, Y: 3),
  (X: 2, Y: 3),
  (X: 2, Y: 3)
]

你不能用这个数据集得到一条线,它只是一个点。

尝试为不同的记录提供不同的数据,您会看到一条线。你可以看下面的例子来理解

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

关于ios - Coreplot折线图线不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46306033/

相关文章:

iOS 10.2 - 使用手电筒激活 Retina Flash 而不是 Back LED

ios - CorePlot 使用从 Web 服务检索到的数据绘制图形

iphone - 尝试覆盖 AppDelegate 实现文件中的 UINavigationBar

ios - 向上滑动(将应用程序置于后台)并且视频正在自定义 AVPLayer 中播放时,如何停止画中画?

swift - 有没有办法让一个函数在不重新指定所有参数的情况下调用自己?

ios - UNUserNotificationCenterDelegate 和 didReceiveRemoteNotification 的区别

ios - 在 Swift 中设置 Coreplot Axis 的 Axis 约束

objective-c - 核心剧情: Custom labels on the x-axis for bar charts

ios - 通过数据库搜索值

iphone - 尝试将代码抽象到库中,但我的代码无法正常工作,我不知道为什么