SwiftUIGraphPlotLibrary

使用 SwiftUI 的图形绘制库

平台

仅支持 iOS v13 及以上版本

用法

添加 Package 仓库

要将 package 依赖项添加到你的 Xcode 项目,选择 File > Swift Packages > Add Package Dependency

导入 package

import SwiftUIGraphPlotLibrary
-- 折线图 --
struct LinePlot: View {
var body: some View {
    let plotset:[PlotData] = [PlotData(x: 0.0,y: 0.0),PlotData(x: 1.0,y: 2.0),PlotData(x: 2.0,y: 5.0),
                              PlotData(x: 3.0,y: 3.0),PlotData(x: 4.0,y: 8.0),PlotData(x: 5.0,y: 10.0),
                              PlotData(x: 6.0,y: 25.0),PlotData(x: 7.0,y: 21.0),PlotData(x: 8.0,y: 23.0),
                              PlotData(x: 9.0,y: 22.0),PlotData(x: 9.6,y: 21.0),PlotData(x: 10,y: 30.0)]

    let plotView = GraphPlot(type: .linePlot, color: .orange)
    let frameSize = CGSize(width: 300, height: 200)
        
    return GraphView(dataSet: plotset, plotType: plotView, frameSize: frameSize, xTicks: true, yTicks: true)
             .frame(width: 350, height: 250, alignment: .center)
    }
}

alt tag

-- 垂直柱状图 --

struct BarPlot:View {
    var body:some View{
        let plotset:[PlotData] = [PlotData(x: 0.0,y: 0.0),PlotData(x: 1.0,y: 2.0),PlotData(x: 2.0,y: 5.0),
                                  PlotData(x: 3.0,y: 3.0),PlotData(x: 4.0,y: 8.0),PlotData(x: 5.0,y: 10.0),
                                  PlotData(x: 6.0,y: 25.0),PlotData(x: 7.0,y: 21.0),PlotData(x: 8.0,y: 23.0),
                                  PlotData(x: 9.0,y: 22.0),PlotData(x: 9.6,y: 21.0),PlotData(x: 10,y: 30.0)]
            
        let plotView = GraphPlot(type: .verticalBar, color: .blue)
            let frameSize = CGSize(width: 300, height: 200)
            
        return GraphView(dataSet: plotset, plotType: plotView, frameSize: frameSize, xTicks: true, yTicks: true)
               .frame(width: 350, height: 250, alignment: .center)
        }
}

alt tag

-- 气泡图 --

import SwiftUI
import SwiftUIGraphPlotLibrary

struct exampleBuble: View {
var body: some View {

  let plotset:[PlotData] = [PlotData(x: 1.0,y: 2.0),PlotData(x: 2.0,y: 3.0),PlotData(x: 3.0,y: 2.0),
                            PlotData(x: 4.0,y: 8.0),PlotData(x: 5.0,y: 30.0),PlotData(x: 6.0,y: 25.0),
                            PlotData(x: 7.0,y: 24.0),PlotData(x: 8.0,y: 23.0),PlotData(x: 9.0,y: 22.0),
                            PlotData(x: 9.6,y: 21.0)]

  let bublePlot = GraphPlot(type: .circlePlot, color: .blue)
      .setCircleRadiusFunc{ index in 
                            plotset[index].getY() * 1.4}
      .setHueDegreeFunc{ index in
                            return Double(plotset[index].getY() * 90 / 23)}
  let frameSize = CGSize(width: 300, height: 200)

  return GraphView(dataSet: plotset, plotType: bublePlot, frameSize: frameSize, xTicks: true, yTicks: true)
        .frame(width: 350, height: 250, alignment: .center)
  
 }
}

alt tag

-- 多重图 --

struct MultiPlot:View {
    var body:some View {
        let plotset:[PlotData] = [PlotData(x: 0.0,y: 0.0),PlotData(x: 1.0,y: 2.0),PlotData(x: 2.0,y: 5.0),
                                  PlotData(x: 3.0,y: 3.0),PlotData(x: 4.0,y: 8.0),PlotData(x: 5.0,y: 10.0),
                                  PlotData(x: 6.0,y: 25.0),PlotData(x: 7.0,y: 21.0),PlotData(x: 8.0,y: 23.0),
                                  PlotData(x: 9.0,y: 22.0),PlotData(x: 9.6,y: 21.0),PlotData(x: 10,y: 30.0)]
        
        let frameSize = CGSize(width: 300, height: 200)
        let linePLot = GraphPlot(type: .linePlot, color: .orange)
        let barPlot = GraphPlot(type: .verticalBar, color: .blue)
        let bubblePlot = GraphPlot(type: .circlePlot, color: .blue)
            .setCircleRadiusFunc{ index in plotset[index].getY() * 1.4}
            .setHueDegreeFunc{ index in
                                return Double(plotset[index].getY() * 90 / 23)
                              }
        
        let plotsSet = [linePLot, barPlot,bubblePlot]
        
        return GraphView(dataSet: plotset, plotTypes: plotsSet, frameSize: frameSize, xTicks: true, yTicks: true)
               .frame(width: 350, height: 250, alignment: .center)
    }
}

alt tag

-- 使用两个 x,y 数据数组进行绘制 --

从 1.0.4 版本及更高版本开始,可以使用两个独立的 x, y 数据数组进行绘制。 支持 Int、Double、Float 和 CGFloat 作为数组元素的类型。

struct arrayPlotInt: View {
    var body: some View {
        
        let xarryInt5:[Int] = [1,2,3,4,5]
        let yarryInt5:[Int] = [1,4,9,16,25]
                
        let bublePlot = GraphPlot(type: .circlePlot, color: .blue)
        
        let frameSize = CGSize(width: 300, height: 200)
        return GraphView(xArray:xarryInt5,yArray:yarryInt5, plotType: bublePlot, frameSize: frameSize, xTicks: true, yTicks: true, xPlotAreaFactor: 0.9)
            .frame(width: 350, height: 250, alignment: .center)
    }
}

alt tag

-- 散点图 --

struct scatterPlot: View {
    var body: some View {
        
        var xarry:[Double] = []
        var yarry:[Double] = []
                
        for _ in 1...100 {
            let x = Double.random(in: 0.0...10)
            let y = x * (1 + Double.random(in: 0.0...0.4))
            xarry.append(x)
            yarry.append(y)
        }
        
        let bublePlot = GraphPlot(type: .circlePlot, color: .blue, circleRadius:4)
        
        let frameSize = CGSize(width: 300, height: 200)
        return GraphView(xArray:xarry,yArray:yarry, plotType: bublePlot, frameSize: frameSize, xTicks: true, yTicks: true, xPlotAreaFactor: 0.9)
            .frame(width: 350, height: 250, alignment: .center)
    }
}

alt tag

贡献

此程序仍在开发中。但是,如果您有任何疑问或需要更多信息,请提出 issue。

非常感谢您的贡献。请帮助我!!!

致谢

此程序由 Kanshu Yokoo 创建

许可

此程序采用 MIT 许可证。有关更多信息,请参见 LICENSE 文件。