使用 Core Plot 和 Swift 绘制多个散点图

2024-04-10

我正在尝试找到一种方法将两个不同的散点图添加到单个图表中,但到目前为止我还无法做到。我在 Objective-C 中找到了一些示例,但在 Swift 中没有找到任何示例,只有 CorePlot 2.1 版本中的 Scatterplot 示例,但它以两种不同的线条颜色绘制相同的数据。

这是我到目前为止所拥有的(仅绘制了一个散点图):

import UIKit
import CorePlot

class ViewController : UIViewController, CPTScatterPlotDataSource {
    private var scatterGraph : CPTXYGraph? = nil

typealias plotDataType = [CPTScatterPlotField : Double]
private var dataForPlot = [plotDataType]()
@IBOutlet var graphView: UIView!

// MARK: Initialization

override func viewDidAppear(animated : Bool)
{
    super.viewDidAppear(animated)

    // Create graph from theme
    let newGraph = CPTXYGraph(frame: CGRectZero)
    newGraph.applyTheme(CPTTheme(named: kCPTDarkGradientTheme))

    let hostingView = graphView as! CPTGraphHostingView
    hostingView.hostedGraph = newGraph

    // Paddings
    newGraph.paddingLeft   = 10.0
    newGraph.paddingRight  = 10.0
    newGraph.paddingTop    = 10.0
    newGraph.paddingBottom = 10.0

    // Plot space
    let plotSpace = newGraph.defaultPlotSpace as! CPTXYPlotSpace
    //plotSpace.allowsUserInteraction = true
    plotSpace.yRange = CPTPlotRange(location:0, length:10)
    plotSpace.xRange = CPTPlotRange(location:0, length:10)

    // Axes
    let axisSet = newGraph.axisSet as! CPTXYAxisSet

    if let x = axisSet.xAxis {
        x.majorIntervalLength   = 2
        x.orthogonalPosition    = 2.0
        x.minorTicksPerInterval = 2

    }

    if let y = axisSet.xAxis {
        y.majorIntervalLength   = 2
        y.minorTicksPerInterval = 5
        y.orthogonalPosition    = 2.0

        y.delegate = self
    }

    // Create a blue plot area
    let boundLinePlot = CPTScatterPlot(frame: CGRectZero)
    let blueLineStyle = CPTMutableLineStyle()
    blueLineStyle.miterLimit    = 1.0
    blueLineStyle.lineWidth     = 3.0
    blueLineStyle.lineColor     = CPTColor.blueColor()
    boundLinePlot.dataLineStyle = blueLineStyle

    boundLinePlot.identifier    = "Blue Plot"
    boundLinePlot.dataSource    = self
    newGraph.addPlot(boundLinePlot)

    // Add plot symbols
    let symbolLineStyle = CPTMutableLineStyle()
    symbolLineStyle.lineColor = CPTColor.blackColor()
    let plotSymbol = CPTPlotSymbol.ellipsePlotSymbol()
    plotSymbol.fill          = CPTFill(color: CPTColor.blueColor())
    plotSymbol.lineStyle     = symbolLineStyle
    plotSymbol.size          = CGSize(width: 10.0, height: 10.0)

    // Put an area gradient under the plot above
    let areaColor    = CPTColor(componentRed: 0.3, green: 1.0, blue: 0.3, alpha: 0.8)
    let areaGradient = CPTGradient(beginningColor: areaColor, endingColor: CPTColor.clearColor())
    areaGradient.angle = -90.0
    let areaGradientFill = CPTFill(gradient: areaGradient)

    // Add some initial data
    var contentArray = [plotDataType]()

    let plotData1: plotDataType = [.X: 0, .Y: 5]
    let plotData2: plotDataType = [.X: 5, .Y: 0]
    contentArray.append(plotData1)
    contentArray.append(plotData2)
    self.dataForPlot = contentArray

    self.scatterGraph = newGraph


}

// MARK: - Plot Data Source Methods

func numberOfRecordsForPlot(plot: CPTPlot) -> UInt
{
    return UInt(self.dataForPlot.count)
}

func numberForPlot(plot: CPTPlot, field: UInt, recordIndex: UInt) -> AnyObject?
{
    let plotField = CPTScatterPlotField(rawValue: Int(field))

    if let num = self.dataForPlot[Int(recordIndex)][plotField!] {

            return num as NSNumber

    }
    else {
        return nil
    }
}

// MARK: - Axis Delegate Methods

func axis(axis: CPTAxis, shouldUpdateAxisLabelsAtLocations locations: NSSet!) -> Bool
{
    if let formatter = axis.labelFormatter {
        let labelOffset = axis.labelOffset

        var newLabels = Set<CPTAxisLabel>()

        for tickLocation in locations {
            if let labelTextStyle = axis.labelTextStyle?.mutableCopy() as? CPTMutableTextStyle {

                if tickLocation.doubleValue >= 0.0 {
                    labelTextStyle.color = CPTColor.greenColor()
                }
                else {
                    labelTextStyle.color = CPTColor.redColor()
                }

                let labelString   = formatter.stringForObjectValue(tickLocation)
                let newLabelLayer = CPTTextLayer(text: labelString, style: labelTextStyle)

                let newLabel = CPTAxisLabel(contentLayer: newLabelLayer)
                newLabel.tickLocation = tickLocation as! NSNumber
                newLabel.offset       = labelOffset

                newLabels.insert(newLabel)
            }

            axis.axisLabels = newLabels
        }
    }

    return false
}
}

这给了我一行,但我想添加一条包含不同数据的附加行。

有什么建议么?


首先,创建两个CPTScatterPlots (e.g. boundLinePlot1 & boundLinePlot2)并为它们配置不同的颜色和不同的identifier然后添加它们

boundLinePlot1.identifier = "Blue Plot"
boundLinePlot2.identifier = "Green Plot"

newGraph.addPlot(boundLinePlot1) 
newGraph.addPlot(boundLinePlot2)

现在在绘图数据源方法中(numberOfRecordsForPlot & numberForPlot)根据计算返回值plot.identifier

if plot.identifier == "Blue Plot" {
    return dataForPlot1[Int(recordIndex)][plotField!]
} else {
    return dataForPlot2[Int(recordIndex)][plotField!]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Core Plot 和 Swift 绘制多个散点图 的相关文章

随机推荐

  • Perl 正则表达式在相同情况下替换

    如果您在 perl 中有一个简单的正则表达式替换 如下所示 line s JAM AAA g 我将如何修改它 以便它查看匹配并使替换与匹配的大小写相同 例如 JAM 将变成 AAA jam 会变成 aaa 基于 Unicode 的解决方案
  • Git 子模块初始化异步

    当我跑步时git submodule update init第一次在有很多子模块的项目上 这通常需要很多时间 因为大多数子模块都存储在缓慢的公共服务器上 是否可以异步初始化子模块 从 Git 2 8 开始 你可以这样做 git submod
  • PHP/C++:将值注入 EXE 文件 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想动态地将一个值注入到 EXE 文件中 我过去接触过的一家公司给了我一个 EXE 存根 我可以在用户下载它之前使用 PHP 动态注入
  • Qt 构建可以开箱即用静态链接?

    我使用 Qt 构建了一个小型应用程序 事实证明 我需要从头开始重新配置 Qt 以便能够静态链接 我以前做过 我记得那是一个非常漫长的过程 那么有人知道提供开箱即用静态链接功能的 Qt SDK 安装程序吗 此外马丁 贝克特的回答 https
  • 比较 data.table 的两行并仅显示有差异的列[重复]

    这个问题在这里已经有答案了 我得到了一个大的 data table 其中包含不同类型的列 例如数字或字符 例如 data table name c A A val1 c 1 2 val2 c 3 3 cat c u v name val1
  • 在 Apache POI 3.9 中,使用 autosizeColumn 同一列上的图像会被拉伸

    我在 Excel 工作表中有一张图像和图像下方的一些文本 当我将 autoSizeColumn 应用于文本呈现的列时 图像也会被拉伸 我还将锚类型设置为 2 但这并不能保护图像调整大小 我在这里发布一些示例代码 public static
  • Spark:JavaRDD 到 JavaPairRDD<>

    我有一个JavaRDD
  • 透明精灵表有黑色背景

    我正在使用 Python 和 Pygame 开发游戏 我为其中一个敌人创建了一个精灵表 并使其代码正常工作 问题是图像看起来有黑色背景 即使它是透明图像 它的代码是这样的 enemySheet pygame image load resou
  • 使用 selenium Java (Mac OSX) 将 Firefox 浏览器置于前端

    我正在使用三个火狐驱动程序实例进行自动化 我需要将当前活动的火狐浏览器置于前面 因为我正在使用一些机器人类进行某些操作 我曾在 mac 中尝试过针对 google chrome 的 java 脚本警报 相同的操作 并且工作正常 在 Wind
  • 在Android 4.2 API 17上读取Sqlite Cursor carsh

    我有一张桌子145行 当我尝试获取所有数据时crashed on android 4 2 BUT它工作得很好android 4 4 emulator public ArrayList
  • 为什么我不能得到与 GridSearchCV 相同的结果?

    GridSearchCV只返回每个参数化的分数 我还希望看到 Roc 曲线以更好地理解结果 为了做到这一点 我想采用性能最好的模型GridSearchCV并重现这些相同的结果 但缓存概率 这是我的代码 import numpy as np
  • 超低延迟硬实时多线程 x86 代码的意外周期性行为

    我正在具有 RT 优先级的专用 CPU 上循环运行代码以进行多次迭代 并希望长时间观察其行为 我发现代码有一个非常奇怪的周期性行为 简而言之 这就是代码的作用 Arraythread while 1 if flag Multiply mat
  • 通过透明 Windows 窗体防止鼠标点击

    我正在制作一个小工具 用于在浮动侧边栏中切换 笔 按钮后用鼠标在屏幕上绘图 我已经做到了这一点 请不要笑 方法是使用最顶层的窗口窗体及其背景 因为它的透明键覆盖整个屏幕 当我处于绘图模式时 我需要使鼠标不会点击表单到下面的内容上 我尝试按照
  • 如何搜索一长串 JavaScript 对象以查找“sent: 0”的第一个实例

    这里有一个主要的循环问题 我的数据如下所示 var mailouts signUp date sent 1 lesson1 sent 1 time 20 lesson2 sent 0 time 20 lesson3 sent 0 time
  • Haxe - 创建 C++ 独立可执行文件

    我编写了一个 haxe 程序 尝试与远程服务器进行通信 我能够成功编译到 C 目标 该可执行文件在我的系统上运行得很好 但是 当我尝试在另一个 Windows 盒子上运行相同的命令时 它失败并出现以下错误 错误 无法加载模块 std soc
  • 是否可以重新排序或忽略控制器路由中的参数?

    问题标题是我能想到的最明确的 但为了清楚起见 这里有一个用例 示例 假设我定义以下路线来显示一篇文章 Route get article slug id ArticleController show class ArticleControl
  • 如何实现向后兼容的soap webservice(基于java)?

    我们的产品之一使用合同最后方法发布网络服务 这已经成为一个真正的问题 因为一旦我们发布产品的新版本 我们所有的客户 ws 客户 都必须重建他们的客户端应用程序 这是由于所有名称空间更改都是自动生成的 wsdls 的成本 我们使用 Axis1
  • S3和EMR数据局部性[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 MapReduce 和 HDFS 的数据局部性非常重要 Spark HBase 也是如此 我一直在研究 AWS 以及在云中部署集群时的两个选项
  • JavaScript 丢失带有私有/公共属性的“this”对象引用

    我在运行以下页面时出现以下错误 this testpublic 不是一个函数 test function var testprivate function this testpublic this testpublic function c
  • 使用 Core Plot 和 Swift 绘制多个散点图

    我正在尝试找到一种方法将两个不同的散点图添加到单个图表中 但到目前为止我还无法做到 我在 Objective C 中找到了一些示例 但在 Swift 中没有找到任何示例 只有 CorePlot 2 1 版本中的 Scatterplot 示例