画直方图

作者: _浅墨_ | 来源:发表于2022-04-30 21:47 被阅读0次
效果:
直方图
代码:
//
//  BarGraphView.swift
//  bargraph
//
//

import UIKit

@IBDesignable
class BarGraphView: UIView {
    
    struct BarGraphData {
        
        var label: String
        
        var value: Int
        
    }
    
    @IBInspectable var barWidth: CGFloat = 20.0
    
    let barGraphData: [BarGraphData] = [BarGraphData(label: "Mon", value: 10),
                                        BarGraphData(label: "Tue", value: 7),
                                        BarGraphData(label: "Wed", value: 2),
                                        BarGraphData(label: "Thu", value: 15),
                                        BarGraphData(label: "Fri", value: 6)]
    
    let marginFactor: CGFloat = 0.1
    
    override func draw(_ rect: CGRect) {
        // Drawing code
        
        let margin: CGFloat = marginFactor * bounds.width
        
        let graphMidPoint = Int((bounds.height - (2 * margin)) / 2)
        
        //draw horizontal lines
        
        let linePath = UIBezierPath()
        
        linePath.lineWidth = 0.5
        
        linePath.move(to: CGPoint(x: 0, y: margin))
        
        linePath.addLine(to: CGPoint(x: bounds.width, y: margin))
        
        linePath.move(to: CGPoint(x: 0, y: CGFloat(graphMidPoint) + margin))
        
        linePath.addLine(to: CGPoint(x: bounds.width, y: CGFloat(graphMidPoint) + margin))
        
        linePath.move(to: CGPoint(x: 0, y: bounds.height - margin))
        
        linePath.addLine(to: CGPoint(x: bounds.width, y: bounds.height - margin))
        
        linePath.stroke()
        
        let numberOfBars: Int = barGraphData.count
        
        let numberOfGaps: Int = numberOfBars - 1
        
        let highestDataValue = (barGraphData.max { $0.value < $1.value })!.value
        
        let barGap: CGFloat = (bounds.width - (barWidth * CGFloat(numberOfBars)) - (margin * 2)) / CGFloat(numberOfGaps)
        
        let font = UIFont(name: "Avenir-Medium", size: 14)!
        
        //vertical labels
        
        let labelAttributes = [
            NSAttributedString.Key.font: font,
            NSAttributedString.Key.foregroundColor: UIColor.black]
        
        let verticalTopLabel = String(highestDataValue) as NSString
        
        verticalTopLabel.draw(at: CGPoint(x: 0, y: margin), withAttributes: labelAttributes)
        
        let midDataValue = Int(highestDataValue / 2)
        
        let verticalMidLabel = String(midDataValue) as NSString
        
        verticalMidLabel.draw(at: CGPoint(x: 0, y: CGFloat(graphMidPoint) + margin), withAttributes: labelAttributes)
        
        let verticalBottomLabel = String(0) as NSString
        
        verticalBottomLabel.draw(at: CGPoint(x: 0, y: bounds.height - margin), withAttributes: labelAttributes)
        
        for i in 0...barGraphData.count - 1 {
            
            let xPos = CGFloat(i) * (barWidth + barGap) + margin
            
            let barHeight: CGFloat = CGFloat(barGraphData[i].value) / CGFloat(highestDataValue) * (bounds.height - margin * 2)
            
            let yPos = bounds.height - barHeight - margin
            
            let bar = UIBezierPath(rect: CGRect(x: xPos, y: yPos, width: barWidth, height: barHeight))
            
            let barColor = UIColor(red: randomColorValue(), green: randomColorValue(), blue: randomColorValue(), alpha: 1.0)
            
            barColor.setFill()
            
            bar.fill()
            
            let label = barGraphData[i].label as NSString
            
            let textPos = CGPoint(x: xPos, y: bounds.height - margin)
            
            label.draw(at: textPos, withAttributes: labelAttributes)
            
        }
        
    }
    
    func randomColorValue() -> CGFloat {
        
        return CGFloat(arc4random()) / CGFloat(UInt32.max)
        
    }
    

}

相关文章

  • [Statistics]Matlab画直方图

    1.画直方图 先从外部导入数据: 画绝对频率的直方图,在画之前先解释什么叫直方图:直方图是反应数据在某一区间内出现...

  • 画直方图

    效果: 代码:

  • R 笔记(基础内容)

    barplot(a) #直方图 数值型变量--每一个独立 VS hist(a) #画直方图barplot(t...

  • seaborn画直方图

    设置hist-bins的个数的参数为 bins, 颜色参数为color, alpha是透明度参数(这里单图形可省略...

  • R画直方图

    来源:https://www.cnblogs.com/xudongliang/p/6913363.html his...

  • python画hist直方图

    一、图形选择 简单说下图形选择啦,通常我们最常用的图形是折线图、扇形图、条形图,它们的功能简单概括为:折线图:表示...

  • Matplotlib实践使用笔记——基本画图

    基本画图操作 内容包括画线、条形图、直方图、饼图。 画线 画条形图 简单条形图 直方图 统计出现的次数 饼状图 会...

  • 直方图-透过现象看本质

    本文大纲 ①直方图-透过现象看本质 ②直方图的构成 ③直方图-案例解析 ④小结 直方图-透过现象看本质 直方图记录...

  • 9.4直方图均衡化

    原图 原图直方图 灰度图 灰度直方图 均衡灰度图 均衡灰度直方图 均衡彩色图 均衡彩色直方图

  • 四、OpenCV+TensorFlow 入门人工智能图像美化处理

    彩色图片直方图 灰度直方图源码 彩色直方图源码 直方图均衡化 1.灰度 1.1灰度直方图均衡化源码 2.彩色 2....

网友评论

    本文标题:画直方图

    本文链接:https://www.haomeiwen.com/subject/ithsyrtx.html