Back to project page HRForecast-WFM.
The source code is released under:
Copyright 2014 Ahmed Shafei
If you think the Android project HRForecast-WFM listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package de.hrf.workforcemanagement; /* www .j a v a 2 s . c o m*/ import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.utils.Legend; import com.github.mikephil.charting.utils.Legend.LegendPosition; import com.github.mikephil.charting.utils.XLabels.XLabelPosition; import com.github.mikephil.charting.utils.XLabels; import com.github.mikephil.charting.utils.YLabels; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import de.hrf.workforcemanagement.listener.ChartValueSelectedListener; import de.hrf.workforcemanagement.models.Bar; import de.hrf.workforcemanagement.models.Chart; import de.hrf.workforcemanagement.models.Dimension; import de.hrf.workforcemanagement.models.Property; /** * {@link Fragment} responsible for showing the details of the selected chart * type. It receives a call whenever new chart type is selected */ public class BarChartFragment extends Fragment { private Chart chart; private de.hrf.workforcemanagement.models.barchart.BarChart parsedBarChart; private ProgressDialog loadingDialog; private BarChart barChartView; // TODO Create global Variable for the Chart view public BarChartFragment(Chart chart) { this.chart = chart; this.parsedBarChart = (de.hrf.workforcemanagement.models.barchart.BarChart) chart .getChartList().get(0); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Edit this to inflate the corresponding layout of this fragment chart * type */ View rootView = inflater.inflate(R.layout.bar_chart_fragment, container, false); barChartView = (BarChart) rootView.findViewById(R.id.bar_chart); loadingDialog = ProgressDialog.show(this.getActivity(), getResources() .getString(R.string.chart_loading), getResources().getString(R.string.please_wait), true, false); new Thread(new Runnable() { @Override public void run() { populateGraph(); } }).start(); return rootView; } private void populateGraph() { loadChartViewProperties(); loadChartProperties(); loadChartData(); this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { loadingDialog.dismiss(); barChartView.invalidate(); barChartView.animateXY(1500, 1500); loadChartLegend(); } }); } private void loadChartViewProperties() { barChartView.setDescription(""); barChartView.setPinchZoom(true); barChartView.setDrawBarShadow(false); ChartValueSelectedListener entrySelectedListener = new ChartValueSelectedListener( this.getActivity(), parsedBarChart.getDataList()); barChartView.setOnChartValueSelectedListener(entrySelectedListener); } private void loadChartProperties() { loadChartBackground(); setBarChartProperties(); loadChartAxis(); } private void loadChartBackground() { for (final Property property : chart.getPropertyList()) { if (property.getType().equals("chart-bg-color")) { this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { barChartView.setDrawGridBackground(false); barChartView.setBackgroundColor(Color .parseColor(property.getValue())); } }); } else if (property.getType().equals("chart-bg-picture")) { try { URL url = new URL(property.getValue()); final Bitmap bmp = BitmapFactory.decodeStream(url .openConnection().getInputStream()); this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { barChartView .setBackgroundDrawable(new BitmapDrawable( bmp)); } }); } catch (IOException e) { Log.e("Exception loading background image URL", e.getMessage()); e.printStackTrace(); } } } } private void setBarChartProperties() { ArrayList<Property> properties = parsedBarChart.getProperties(); for (Property property : properties) { if (property.getType().equals("bar-gridLines")) { setChartGridlines(property); } else if (property.getType().equals("bar-label-color")) { setBarLabelColor(property); } else if (property.getType().equals("bar-label-fontSize")) { setBarLabelFontSize(property); } else if (property.getType().equals("bar-barWidth")) { // TODO currently the possible values here are "thin" or // "overlapping", but if we have normal Bar chart (not stacked // bar chart for example) we do not have the requirement the // different bars to overlap } } } private void loadChartAxis() { boolean isXAxisVisible = parsedBarChart.getXAxis().isVisible(); boolean isYAxisVisible = parsedBarChart.getYAxis().isVisible(); if (!isXAxisVisible) { barChartView.setDrawXLabels(false); barChartView.setDrawBorder(true); } else { XLabels xLabels = barChartView.getXLabels(); xLabels.setPosition(XLabelPosition.BOTTOM); xLabels.setCenterXLabelText(true); xLabels.setSpaceBetweenLabels(0); } if (!isYAxisVisible) { barChartView.setDrawYLabels(false); } // StandardChartYAxis yAxis = barChartData.getYAxis(); // // int startYAxis = yAxis.getStart(); // int endYAxis = yAxis.getEnd(); // int stepYAxis = yAxis.getStep(); // barChartView.setYRange(startYAxis, endYAxis, true); // // YLabels yLabels = barChartView.getYLabels(); // yLabels.setLabelCount(endYAxis / stepYAxis); // // barChartView.setDrawYLabels(yAxis.isVisible()); } private void loadChartLegend() { Legend l = barChartView.getLegend(); l.setPosition(LegendPosition.BELOW_CHART_LEFT); l.setXEntrySpace(11f); l.setYEntrySpace(5f); l.setTextSize(15f); // barChartView.setDrawBarShadow(false); } private void setChartGridlines(Property property) { boolean displayGridlines = Boolean.parseBoolean(property.getValue()); if (displayGridlines) { barChartView.setDrawHorizontalGrid(true); barChartView.setDrawVerticalGrid(true); } else { barChartView.setDrawHorizontalGrid(false); barChartView.setDrawVerticalGrid(false); } } private void setBarLabelColor(Property property) { String labelColor = property.getValue(); XLabels xLabels = barChartView.getXLabels(); xLabels.setTextColor(Color.parseColor(labelColor)); YLabels yLabels = barChartView.getYLabels(); yLabels.setTextColor(Color.parseColor(labelColor)); } private void setBarLabelFontSize(Property property) { String barLabelFontSize = property.getValue(); XLabels xl = barChartView.getXLabels(); YLabels yl = barChartView.getYLabels(); float labelSize = 15f; // default value for "medium" if (barLabelFontSize.equals("small")) { labelSize = 12f; } else if (barLabelFontSize.equals("large")) { labelSize = 18f; } xl.setTextSize(labelSize); yl.setTextSize(labelSize); } private void loadChartData() { ArrayList<String> xValues = new ArrayList<String>(); List<ArrayList<BarEntry>> dataSetsYValues = new ArrayList<ArrayList<BarEntry>>(); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); ArrayList<Bar> chartBars = parsedBarChart.getDataList(); ArrayList<Dimension> dataSetsDimensions = parsedBarChart.getYAxis() .getDimensions(); int dimensionsNumber = parsedBarChart.getYAxis().getDimensions().size(); for (int i = 0; i < dimensionsNumber; i++) { dataSetsYValues.add(new ArrayList<BarEntry>()); } for (int i = 0; i < chartBars.size(); i++) { Bar bar = chartBars.get(i); xValues.add(bar.getName()); String[] parsedYValues = bar.getYValues().split(","); for (int j = 0; j < parsedYValues.length; j++) { float yValue = Float.parseFloat(parsedYValues[j].trim()); dataSetsYValues.get(j).add(new BarEntry(yValue, i)); } } for (int i = 0; i < dimensionsNumber; i++) { dataSets.add(new BarDataSet(dataSetsYValues.get(i), dataSetsDimensions.get(i).getText())); } this.setDataSetsColors(dataSets); BarData data = new BarData(xValues, dataSets); barChartView.setData(data); } private void setDataSetsColors(ArrayList<BarDataSet> dataSets) { ArrayList<de.hrf.workforcemanagement.models.Color> dataSetsColors = parsedBarChart .getDataList().get(0).getColors(); for (int i = 0; i < dataSets.size(); i++) { dataSets.get(i).setColor( Color.parseColor(dataSetsColors.get(i).getColorHexCode())); } } }