Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create verticle bargraph in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 309
    Comment on it

    Step1: Add dependency MPAndroidChart and jitpack maven repository in your build.gradle

    repositories {
    	    maven { 
    			url "https://jitpack.io" 
    		  }
    	}
    
    	dependencies{
    	
    		compile 'com.github.PhilJay:MPAndroidChart:v2.1.6'
    	}

    Step2: Adding com.github.mikephil.charting.charts.BarChart as a xml tag in a layout file, you can add different tags according to the kind of graph you need to implement Grouped Bar Chart, Horizontal Bar Chart, Simple Line Chart, Line Chart with Cubic Lines, Combined Line and Bar Chart, Pie Chart, Scatter Chart, Candlestick Chart and Radar Chart.

    <com.github.mikephil.charting.charts.BarChart
           		android:id="@+id/chart2"
           		android:layout_width="match_parent"
           		android:layout_height="130dp"
           		android:layout_marginBottom="@dimen/ten_dp"
           		android:layout_marginLeft="@dimen/twenty_dp"
           		android:layout_marginRight="@dimen/twenty_dp"
           		android:layout_marginTop="@dimen/ten_dp" />

    Step3: Initializing the BarChart into the activity onCreate() method

    	barChart = (BarChart) view.findViewById(R.id.chart2);

    Step4: Arraylist of verticle bars having BarEntries according to the different labels list \

    // verticle bars height 
            ArrayList<BarEntry> entries = new ArrayList<>();
            entries.add(new BarEntry(4f, 0));
            entries.add(new BarEntry(8f, 1));
            entries.add(new BarEntry(6f, 2));
            entries.add(new BarEntry(12f, 3));
            entries.add(new BarEntry(18f, 4));
            entries.add(new BarEntry(9f, 5));
            entries.add(new BarEntry(11f, 6));
            
    	// verticle bars according to different days 
            ArrayList<String> labels = new ArrayList<String>();
            labels.add("Mon");
            labels.add("Tues");
            labels.add("Wed");
            labels.add("Thurs");
            labels.add("Fri");
            labels.add("Sat");
            labels.add("Sun");

    Step5:  Allowing enteries into the dataset and make it as a app theme color

    BarDataSet dataset = new BarDataSet(entries, "Activities in a days");
            dataset.setColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));

    Step6: Legend is a kind of indicator that is used to describe the different lables in a graph, we can set textcolor by getting barChart object

    Legend legend = barChart.getLegend();
            legend.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));

    Step7: Adding graph x and y axis textcolor, axislinecolor according to the type of color we have to implement

    // XAxis 
            XAxis xAxis = barChart.getXAxis();
            xAxis.setAvoidFirstLastClipping(true);
            xAxis.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
            
    	// YAxis 
    	YAxis yAxisLeft = barChart.getAxisLeft();
            yAxisLeft.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
            
    	YAxis yAxisRight = barChart.getAxisRight();
            yAxisRight.setEnabled(true);
            yAxisRight.setAxisLineColor(ContextCompat.getColor(getActivity(), R.color.white));
            yAxisRight.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));

    Step8: Data object that represents all data for the BarChart, we can set set its description color, datavalue textcolor and its animation

    BarData data = new BarData(labels, dataset);
            barChart.setDescriptionColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
            barChart.setData(data);
            data.setValueTextColor(ContextCompat.getColor(getActivity(), R.color.white));
    	// for y-axis animation
            barChart.animateY(5000);

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: