条形图


  • BarChartDataEntry 条形图条目
  • BarChartDataSet 条形图数据集合
  • BarChartData 条形图数据

BarChartDataEntry


public class BarChartDataEntry: ChartDataEntry
{
    /// the values the stacked barchart holds
    /// 堆积条形图
    private var _values: [Double]?
    
    /// the sum of all negative values this entry (if stacked) contains
    // 负数条目之和(如果是堆积条形图)
    private var _negativeSum: Double = 0.0
    
    /// the sum of all positive values this entry (if stacked) contains
    // 正数条目之和(如果是堆积条形图)
    private var _positiveSum: Double = 0.0
    
    /// Constructor for stacked bar entries.
    public init(values: [Double], xIndex: Int)
    {
        super.init(value: BarChartDataEntry.calcSum(values), xIndex: xIndex)
        self.values = values
        calcPosNegSum()
    }
    
    /// Constructor for normal bars (not stacked).
    public override init(value: Double, xIndex: Int)
    {
        super.init(value: value, xIndex: xIndex)
    }
    
    /// Constructor for stacked bar entries.
    public init(values: [Double], xIndex: Int, label: String)
    {
        super.init(value: BarChartDataEntry.calcSum(values), xIndex: xIndex, data: label)
        self.values = values
    }
    
    /// Constructor for normal bars (not stacked).
    public override init(value: Double, xIndex: Int, data: AnyObject?)
    {
        super.init(value: value, xIndex: xIndex, data: data)
    }
    
    public func getBelowSum(stackIndex :Int) -> Double
    {
        if (values == nil)
        {
            return 0
        }
        
        var remainder: Double = 0.0
        var index = values!.count - 1
        
        while (index > stackIndex && index >= 0)
        {
            remainder += values![index]
            index--
        }
        
        return remainder
    }
    
    /// :returns: the sum of all negative values this entry (if stacked) contains. (this is a positive number)
    public var negativeSum: Double
    {
        return _negativeSum
    }
    
    /// :returns: the sum of all positive values this entry (if stacked) contains.
    public var positiveSum: Double
    {
        return _positiveSum
    }
    
    public func calcPosNegSum()
    {
        if _values == nil
        {
            _positiveSum = 0.0
            _negativeSum = 0.0
            return
        }
        
        var sumNeg: Double = 0.0
        var sumPos: Double = 0.0
        
        for f in _values!
        {
            if f < 0.0
            {
                sumNeg += -f
            }
            else
            {
                sumPos += f
            }
        }
        
        _negativeSum = sumNeg
        _positiveSum = sumPos
    }
    
    // MARK: Accessors
    
    /// the values the stacked barchart holds
    public var isStacked: Bool { return _values != nil }
    
    /// the values the stacked barchart holds
    public var values: [Double]?
    {
        get { return self._values }
        set
        {
            self.value = BarChartDataEntry.calcSum(newValue)
            self._values = newValue
            calcPosNegSum()
        }
    }
    
    // MARK: NSCopying
    
    public override func copyWithZone(zone: NSZone) -> AnyObject
    {
        var copy = super.copyWithZone(zone) as! BarChartDataEntry
        copy._values = _values
        copy.value = value
        copy._negativeSum = _negativeSum
        return copy
    }
    
    /// Calculates the sum across all values of the given stack.
    ///
    /// :param: vals
    /// :returns:
    private static func calcSum(vals: [Double]?) -> Double
    {
        if vals == nil
        {
            return 0.0
        }
        
        var sum = 0.0
        
        for f in vals!
        {
            sum += f
        }
        
        return sum
    }
}

BarChartDataSet


public class BarChartDataSet: BarLineScatterCandleChartDataSet
{
    /// space indicator between the bars in percentage of the whole width of one value (0.15 == 15% of bar width)
    /// Bar之间的间距,相对于整个Bar的百分比
    public var barSpace: CGFloat = 0.15
    
    /// the maximum number of bars that are stacked upon each other, this value
    /// is calculated from the Entries that are added to the DataSet
    private var _stackSize = 1
    
    /// the color used for drawing the bar-shadows. The bar shadows is a surface behind the bar that indicates the maximum value
    /// Bar 阴影颜色
    public var barShadowColor = UIColor(red: 215.0/255.0, green: 215.0/255.0, blue: 215.0/255.0, alpha: 1.0)
    
    /// the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0.0 (fully transparent), max = 1.0 (fully opaque)
    /// 标识 高亮 的透明度
    public var highLightAlpha = CGFloat(120.0 / 255.0)
    
    /// the overall entry count, including counting each stack-value individually
    private var _entryCountStacks = 0
    
    /// array of labels used to describe the different values of the stacked bars
    // 堆积条形图标签数组
    public var stackLabels: [String] = ["Stack"]
    
    public override init(yVals: [ChartDataEntry]?, label: String?)
    {
        super.init(yVals: yVals, label: label)
        
        self.highlightColor = UIColor.blackColor()
        
        self.calcStackSize(yVals as! [BarChartDataEntry]?)
        self.calcEntryCountIncludingStacks(yVals as! [BarChartDataEntry]?)
    }
    
    // MARK: NSCopying
    
    public override func copyWithZone(zone: NSZone) -> AnyObject
    {
        var copy = super.copyWithZone(zone) as! BarChartDataSet
        copy.barSpace = barSpace
        copy._stackSize = _stackSize
        copy.barShadowColor = barShadowColor
        copy.highLightAlpha = highLightAlpha
        copy._entryCountStacks = _entryCountStacks
        copy.stackLabels = stackLabels
        return copy
    }
    
    /// Calculates the total number of entries this DataSet represents, including
    /// stacks. All values belonging to a stack are calculated separately.
    private func calcEntryCountIncludingStacks(yVals: [BarChartDataEntry]!)
    {
        _entryCountStacks = 0
        
        for (var i = 0; i < yVals.count; i++)
        {
            var vals = yVals[i].values
            
            if (vals == nil)
            {
                _entryCountStacks++
            }
            else
            {
                _entryCountStacks += vals!.count
            }
        }
    }
    
    /// calculates the maximum stacksize that occurs in the Entries array of this DataSet
    private func calcStackSize(yVals: [BarChartDataEntry]!)
    {
        for (var i = 0; i < yVals.count; i++)
        {
            if let vals = yVals[i].values
            {
                if vals.count > _stackSize
                {
                    _stackSize = vals.count
                }
            }
        }
    }
    
    internal override func calcMinMax(#start : Int, end: Int)
    {
        let yValCount = _yVals.count
        
        if yValCount == 0
        {
            return
        }
        
        var endValue : Int
        
        if end == 0 || end >= yValCount
        {
            endValue = yValCount - 1
        }
        else
        {
            endValue = end
        }
        
        _lastStart = start
        _lastEnd = endValue
        
        _yMin = DBL_MAX
        _yMax = -DBL_MAX
        
        for (var i = start; i <= endValue; i++)
        {
            if let e = _yVals[i] as? BarChartDataEntry
            {
                if !e.value.isNaN
                {
                    if e.values == nil
                    {
                        if e.value < _yMin
                        {
                            _yMin = e.value
                        }
                        
                        if e.value > _yMax
                        {
                            _yMax = e.value
                        }
                    }
                    else
                    {
                        if -e.negativeSum < _yMin
                        {
                            _yMin = -e.negativeSum
                        }
                        
                        if e.positiveSum > _yMax
                        {
                            _yMax = e.positiveSum
                        }
                    }
                }
            }
        }
        
        if (_yMin == DBL_MAX)
        {
            _yMin = 0.0
            _yMax = 0.0
        }
    }
    
    /// Returns the maximum number of bars that can be stacked upon another in this DataSet.
    public var stackSize: Int
    {
        return _stackSize
    }
    
    /// Returns true if this DataSet is stacked (stacksize > 1) or not.
    public var isStacked: Bool
    {
        return _stackSize > 1 ? true : false
    }
    
    /// returns the overall entry count, including counting each stack-value individually
    public var entryCountStacks: Int
    {
        return _entryCountStacks
    }
}

BarChartData


public class BarChartData: BarLineScatterCandleChartData
{
    public override init()
    {
        super.init()
    }
    
    public override init(xVals: [String?]?, dataSets: [ChartDataSet]?)
    {
        super.init(xVals: xVals, dataSets: dataSets)
    }
    
    public override init(xVals: [NSObject]?, dataSets: [ChartDataSet]?)
    {
        super.init(xVals: xVals, dataSets: dataSets)
    }
    
    // 组之间的距离
    private var _groupSpace = CGFloat(0.8)
    
    /// The spacing is relative to a full bar width
    public var groupSpace: CGFloat
    {
        get
        {
            if (_dataSets.count <= 1)
            {
                return 0.0
            }
            return _groupSpace
        }
        set
        {
            _groupSpace = newValue
        }
    }
    
    /// Returns true if this BarData object contains grouped DataSets (more than 1 DataSet).
    public var isGrouped: Bool
    {
        return _dataSets.count > 1 ? true : false
    }
}