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; // w w w . ja v a 2 s.co m import java.io.IOException; import java.net.URL; import java.util.ArrayList; import com.github.mikephil.charting.charts.RadarChart; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.RadarData; import com.github.mikephil.charting.data.RadarDataSet; import com.github.mikephil.charting.utils.XLabels.XLabelPosition; 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.models.Chart; import de.hrf.workforcemanagement.models.Property; import de.hrf.workforcemanagement.models.radarchart.RadarRegion; /** * {@link Fragment} responsible for showing the details of the selected chart * type. It receives a call whenever new chart type is selected */ public class RadarChartFragment extends Fragment { private Chart chart; private ProgressDialog loadingDialog; private RadarChart radarChartView; private RadarDataSet radarDataSet; private de.hrf.workforcemanagement.models.radarchart.RadarChart radarChartData; public RadarChartFragment(Chart chart) { this.chart = chart; } @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.radar_chart_fragment, container, false); radarChartView = (RadarChart) rootView.findViewById(R.id.radar_chart); // Show loadingDialog 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(); loadChartData(); loadChartProperties(); this.getActivity().runOnUiThread(new Runnable() { @Override public void run() { loadingDialog.dismiss(); radarChartView.invalidate(); radarChartView.animateXY(1500, 1500); } }); } /** * Load {@link RadatChart} related properties. */ private void loadChartViewProperties() { radarChartView.setDescription(""); radarChartView.setDrawYValues(true); radarChartView.setDrawUnitsInChart(true); radarChartView.getXLabels().setPosition(XLabelPosition.BOTTOM); radarChartView.getLegend(); radarChartView.setValueTextSize(12f); radarChartView.getXLabels().setTextSize(11f); radarChartView.getYLabels().setTextSize(14f); } /** * Load {@link Chart} related {@link Property} list. */ private void loadChartProperties() { loadChartBackground(); setChartDataProperties(); setChartAxisProperties(); } 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() { radarChartView.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() { radarChartView .setBackgroundDrawable(new BitmapDrawable( bmp)); } }); } catch (IOException e) { Log.e("Exception loading background image URL", e.getMessage()); e.printStackTrace(); } } } /** * Load {@link Chart} * {@link de.hrf.workforcemanagement.model.radarChart.RadarChart} data. * * @param radarChartView */ private void loadChartData() { radarChartData = (de.hrf.workforcemanagement.models.radarchart.RadarChart) chart .getChartList().get(0); ArrayList<RadarDataSet> radarDataSets = new ArrayList<RadarDataSet>(); ArrayList<String> xVals = new ArrayList<String>(); String[] radarRegionYVals = new String[0]; // Adds Y Values for (int index = 0; index < radarChartData.getRadarYaxis() .getDimensions().size(); index++) { ArrayList<Entry> yVals = new ArrayList<Entry>(); for (int i = 0; i < radarChartData.getRadarData().size(); i++) { RadarRegion radarRegion = radarChartData.getRadarData().get(i); radarRegionYVals = radarRegion.getyValue().split(","); yVals.add(new Entry(Float.parseFloat(radarRegionYVals[index]), i)); } radarDataSet = new RadarDataSet(yVals, radarChartData .getRadarYaxis().getDimensions().get(index).getText()); radarDataSet.setLineWidth(3f); radarDataSet.setFillAlpha(65); radarDataSets.add(radarDataSet); } // Adds X Values for (int i = 0; i < radarChartData.getRadarData().size(); i++) { RadarRegion radarRegion = radarChartData.getRadarData().get(i); xVals.add(radarRegion.getRegionName()); } RadarData data = new RadarData(xVals, radarDataSets); radarChartView.setData(data); setRadarDataColor(radarDataSets); } private void setChartDataProperties() { } private void setChartAxisProperties() { if (radarChartData.getRadarXAxis().isVisible() == false) { radarChartView.setDrawXLabels(false); } if (radarChartData.getRadarYaxis().isVisible() == false) { radarChartView.setDrawYLabels(false); radarChartView.setDrawYValues(false); } if ((radarChartData.getRadarXAxis().isVisible() == false) && (radarChartData.getRadarYaxis().isVisible() == false)) { radarChartView.setDrawYValues(false); radarChartView.setDrawXLabels(false); radarChartView.setDrawYLabels(false); } } private void setRadarDataColor(ArrayList<RadarDataSet> radarDataSets) { ArrayList<de.hrf.workforcemanagement.models.Color> radarColors = radarChartData .getRadarData().get(0).getColors(); for (int index = 0; index < radarColors.size(); index++) { radarDataSets.get(index).setColor( Color.parseColor(radarColors.get(index).getColorHexCode())); } } }