Android Open Source - HRForecast-WFM Pie Chart Fragment






From Project

Back to project page HRForecast-WFM.

License

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.

Java Source Code

package de.hrf.workforcemanagement;
/*from ww  w .ja v a2s . com*/
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

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 com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.utils.Highlight;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
import com.github.mikephil.charting.utils.Utils;

import de.hrf.workforcemanagement.listener.ChartValueSelectedListener;
import de.hrf.workforcemanagement.models.Chart;
import de.hrf.workforcemanagement.models.Property;
import de.hrf.workforcemanagement.models.piechart.PieRegion;

/**
 * {@link Fragment} responsible for showing the {@link PieChart}
 */
public class PieChartFragment extends Fragment {

  private Chart chart;
  private ProgressDialog loadingDialog;
  private PieChart pieChartView;

  public PieChartFragment(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.pie_chart_fragment,
        container, false);

    pieChartView = (PieChart) rootView.findViewById(R.id.pie_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();
        pieChartView.invalidate();
        pieChartView.animateXY(1500, 1500);
        Legend l = pieChartView.getLegend();
        l.setPosition(LegendPosition.RIGHT_OF_CHART);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(5f);
        l.setTextSize(15f);
      }
    });
  }

  /**
   * Load {@link PieChart} related properties.
   */
  private void loadChartViewProperties() {
    pieChartView.setDescription("");
    pieChartView.setDrawHoleEnabled(false);
    pieChartView.setDrawYValues(true);
    pieChartView.setDrawXValues(true);
    pieChartView.setRotationEnabled(true);
    pieChartView.setUsePercentValues(true);
    de.hrf.workforcemanagement.models.piechart.PieChart chartData = (de.hrf.workforcemanagement.models.piechart.PieChart) chart
        .getChartList().get(0);
    ChartValueSelectedListener entrySelectedListener = new ChartValueSelectedListener(
        this.getActivity(), chartData.getPieData());
    pieChartView.setOnChartValueSelectedListener(entrySelectedListener);
  }

  /**
   * Load {@link Chart} related {@link Property} list.
   */
  private void loadChartProperties() {
    loadChartBackground();
    loadChartTextSize();
  }

  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() {
            pieChartView.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() {
              pieChartView
                  .setBackgroundDrawable(new BitmapDrawable(
                      bmp));
            }
          });
        } catch (IOException e) {
          Log.e("Exception loading background image URL",
              e.getMessage());
          e.printStackTrace();
        }
    }
  }

  private void loadChartTextSize() {
    de.hrf.workforcemanagement.models.piechart.PieChart pieChartData = (de.hrf.workforcemanagement.models.piechart.PieChart) chart
        .getChartList().get(0);
    String textSize = pieChartData.getPieData().get(0).getPieLabel()
        .getSize();
    // FIXME Text values to be added in
    // de.hrf.workforcemanagement.utility.Constant class when it's merged
    // from the Feature-Proxy branch
    if (textSize.equalsIgnoreCase("small"))
      pieChartView.getPaint(
          com.github.mikephil.charting.charts.Chart.PAINT_VALUES)
          .setTextSize(Utils.convertDpToPixel(18));
    else if (textSize.equalsIgnoreCase("medium"))
      pieChartView.getPaint(
          com.github.mikephil.charting.charts.Chart.PAINT_VALUES)
          .setTextSize(Utils.convertDpToPixel(22));
    else if (textSize.equalsIgnoreCase("large"))
      pieChartView.getPaint(
          com.github.mikephil.charting.charts.Chart.PAINT_VALUES)
          .setTextSize(Utils.convertDpToPixel(28));
  }

  /**
   * Load {@link Chart}
   * {@link de.hrf.workforcemanagement.model.piechart.PieChart} data.
   */
  private void loadChartData() {
    de.hrf.workforcemanagement.models.piechart.PieChart pieChartData = (de.hrf.workforcemanagement.models.piechart.PieChart) chart
        .getChartList().get(0);
    ArrayList<Entry> yVals = new ArrayList<Entry>();
    ArrayList<String> xVals = new ArrayList<String>();
    ArrayList<Integer> colors = new ArrayList<Integer>();
    ArrayList<Highlight> focusedRegions = new ArrayList<Highlight>();
    for (int i = 0; i < pieChartData.getPieData().size(); i++) {
      PieRegion region = pieChartData.getPieData().get(i);
      yVals.add(new Entry((float) region.getValue().doubleValue(), i));
      xVals.add(region.getPieLabel().getText());
      colors.add(Color.parseColor(region.getColor()));
      if (region.getFocused().equalsIgnoreCase("true"))
        focusedRegions.add(new Highlight(i, 0));

    }
    PieDataSet pieChartSet = new PieDataSet(yVals, chart.getSubcategory());
    pieChartSet.setSliceSpace(1f);
    pieChartSet.setColors(colors);

    PieData data = new PieData(xVals, pieChartSet);
    pieChartView.setData(data);
    final Highlight[] highlighedArray = Arrays.copyOf(
        focusedRegions.toArray(), focusedRegions.toArray().length,
        Highlight[].class);
    this.getActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        pieChartView.highlightValues(highlighedArray);
      }
    });
  }
}




Java Source Code List

de.hrf.workforcemanagement.AnalysisMainActivity.java
de.hrf.workforcemanagement.BarChartFragment.java
de.hrf.workforcemanagement.BubbleChartFragment.java
de.hrf.workforcemanagement.CustomAdapter.java
de.hrf.workforcemanagement.CustomOnItemSelectedListener.java
de.hrf.workforcemanagement.HorizontalBarChartFragment.java
de.hrf.workforcemanagement.LineBarChartFragment.java
de.hrf.workforcemanagement.LineChartFragment.java
de.hrf.workforcemanagement.MainActivity.java
de.hrf.workforcemanagement.PieChartFragment.java
de.hrf.workforcemanagement.RadarChartFragment.java
de.hrf.workforcemanagement.RowModel.java
de.hrf.workforcemanagement.StackedBarChartFragment.java
de.hrf.workforcemanagement.adapter.MetadataListAdapter.java
de.hrf.workforcemanagement.dialog.BaseDialog.java
de.hrf.workforcemanagement.dialog.MetadataListDialog.java
de.hrf.workforcemanagement.listener.ChartValueSelectedListener.java
de.hrf.workforcemanagement.models.Bar.java
de.hrf.workforcemanagement.models.ChartType.java
de.hrf.workforcemanagement.models.Chart.java
de.hrf.workforcemanagement.models.Color.java
de.hrf.workforcemanagement.models.CoreFilter.java
de.hrf.workforcemanagement.models.Dimension.java
de.hrf.workforcemanagement.models.Filter.java
de.hrf.workforcemanagement.models.MetadataList.java
de.hrf.workforcemanagement.models.Metadata.java
de.hrf.workforcemanagement.models.Property.java
de.hrf.workforcemanagement.models.SpecialChartXAxis.java
de.hrf.workforcemanagement.models.SpecialChartYAxis.java
de.hrf.workforcemanagement.models.StandardChartAxis.java
de.hrf.workforcemanagement.models.StandardChartXAxis.java
de.hrf.workforcemanagement.models.StandardChartYAxis.java
de.hrf.workforcemanagement.models.TestSerializer.java
de.hrf.workforcemanagement.models.barchart.BarChart.java
de.hrf.workforcemanagement.models.bubblechart.BubbleChart.java
de.hrf.workforcemanagement.models.bubblechart.BubbleData.java
de.hrf.workforcemanagement.models.bubblechart.BubbleYAxis.java
de.hrf.workforcemanagement.models.linechart.LineChart.java
de.hrf.workforcemanagement.models.linechart.LinePoint.java
de.hrf.workforcemanagement.models.piechart.PieChart.java
de.hrf.workforcemanagement.models.piechart.PieLabel.java
de.hrf.workforcemanagement.models.piechart.PieRegion.java
de.hrf.workforcemanagement.models.radarchart.RadarChart.java
de.hrf.workforcemanagement.models.radarchart.RadarRegion.java
de.hrf.workforcemanagement.models.radarchart.RadarYAxis.java
de.hrf.workforcemanagement.models.stackedbarchart.StackedbarChart.java
de.hrf.workforcemanagement.parser.ChartParser.java