extjs4.2 bar图 柱子自动对齐问题解决方法

修改Ext.chart.series.Bar中getPaths方法,修改部分红色标注

Ext.apply(Ext.chart.series.Bar.prototype,{

getPaths  : function() {
var me = this,
chart = me.chart,
store = chart.getChartStore(),
data = store.data.items,
i, total, record,
bounds = me.bounds = me.getBounds(),
items = me.items = [],
yFields = Ext.isArray(me.yField) ? me.yField : [me.yField],
gutter = me.gutter / 100,
groupGutter = me.groupGutter / 100,
animate = chart.animate,
column = me.column,
group = me.group,
enableShadows = chart.shadow,
shadowGroups = me.shadowGroups,
shadowAttributes = me.shadowAttributes,
shadowGroupsLn = shadowGroups.length,
bbox = bounds.bbox,
barWidth = bounds.barWidth,
shrunkBarWidth = bounds.shrunkBarWidth,
padding = me.getPadding(),
stacked = me.stacked,
barsLen = bounds.barsLen,
colors = me.colorArrayStyle,
colorLength = colors && colors.length || 0,
themeIndex = me.themeIdx,
math = Math,
mmax = math.max,
mmin = math.min,
mabs = math.abs,
j, yValue, height, totalDim, totalNegDim, bottom, top, hasShadow, barAttr, attrs, counter,
totalPositiveValues, totalNegativeValues,
shadowIndex, shadow, sprite, offset, floorY, idx;
var barPlace = me.getBarWidthPlace();


for (i = 0, total = data.length; i < total; i++) {
record = data[i];
bottom = bounds.zero;
top = bounds.zero;
totalDim = 0;
totalNegDim = 0;
totalPositiveValues = totalNegativeValues = 0;
hasShadow = false;
for (j = 0, counter = 0; j < barsLen; j++) {

if (me.__excludes && me.__excludes[j]) {
continue;
}
yValue = record.get(bounds.bars[j]);
if (yValue >= 0) {
totalPositiveValues += yValue;
}
else {
totalNegativeValues += yValue;
}
height = Math.round((yValue - mmax(bounds.minY, 0)) * bounds.scale);
idx = themeIndex + (barsLen > 1 ? j : 0);
barAttr = {
fill: colors[idx % colorLength]
};
if (column) {
Ext.apply(barAttr, {
height: height,
width: mmax(bounds.groupBarWidth, 0),
x: (me.boundColumn ? bounds.barsLoc[i] 
  : (bbox.x + padding.left 
+ (barPlace - shrunkBarWidth) * 0.5
+ i * barPlace * (1 + gutter) 

+ counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked)),
y: bottom - height
});
}
else {

offset = (total - 1) - i;
Ext.apply(barAttr, {
height: mmax(bounds.groupBarWidth, 0),
width: height + (bottom == bounds.zero),
x: bottom + (bottom != bounds.zero),
y: (bbox.y + padding.top 
+ (barPlace - shrunkBarWidth) * 0.5 
+ offset * barPlace * (1 + gutter) 

+ counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked + 1)
});
}
if (height < 0) {
if (column) {
barAttr.y = top;
barAttr.height = mabs(height);
} else {
barAttr.x = top + height;
barAttr.width = mabs(height);
}
}
if (stacked) {
if (height < 0) {
top += height * (column ? -1 : 1);
} else {
bottom += height * (column ? -1 : 1);
}
totalDim += mabs(height);
if (height < 0) {
totalNegDim += mabs(height);
}
}
barAttr.x = Math.floor(barAttr.x) + 1;
floorY = Math.floor(barAttr.y);
if (Ext.isIE8m && barAttr.y > floorY) {
floorY--;
}
barAttr.y = floorY;
barAttr.width = Math.floor(barAttr.width);
barAttr.height = Math.floor(barAttr.height);
items.push({
series: me,
yField: yFields[j],
storeItem: record,
value: [record.get(me.xField), yValue],
attr: barAttr,
point: column ? [barAttr.x + barAttr.width / 2, yValue >= 0 ? barAttr.y : barAttr.y + barAttr.height] :
[yValue >= 0 ? barAttr.x + barAttr.width : barAttr.x, barAttr.y + barAttr.height / 2]
});

if (animate && chart.resizing) {
attrs = column ? {
x: barAttr.x,
y: bounds.zero,
width: barAttr.width,
height: 0
} : {
x: bounds.zero,
y: barAttr.y,
width: 0,
height: barAttr.height
};
if (enableShadows && (stacked && !hasShadow || !stacked)) {
hasShadow = true;

for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
shadow = shadowGroups[shadowIndex].getAt(stacked ? i : (i * barsLen + j));
if (shadow) {
shadow.setAttributes(attrs, true);
}
}
}

sprite = group.getAt(i * barsLen + j);
if (sprite) {
sprite.setAttributes(attrs, true);
}
}
counter++;
}
if (stacked && items.length) {
items[i * counter].totalDim = totalDim;
items[i * counter].totalNegDim = totalNegDim;
items[i * counter].totalPositiveValues = totalPositiveValues;
items[i * counter].totalNegativeValues = totalNegativeValues;
}
}
if (stacked && counter == 0) {

for (i = 0, total = data.length; i < total; i++) {
for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
shadow = shadowGroups[shadowIndex].getAt(i);
if (shadow) {
shadow.hide(true);
}
}
}
}
},
getBarWidthPlace : function() {
var me = this,
store = me.chart.getChartStore(),
column = me.column,
ln = store.getCount(),
gutter = me.gutter / 100,
padding,
property;
padding = me.getPadding();
property = (column ? 'width' : 'height');
return (me.chart.chartBBox[property] - padding[property]) / (ln * (gutter + 1) - gutter);
}

});
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值