答案 0 :(得分:0)
找到了一种方法。我扩展了BarChartRenderer类并覆盖了drawHighlighted方法。
public class CustomBarChartRenderer extends BarChartRenderer {
public CustomBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
super(chart, animator, viewPortHandler);
}
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {
for (int i = 0; i < indices.length; i++) {
Highlight h = indices[i];
int index = h.getXIndex();
BarData barData = mChart.getBarData();
for (int j = 0; j < barData.getDataSetCount(); j++) {
IBarDataSet set = barData.getDataSetByIndex(j);
if (set.isVisible() && set.getEntryCount() > 0) {
customDrawDataSet(c, set, j, index);
}
}
}
}
protected void customDrawDataSet(Canvas c, IBarDataSet dataSet, int index, int selectedIndex) {
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
mShadowPaint.setColor(dataSet.getBarShadowColor());
float phaseX = mAnimator.getPhaseX();
float phaseY = mAnimator.getPhaseY();
// initialize the buffer
BarBuffer buffer = mBarBuffers[index];
buffer.setPhases(phaseX, phaseY);
buffer.setBarSpace(dataSet.getBarSpace());
buffer.setDataSet(index);
buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
buffer.feed(dataSet);
trans.pointValuesToPixel(buffer.buffer);
// draw the bar shadow before the values
if (mChart.isDrawBarShadowEnabled()) {
for (int j = 0; j < buffer.size(); j += 4) {
if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
continue;
if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
break;
c.drawRect(buffer.buffer[j], mViewPortHandler.contentTop(),
buffer.buffer[j + 2],
mViewPortHandler.contentBottom(), mShadowPaint);
}
}
// if multiple colors
if (dataSet.getColors().size() > 1) {
for (int j = 0; j < buffer.size(); j += 4) {
mHighlightPaint.setColor(Color.rgb(255, 255, 255));
mHighlightPaint.setAlpha(dataSet.getHighLightAlpha());
if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
continue;
if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
break;
// Set the color for the currently drawn value. If the index
// is
// out of bounds, reuse colors.
float left = 0f;
float top = 0f;
float right = 0f;
float bottom = 0f;
if( j == (selectedIndex * 4)) {
left = buffer.buffer[j] - 10;
top = buffer.buffer[j + 1];
right = buffer.buffer[j + 2] + 10;
bottom = buffer.buffer[j + 3];
mRenderPaint.setColor(dataSet.getColor(j / 4));
}
else {
left = buffer.buffer[j];
top = buffer.buffer[j + 1];
right = buffer.buffer[j + 2];
bottom = buffer.buffer[j + 3];
mRenderPaint = mHighlightPaint;
}
c.drawRect(left, top, right, bottom, mRenderPaint);
}
} else {
mRenderPaint.setColor(dataSet.getColor());
for (int j = 0; j < buffer.size(); j += 4) {
mHighlightPaint.setColor(Color.rgb(255, 255, 255));
mHighlightPaint.setAlpha(dataSet.getHighLightAlpha());
if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
continue;
if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
break;
float left = 0f;
float top = 0f;
float right = 0f;
float bottom = 0f;
if( j == (selectedIndex * 4)) {
left = buffer.buffer[j] - 10;
top = buffer.buffer[j + 1];
right = buffer.buffer[j + 2] + 10;
bottom = buffer.buffer[j + 3];
mRenderPaint.setColor(dataSet.getColor(j / 4));
}
else {
left = buffer.buffer[j];
top = buffer.buffer[j + 1];
right = buffer.buffer[j + 2];
bottom = buffer.buffer[j + 3];
mRenderPaint = mHighlightPaint;
}
c.drawRect(left, top, right, bottom, mRenderPaint);
}
}
}