Uday
Uday

Reputation: 1484

Display MPAndroidChart PieChart horizontally

Is there a chart that looks like PieChart and rather than circle, numbers should be displayed in horizontal bar?

In PieChart, we display numbers in Circle. But how can i display them in single bar(horizontal)?

Anybody can help here?

I need a horizontal chart for this PieChart enter image description here

I tried updating my code to have horizontalbarchar and got below chart. But the values are showing wrong in the chart and also i dont want to show the other parameters which are marked in Red. Can someone help me here.

My Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.github.mikephil.charting.charts.HorizontalBarChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/white" />



</RelativeLayout>

My code:

  super.onCreate(savedInstanceState);
        setContentView(R.layout.horizontal_seekbar_layout);
        mChart = findViewById(R.id.chart1);

        ArrayList<BarEntry> values=new ArrayList<>();
        values.add(new BarEntry(0,new float[]{5,3,1}));

        BarDataSet set1=new BarDataSet(values,"Summary");
        //set1.setDrawIcons(false);
        set1.setStackLabels(new String[]{"Passed","Failed","Skipped"});
        ArrayList<Integer> colors=new ArrayList<Integer>();
        colors.add(Color.GREEN);
        colors.add(Color.RED);
        colors.add(Color.GRAY);
        set1.setColors(colors);
        set1.setValueFormatter(new DecValueFormatter());

        BarData data=new BarData(set1);
        mChart.setData(data);
       // mChart.setFitBars(true);
        mChart.invalidate();

My current chart i am getting: enter image description here

Upvotes: 0

Views: 1057

Answers (1)

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

It can be done using stacked bar chart of mp android chart. Please follow example below to do that:

ArrayList<BarEntry> barEntries = new ArrayList<BarEntry>();
    barEntries.add(new BarEntry(0, new float[]{2,4,5,3}));


    BarDataSet barDataSet = new BarDataSet(barEntries, "Stacked Bars");
    barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
    barDataSet.setHighlightEnabled(true);
    barDataSet.setHighLightColor(Color.RED);
    barDataSet.setColors(getColors(4,Color.RED,Color.BLUE,Color.GREEN,Color.CYAN));
    barDataSet.setStackLabels(new String[]{"Bottom","Top","Left","Right"});

    BarData barData = new BarData(barDataSet);
    barData.setValueTextSize(defaultValueTextSize);
    barData.setValueTextColor(getColor("primaryDark"));

    barChart.setDrawValueAboveBar(false); // add this line to show values inside bars
    barChart.getAxisLeft().setAxisMinimum(0);
    barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
    barChart.animateY(1000);
    barChart.setData(barData);

This is for vertical bar just use horizontal bar chart of this library and you are good to go with buddy. Happy Coding :)

Upvotes: 1

Related Questions