Android PhilJay MPAndroidChart Bar Chart Example with Server API

Hii Developer in this Android example we have implemented  Bar Chart, Bar graph In Android show data in Graph using  PhilJay MPAndroidChart. Android PhilJay MPAndroidChart Bar Chart Example with Server API to used a network API to get responses from the backend and show in graph chart.

Just follow these simple steps to make a dashboard with Chart.  Android PhilJay MPAndroidChart Bar Chart Example with Server API. Add these libraries in your project Gradle build.

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

Add Bar Chart Layout in you XML file where you want to add these Bar Chart.

<com.github.mikephil.charting.charts.BarChart
    android:id="@+id/severityBarChart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="320dp"
    android:visibility="gone" />

User These Methods for initializeBarChart Pie chart. UI in your java class and add this method in OnCreate of activity.

private void initializeBarChart() {
    severityBarChart.getDescription().setEnabled(false);

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
    severityBarChart.setMaxVisibleValueCount(4);
    severityBarChart.getXAxis().setDrawGridLines(false);
    // scaling can now only be done on x- and y-axis separately
    severityBarChart.setPinchZoom(false);

    severityBarChart.setDrawBarShadow(false);
    severityBarChart.setDrawGridBackground(false);

    XAxis xAxis = severityBarChart.getXAxis();
    xAxis.setDrawGridLines(false);

    severityBarChart.getAxisLeft().setDrawGridLines(false);
    severityBarChart.getAxisRight().setDrawGridLines(false);
    severityBarChart.getAxisRight().setEnabled(false);
    severityBarChart.getAxisLeft().setEnabled(true);
    severityBarChart.getXAxis().setDrawGridLines(false);
    // add a nice and smooth animation
    severityBarChart.animateY(1500);


    severityBarChart.getLegend().setEnabled(false);

    severityBarChart.getAxisRight().setDrawLabels(false);
    severityBarChart.getAxisLeft().setDrawLabels(true);
    severityBarChart.setTouchEnabled(false);
    severityBarChart.setDoubleTapToZoomEnabled(false);
    severityBarChart.getXAxis().setEnabled(true);
    severityBarChart.getXAxis().setPosition(XAxisPosition.BOTTOM);
    severityBarChart.invalidate();

}

Android Get API response And add Your Array list value and label name used these Methods.

Get the API response array list and add you JSONArray in These Method.

protected void onPostSeveritylist(JSONArray result) {
    //this method will be running on UI thread
    ArrayList<DataObject> data = new ArrayList<>();
    data.equals(null);
    try {
        // Extract data from json and store into ArrayList as class objects
        for (int i = 0; i < result.length(); i++) {
            JSONObject json_data = result.getJSONObject(i);
            DataObject report = new DataObject();
            report.mText2 = json_data.getString("severityType");
            report.mText3 = json_data.getString("ticketCount");
            data.add(report);
            severityStringList.add("S"+report.mText2);
        }
        severityWiseGraph = data;
        createBarChart(severityWiseGraph);
        getTicketSourcePieChartData();
    } catch (JSONException e) {
    }
}

Add API response in your Bar chart and show value.

ArrayList<String> severityStringList = new ArrayList<>();
private void createBarChart(ArrayList<DataObject> severityListServer) {
    ArrayList<BarEntry> values = new ArrayList<>();

    for (int i = 0; i < severityListServer.size(); i++) {
        DataObject dataObject = severityListServer.get(i);
        values.add(new BarEntry(i, Float.parseFloat(dataObject.mText3)));
    }

    BarDataSet set1;

    if (severityBarChart.getData() != null &&
            severityBarChart.getData().getDataSetCount() > 0) {
        set1 = (BarDataSet) severityBarChart.getData().getDataSetByIndex(0);
        set1.setValues(values);
        severityBarChart.getData().notifyDataChanged();
        severityBarChart.notifyDataSetChanged();
    } else {
        set1 = new BarDataSet(values, "Data Set");
        set1.setColors(SessionManagement.MATERIAL_COLORS);
        set1.setDrawValues(true);

        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);

        BarData data = new BarData(dataSets);
        severityBarChart.setData(data);
        severityBarChart.setVisibleXRange(1,4);
        severityBarChart.setFitBars(true);
        XAxis xAxis = severityBarChart.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setGranularityEnabled(true);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(severityStringList));//setting String values in Xaxis
        for (IDataSet set : severityBarChart.getData().getDataSets())
            set.setDrawValues(!set.isDrawValuesEnabled());

        severityBarChart.invalidate();
    }
}

 

You may also like...