1
votes

I'm using the combined chart to draw stacked bars and a line chart on top of it. When I set the bar shadow to true, some of the bars are hidden. This is the code I have with bar shadow set to true.

public void setupChart(CombinedChart combinedChart)
{

    combinedChart.setDrawOrder(new CombinedChart.DrawOrder[]{CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.LINE});
    combinedChart.setNoDataText("");
    combinedChart.setNoDataTextDescription("");
    combinedChart.setDescription(null);
    combinedChart.setDrawGridBackground(false);
    combinedChart.setDrawBarShadow(true);
    combinedChart.setBackgroundColor(getResources().getColor(R.color.transparent));

    XAxisValueFormatter customX=new MyXAxisValueFormatter();

    XAxis xAxis=combinedChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawAxisLine(false);
    xAxis.setDrawLabels(true);
    xAxis.setDrawGridLines(false);
    xAxis.setGridColor(getResources().getColor(R.color.Gray));
    xAxis.setValueFormatter(customX);
    xAxis.setSpaceBetweenLabels(2);

    YAxisValueFormatter customY=new MyYAxisValueFormatter();

    YAxis leftAxis=combinedChart.getAxisLeft();
    leftAxis.setLabelCount(4, false);
    leftAxis.setDrawAxisLine(false);
    leftAxis.setDrawGridLines(false);
    leftAxis.setValueFormatter(customY);
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setSpaceTop(15f);

    YAxis rightAxis=combinedChart.getAxisRight();
    rightAxis.setEnabled(false);

    combinedChart.getLegend().setEnabled(false);
}

And I get the following chart

enter image description here

The same code with bar shadow set to false

combinedChart.setDrawBarShadow(false);

produces the following chart with the bars right as expected.

enter image description here

Am i doing anything out of order here ?

If it's a bug which I hope not, can i achieve the bar shadows using the grid lines ?

Great library nevertheless.

1

1 Answers

0
votes

Thanks to Phil. I got a response thru GitHub. The fix should be out in the next release I guess. But this is the method that needed to be updated in the BarChartRenderer class.

It basically draws the shadows before the actual bars are drawn

protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

    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) {

            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.
            mRenderPaint.setColor(dataSet.getColor(j / 4));
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mRenderPaint);
        }
    } else {

        mRenderPaint.setColor(dataSet.getColor());

        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], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mRenderPaint);
        }
    }
}

Hope this helps.