【问题标题】:How to make a line graph using CorePlot framework and swift 3?如何使用 CorePlot 框架和 swift 3 制作折线图?
【发布时间】:2017-09-15 17:46:18
【问题描述】:

我不明白如何使用 CorePlot 2.2 和 Swift 3(Xcode 8、iOS 10)制作线图。

有人能解释一下怎么做吗?

特别是,我不明白最后一个函数numbers(第 97-103 行(最后几行))是如何工作的:

    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 numbers(for plot: CPTPlot, field fieldEnum: UInt, recordIndexRange indexRange: NSRange) -> [Any]? {
        print("xxxxxxx")
        switch CPTScatterPlotField(rawValue: Int(fieldEnum))! {
        case .X:
            return xValues[index] as NSNumber

        case .Y:
            return yValues[indexRange] as NSNumber
        }

    } */

   /* func symbols(for plot: CPTScatterPlot, recordIndexRange indexRange: NSRange) -> [CPTPlotSymbol]? {
        return xValues
    } */

    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
        }
    }
}

【问题讨论】:

    标签: ios swift core-plot


    【解决方案1】:

    对于散点图,此方法将针对每个索引处的 x 值和 y 值调用一次。

    这是 DatePlot 示例应用程序中的方法:

    func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any?
    {
        switch CPTScatterPlotField(rawValue: Int(field))! {
        case .X:
            return (oneDay * Double(record)) as NSNumber
    
        case .Y:
            return self.plotData[Int(record)] as NSNumber
        }
    }
    

    【讨论】:

    • 我试图插入这个函数(改变它的返回值)但是这个函数甚至没有被调用
    • 确保设置图表的datasource
    • 它是(视图控制器的)类的协议吗?
    • 这是情节上的财产。您已经在视图控制器中实现了数据源协议,所以在配置绘图时只需将其设置为self
    • 对不起,我还是不明白该怎么做
    猜你喜欢
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多