logo

Swift Platform CocoaPods GitHub release (latest SemVer) GitHub

DPCharts 是一个基于 Swift 的轻量级框架,专门用于在 iOS 上渲染图表。其主要目标是创建一个用户友好的图表库,遵循 iOS 委托模式方法。框架中的每个图表仅负责呈现数据,而不是管理数据本身。 要管理数据,您需要为图表提供一个数据源对象(一个符合图表数据源协议的对象)。 此外,图表还会管理手势交互,触摸手势事件会传递到配置的委托(一个符合图表委托协议的对象)。

目录

功能亮点

DPCharts 提供多种功能和广泛的自定义选项。 以下是一个简明的列表,展示了它的一些功能

可用图表

柱状图

柱状图是一种使用矩形条表示不同类别或变量的数据的图形表示。每个条的长度对应于它所代表的类别的价值或频率。

BarChart

let barChartView = DPBarChartView()
barChartView.datasource = self
barChartView.barStacked = false
barChartView.xAxisTitle = "Title of X-axis"
barChartView.yAxisInverted = false
barChartView.yAxisMarkersWidthRetained = true
barChartView.yAxisTitle = "Title of Y-axis"

堆叠柱状图

堆叠柱状图是一种图形表示,它使用矩形条来显示堆叠在一起的多个类别或变量。 每个条段代表一个特定的类别或子组,堆叠条的总高度对应于所代表的变量的累积值或频率。

StackedBarChart

let barChartView = DPBarChartView()
barChartView.datasource = self
barChartView.barStacked = true
barChartView.xAxisTitle = "Title of X-axis"
barChartView.yAxisInverted = false
barChartView.yAxisMarkersWidthRetained = true
barChartView.yAxisTitle = "Title of Y-axis"

折线图

折线图是一种数据的图形表示,它使用线条连接数据点,通常显示特定时期内两个或多个变量之间的关系或趋势。

LineChart

let lineChartView = DPLineChartView()
lineChartView.datasource = self
lineChartView.bezierCurveEnabled = false
lineChartView.areaEnabled = false
lineChartView.xAxisTitle = "Title of X-axis"
lineChartView.yAxisInverted = false
lineChartView.yAxisMarkersWidthRetained = true
lineChartView.yAxisTitle = "Title of Y-axis"

带区域的折线图

带区域的折线图是一种图形表示,它结合了传统折线图的特性,并在线条下方添加了阴影区域。 该线条表示变量随时间或连续尺度变化的趋势或关系,而线条下方的阴影区域表示累积值或频率。

LineChartArea

let lineChartView = DPLineChartView()
lineChartView.datasource = self
lineChartView.bezierCurveEnabled = false
lineChartView.areaEnabled = true
lineChartView.xAxisTitle = "Title of X-axis"
lineChartView.yAxisInverted = false
lineChartView.yAxisMarkersWidthRetained = true
lineChartView.yAxisTitle = "Title of Y-axis"

贝塞尔曲线折线图

贝塞尔曲线折线图是一种特殊的折线图图形表示,它使用平滑的曲线连接数据点,显示变量随时间或连续尺度变化的趋势或关系。 贝塞尔曲线是一种数学曲线,可在数据点之间提供平滑且视觉上吸引人的插值。

LineChartBezierCurve

let lineChartView = DPLineChartView()
lineChartView.datasource = self
lineChartView.bezierCurveEnabled = true
lineChartView.areaEnabled = false
lineChartView.xAxisTitle = "Title of X-axis"
lineChartView.yAxisInverted = false
lineChartView.yAxisMarkersWidthRetained = true
lineChartView.yAxisTitle = "Title of Y-axis"

带贝塞尔曲线和区域的折线图

带有贝塞尔曲线和区域的折线图将贝塞尔曲线的平滑曲线与通过曲线下方的阴影区域添加的累积值或频率的视觉表示相结合。

LineChartBezierCurveArea

let lineChartView = DPLineChartView()
lineChartView.datasource = self
lineChartView.bezierCurveEnabled = true
lineChartView.areaEnabled = true
lineChartView.xAxisTitle = "Title of X-axis"
lineChartView.yAxisInverted = false
lineChartView.yAxisMarkersWidthRetained = true
lineChartView.yAxisTitle = "Title of Y-axis"

散点图

散点图,也称为散点图,是一种图形表示,它在二维坐标系上将各个数据点显示为点。 它用于显示两个变量之间的关系或相关性。 图表上的每个数据点表示所分析的两个变量的特定值对。

ScatterChart

let scatterChartView = DPScatterChartView()
scatterChartView.datasource = self
scatterChartView.xAxisTitle = "Title of X-axis"
scatterChartView.yAxisInverted = false
scatterChartView.yAxisMarkersWidthRetained = true
scatterChartView.yAxisTitle = "Title of Y-axis"

饼图

饼图是一种圆形图形表示,它以饼图切片的形式直观地显示数据。 图表中的每个切片代表一个不同的类别或变量,每个切片的大小对应于它所代表的整体的比例或百分比。

PieChart

let pieChartView = DPPieChartView()
pieChartView.datasource = self
pieChartView.donutEnabled = false

甜甜圈图 (饼图)

甜甜圈图是一种圆形图形表示,类似于饼图,但中心是空心的。 它是通过在饼图中间创建一个孔,从而形成环状结构而形成的。

PieChartDonut

let pieChartView = DPPieChartView()
pieChartView.datasource = self
pieChartView.donutEnabled = true
pieChartView.donutTitle = "Title"
pieChartView.donutSubtitle = "Brief description"
pieChartView.donutVerticalSpacing = 4.0

热力图

热力图是一种图形表示,它使用颜色编码的单元格或矩形以矩阵或表格格式显示数据。 它对于可视化和分析具有多个变量或维度的数据集特别有用。 在热力图中,矩阵中的每个单元格代表变量或类别的特定组合。 每个单元格的颜色由它所代表的数据的值或大小决定。 通常,使用颜色渐变,其中颜色的较浅或较深阴影分别表示较高或较低的值。

Heatmap

let heatmapView = DPHeatMapView()
heatmapView.datasource = self
heatmapView.xAxisInverted = false
heatmapView.yAxisInverted = false

带单元格文本的热力图

单元格内带有文本的热力图是一种图形表示,它结合了热力图的视觉元素以及每个单元格中包含的文本信息。 它通常用于显示和分析涉及数值和相应标签或类别的数据。

Heatmap

let heatmapView = DPHeatMapView()
heatmapView.datasource = self
heatmapView.cellTextEnabled = true
heatmapView.xAxisInverted = false
heatmapView.yAxisInverted = false

安装

Cocoapods

将依赖项添加到 Podfile 中的 DPCharts 框架

pod 'DPCharts', '~> 1.3.0'

Swift Package Manager

将其作为依赖项添加到 Swift Package 中

dependencies: [
    .package(url: "https://github.com/danielepantaleone/DPCharts.git", .upToNextMajor(from: "1.3.0"))
]

运行演示

DPCharts 包含一个演示应用程序,展示了它提供的所有图表功能。

文档

该库目前缺少任何形式的结构化文档。 但是,可以直接在 Xcode 快速帮助中访问 Swift 文档。 目前,没有必要提供全面的文档,因为使用 DPCharts 的视图只需将所需的图表子视图添加到您的 ViewController 层次结构中,并配置数据源和委托(类似于 UITableViewUICollectionView 的用法)。 也支持 Storyboard 原位配置。

如果您在使用 DPCharts 时需要帮助,请打开一个 issue

贡献

如果您喜欢这个项目,您可以通过以下方式贡献它

许可证

MIT License

Copyright (c) 2023 Daniele Pantaleone

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.