publicclassBarChartDataEntry:ChartDataEntry{/// the values the stacked barchart holds/// 堆积条形图privatevar_values:[Double]?/// the sum of all negative values this entry (if stacked) contains// 负数条目之和(如果是堆积条形图)privatevar_negativeSum:Double=0.0/// the sum of all positive values this entry (if stacked) contains// 正数条目之和(如果是堆积条形图)privatevar_positiveSum:Double=0.0/// Constructor for stacked bar entries.publicinit(values:[Double],xIndex:Int){super.init(value:BarChartDataEntry.calcSum(values),xIndex:xIndex)self.values=valuescalcPosNegSum()}/// Constructor for normal bars (not stacked).publicoverrideinit(value:Double,xIndex:Int){super.init(value:value,xIndex:xIndex)}/// Constructor for stacked bar entries.publicinit(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).publicoverrideinit(value:Double,xIndex:Int,data:AnyObject?){super.init(value:value,xIndex:xIndex,data:data)}publicfuncgetBelowSum(stackIndex:Int)->Double{if(values==nil){return0}varremainder:Double=0.0varindex=values!.count-1while(index>stackIndex&&index>=0){remainder+=values![index]index--}returnremainder}/// :returns: the sum of all negative values this entry (if stacked) contains. (this is a positive number)publicvarnegativeSum:Double{return_negativeSum}/// :returns: the sum of all positive values this entry (if stacked) contains.publicvarpositiveSum:Double{return_positiveSum}publicfunccalcPosNegSum(){if_values==nil{_positiveSum=0.0_negativeSum=0.0return}varsumNeg:Double=0.0varsumPos:Double=0.0forfin_values!{iff<0.0{sumNeg+=-f}else{sumPos+=f}}_negativeSum=sumNeg_positiveSum=sumPos}// MARK: Accessors/// the values the stacked barchart holdspublicvarisStacked:Bool{return_values!=nil}/// the values the stacked barchart holdspublicvarvalues:[Double]?{get{returnself._values}set{self.value=BarChartDataEntry.calcSum(newValue)self._values=newValuecalcPosNegSum()}}// MARK: NSCopyingpublicoverridefunccopyWithZone(zone:NSZone)->AnyObject{varcopy=super.copyWithZone(zone)as!BarChartDataEntrycopy._values=_valuescopy.value=valuecopy._negativeSum=_negativeSumreturncopy}/// Calculates the sum across all values of the given stack.////// :param: vals/// :returns:privatestaticfunccalcSum(vals:[Double]?)->Double{ifvals==nil{return0.0}varsum=0.0forfinvals!{sum+=f}returnsum}}
BarChartDataSet
publicclassBarChartDataSet:BarLineScatterCandleChartDataSet{/// space indicator between the bars in percentage of the whole width of one value (0.15 == 15% of bar width)/// Bar之间的间距,相对于整个Bar的百分比publicvarbarSpace: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 DataSetprivatevar_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 阴影颜色publicvarbarShadowColor=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)/// 标识 高亮 的透明度publicvarhighLightAlpha=CGFloat(120.0/255.0)/// the overall entry count, including counting each stack-value individuallyprivatevar_entryCountStacks=0/// array of labels used to describe the different values of the stacked bars// 堆积条形图标签数组publicvarstackLabels:[String]=["Stack"]publicoverrideinit(yVals:[ChartDataEntry]?,label:String?){super.init(yVals:yVals,label:label)self.highlightColor=UIColor.blackColor()self.calcStackSize(yValsas![BarChartDataEntry]?)self.calcEntryCountIncludingStacks(yValsas![BarChartDataEntry]?)}// MARK: NSCopyingpublicoverridefunccopyWithZone(zone:NSZone)->AnyObject{varcopy=super.copyWithZone(zone)as!BarChartDataSetcopy.barSpace=barSpacecopy._stackSize=_stackSizecopy.barShadowColor=barShadowColorcopy.highLightAlpha=highLightAlphacopy._entryCountStacks=_entryCountStackscopy.stackLabels=stackLabelsreturncopy}/// Calculates the total number of entries this DataSet represents, including/// stacks. All values belonging to a stack are calculated separately.privatefunccalcEntryCountIncludingStacks(yVals:[BarChartDataEntry]!){_entryCountStacks=0for(vari=0;i<yVals.count;i++){varvals=yVals[i].valuesif(vals==nil){_entryCountStacks++}else{_entryCountStacks+=vals!.count}}}/// calculates the maximum stacksize that occurs in the Entries array of this DataSetprivatefunccalcStackSize(yVals:[BarChartDataEntry]!){for(vari=0;i<yVals.count;i++){ifletvals=yVals[i].values{ifvals.count>_stackSize{_stackSize=vals.count}}}}internaloverridefunccalcMinMax(#start:Int,end:Int){letyValCount=_yVals.countifyValCount==0{return}varendValue:Intifend==0||end>=yValCount{endValue=yValCount-1}else{endValue=end}_lastStart=start_lastEnd=endValue_yMin=DBL_MAX_yMax=-DBL_MAXfor(vari=start;i<=endValue;i++){iflete=_yVals[i]as?BarChartDataEntry{if!e.value.isNaN{ife.values==nil{ife.value<_yMin{_yMin=e.value}ife.value>_yMax{_yMax=e.value}}else{if-e.negativeSum<_yMin{_yMin=-e.negativeSum}ife.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.publicvarstackSize:Int{return_stackSize}/// Returns true if this DataSet is stacked (stacksize > 1) or not.publicvarisStacked:Bool{return_stackSize>1?true:false}/// returns the overall entry count, including counting each stack-value individuallypublicvarentryCountStacks:Int{return_entryCountStacks}}
BarChartData
publicclassBarChartData:BarLineScatterCandleChartData{publicoverrideinit(){super.init()}publicoverrideinit(xVals:[String?]?,dataSets:[ChartDataSet]?){super.init(xVals:xVals,dataSets:dataSets)}publicoverrideinit(xVals:[NSObject]?,dataSets:[ChartDataSet]?){super.init(xVals:xVals,dataSets:dataSets)}// 组之间的距离privatevar_groupSpace=CGFloat(0.8)/// The spacing is relative to a full bar widthpublicvargroupSpace:CGFloat{get{if(_dataSets.count<=1){return0.0}return_groupSpace}set{_groupSpace=newValue}}/// Returns true if this BarData object contains grouped DataSets (more than 1 DataSet).publicvarisGrouped:Bool{return_dataSets.count>1?true:false}}