MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

HorizontalBarChart use the setGradientColor() do not work.

Open ProgramerStar opened this issue 6 years ago • 10 comments

I used the setGradientColor(xx, xx) in HorizontalBarChart ,but it do not work in any condition . is it a bug? if not, add it in example ,please. thanks.

ProgramerStar avatar Nov 13 '18 06:11 ProgramerStar

Hi, could you provide a code sample showing how you tried to achieve the desired behaviour? Also what version of the library did you use?

JamieKalloe avatar Nov 13 '18 11:11 JamieKalloe

Hi, could you provide a code sample showing how you tried to achieve the desired behaviour? Also what version of the library did you use?

      The library  version  is  v3.1.0-alpha

      code  as this :

  `     HorizontalBarChart horizontalBarChart;
        horizontalBarChart= findViewById(R.id.chart1);
        BarDataSet set1;
                  ###    ###    ### 
        set1 = new BarDataSet(values, "DataSet 1");
        set1.setDrawIcons(false);
        int startColor = ContextCompat.getColor(this, android.R.color.holo_orange_light);
        int endColor = ContextCompat.getColor(this, android.R.color.holo_blue_light);
        set1.setGradientColor(startColor, endColor);
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        data.setValueTypeface(tfLight);
        data.setBarWidth(barWidth);
        horizontalBarChart.setData(data);
        horizontalBarChart.invalidate();`

ProgramerStar avatar Nov 14 '18 02:11 ProgramerStar

Looks like this wasn't added to Horizontal Bar charts! I'll update the renderer today and get it pushed.

almic avatar Nov 15 '18 17:11 almic

Hi @almic , Is this issue open?

ghost avatar Jan 29 '19 19:01 ghost

这个问题今天我也遇到了,现在还没有修复么? 我拉取源码后,HorizontalBarChartRenderer.java添加如下可以实现 ` @Override protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

    Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

    mBarBorderPaint.setColor(dataSet.getBarBorderColor());
    mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

    final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    // draw the bar shadow before the values
    if (mChart.isDrawBarShadowEnabled())
    {
        mShadowPaint.setColor(dataSet.getBarShadowColor());

        BarData barData = mChart.getBarData();

        final float barWidth = barData.getBarWidth();
        final float barWidthHalf = barWidth / 2.0f;
        float x;

        for (int i = 0, count = Math.min((int) (Math.ceil((float) (dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
             i < count;
             i++)
        {

            BarEntry e = dataSet.getEntryForIndex(i);

            x = e.getX();

            mBarShadowRectBuffer.top = x - barWidthHalf;
            mBarShadowRectBuffer.bottom = x + barWidthHalf;

            trans.rectValueToPixel(mBarShadowRectBuffer);

            if (!mViewPortHandler.isInBoundsTop(mBarShadowRectBuffer.bottom))
                continue;

            if (!mViewPortHandler.isInBoundsBottom(mBarShadowRectBuffer.top))
                break;

            mBarShadowRectBuffer.left = mViewPortHandler.contentLeft();
            mBarShadowRectBuffer.right = mViewPortHandler.contentRight();

            c.drawRect(mBarShadowRectBuffer, mShadowPaint);
        }
    }

    // initialize the buffer
    BarBuffer buffer = mBarBuffers[index];
    buffer.setPhases(phaseX, phaseY);
    buffer.setDataSet(index);
    buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
    buffer.setBarWidth(mChart.getBarData().getBarWidth());

    buffer.feed(dataSet);

    trans.pointValuesToPixel(buffer.buffer);

    final boolean isSingleColor = dataSet.getColors().size() == 1;

    if (isSingleColor)
    {
        mRenderPaint.setColor(dataSet.getColor());
    }

    for (int j = 0; j < buffer.size(); j += 4)
    {

        if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 3]))
            break;

        if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
            continue;

        if (!isSingleColor)
        {
            // Set the color for the currently drawn value. If the index
            // is out of bounds, reuse colors.
            mRenderPaint.setColor(dataSet.getColor(j / 4));
        }

// buffer[index++] = left; // buffer[index++] = top; // buffer[index++] = right; // buffer[index++] = bottom; //

        if (dataSet.getGradientColor() != null)
        {
            GradientColor gradientColor = dataSet.getGradientColor();
            mRenderPaint.setShader(
                    new LinearGradient(
                            buffer.buffer[j],
                            buffer.buffer[j + 1],
                            buffer.buffer[j + 2],
                            buffer.buffer[j + 3],
                            gradientColor.getStartColor(),
                            gradientColor.getEndColor(),
                            android.graphics.Shader.TileMode.MIRROR));
        }

        if (dataSet.getGradientColors() != null) {
            mRenderPaint.setShader(
                    new LinearGradient(
                            buffer.buffer[j],
                            buffer.buffer[j + 1],
                            buffer.buffer[j + 2],
                            buffer.buffer[j + 3],
                            dataSet.getGradientColor(j / 4).getStartColor(),
                            dataSet.getGradientColor(j / 4).getEndColor(),
                            android.graphics.Shader.TileMode.MIRROR));
        }

        c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                buffer.buffer[j + 3], mRenderPaint);

        if (drawBorder)
        {
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mBarBorderPaint);
        }
    }
}`

ghost avatar Mar 11 '19 09:03 ghost

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' I have same problem,the three BarEntry‘s color is same as # 8CEAFF

I see 6 month ago Implement a more generic Fill class instead of GradientColor,but 3.1.0 don't have Fill,please put new Releases!

for (int i = 0; i < 3; i++) {
    mBarEntries.add(new BarEntry(i, 10f));
}
//BarDataSet
mBarDataSet = new BarDataSet(mBarEntries, "");
mGradientColors.add(new GradientColor(Color.parseColor("#760303"), Color.parseColor("#E30000")));
//mGradientColors.add(new GradientColor(Color.parseColor("#0cb32f"), Color.parseColor("#2bff5d")));
//mGradientColors.add(new GradientColor(Color.parseColor("#b30c14"), Color.parseColor("#ff2b58")));
mBarDataSet.setGradientColors(mGradientColors);
BarData mBarData = new BarData(mBarDataSet);
mBarChart.setData(mBarData);
mBarChart.invalidate();

Pzxxc avatar Jul 23 '20 07:07 Pzxxc

If

mBarChart.getAxisLeft().setEnabled(false);
YAxis yAxis_right = mBarChart.getAxisRight();
yAxis_right.setAxisMinimum(0);

The Fill will not work,but , if getAxisRight is OK!

mBarChart.getAxisRight().setEnabled(false);
YAxis yAxis_left = mBarChart.getAxisLeft();
yAxis_left.setAxisMinimum(0);
//data
for (int i = 0; i < 3; i++) {
    mBarEntries.add(new BarEntry(i, 10f));
}
mBarDataSet = new BarDataSet(mBarEntries, "");
mGradientColors.add(new Fill(new int[]{Color.parseColor("#FFd9ef1f"), Color.parseColor("#FFDEF723"), Color.parseColor("#00e3ff28")}));
//mGradientColors.add(new Fill(new int[]{Color.parseColor("#FF0cb32f"), Color.parseColor("#FF1BD946"), Color.parseColor("#002bff5d")}));
//mGradientColors.add(new Fill(new int[]{Color.parseColor("#FFb30c14"), Color.parseColor("#FFD91B36"), Color.parseColor("#00ff2b58")}));
mBarDataSet.setFills(mGradientColors);
BarData mBarData = new BarData(mBarDataSet);
mBarChart.setData(mBarData);
mBarChart.invalidate();

Pzxxc avatar Jul 23 '20 08:07 Pzxxc

@all the solution is given by @MyMSDY guys follow that it will fix the issue fork the project and add the changes ,Problem solved

rameshvoltella avatar Sep 29 '22 15:09 rameshvoltella

Hi I am unable to get Fill from MP chart Library I need to display Multi gradient bar chart It is working in MP chart sample app but i use MP chart Library i unable to access Fill and setFills() Can you help me in this issue

image val gradientFills: MutableList = java.util.ArrayList() val numbersRange = intArrayOf( ContextCompat.getColor(requireContext(), android.R.color.holo_red_light), ContextCompat.getColor(requireContext(), android.R.color.holo_orange_light), ContextCompat.getColor(requireContext(), android.R.color.holo_green_light) ) gradientFills.add(Fill(numbersRange)) gradientFills.add(Fill(numbersRange)) gradientFills.add(Fill(numbersRange)) gradientFills.add(Fill(numbersRange)) gradientFills.add(Fill(numbersRange))
        set1.setFills(gradientFills)  

NareshVR22 avatar May 23 '23 01:05 NareshVR22

If

mBarChart.getAxisLeft().setEnabled(false);
YAxis yAxis_right = mBarChart.getAxisRight();
yAxis_right.setAxisMinimum(0);

The Fill will not work,but , if getAxisRight is OK!

mBarChart.getAxisRight().setEnabled(false);
YAxis yAxis_left = mBarChart.getAxisLeft();
yAxis_left.setAxisMinimum(0);
//data
for (int i = 0; i < 3; i++) {
    mBarEntries.add(new BarEntry(i, 10f));
}
mBarDataSet = new BarDataSet(mBarEntries, "");
mGradientColors.add(new Fill(new int[]{Color.parseColor("#FFd9ef1f"), Color.parseColor("#FFDEF723"), Color.parseColor("#00e3ff28")}));
//mGradientColors.add(new Fill(new int[]{Color.parseColor("#FF0cb32f"), Color.parseColor("#FF1BD946"), Color.parseColor("#002bff5d")}));
//mGradientColors.add(new Fill(new int[]{Color.parseColor("#FFb30c14"), Color.parseColor("#FFD91B36"), Color.parseColor("#00ff2b58")}));
mBarDataSet.setFills(mGradientColors);
BarData mBarData = new BarData(mBarDataSet);
mBarChart.setData(mBarData);
mBarChart.invalidate();

Hi I am unable to get Fill from MP chart Library I need to display Multi gradient bar chart It is working in MP chart sample app but i use MP chart Library i unable to access Fill and setFills() Can you help me in this issue

NareshVR22 avatar May 24 '23 12:05 NareshVR22