Coreplot折线图线不可见

时间:2017-09-19 17:00:40

标签: 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
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我检查了你的代码。

@foreach($company as $cmp)
<tr>
  <td>{{ $cmp->number }}</td>
  <td>{{ $cmp->name}}</td>
  <td>  <a type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" </td
</tr>

@endforeach

根据您的代码的上述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)
]