Android Open Source - HRForecast-WFM Stacked Bar 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;
/*ww w .j  a  va  2  s.  co  m*/
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

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.XLabels;
import com.github.mikephil.charting.utils.YLabels;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
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.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 StackedBarChartFragment extends Fragment {

  private Chart chart;
  private de.hrf.workforcemanagement.models.stackedbarchart.StackedbarChart parsedStackedbarChart;
  private ProgressDialog loadingDialog;
  private BarChart stackedbarChartView;

  public StackedBarChartFragment(Chart chart) {
    this.chart = chart;
    this.parsedStackedbarChart = (de.hrf.workforcemanagement.models.stackedbarchart.StackedbarChart) 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.stacked_bar_chart_fragment,
        container, false);
    stackedbarChartView = (BarChart) rootView
        .findViewById(R.id.stacked_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();
        stackedbarChartView.invalidate();
        stackedbarChartView.animateXY(1500, 1500);
        loadChartLegend();
      }
    });
  }

  private void loadChartProperties() {
    loadChartBackground();
    setChartDataProperties();
    loadChartAxis();
  }

  private void loadChartViewProperties() {
    stackedbarChartView.setPinchZoom(true);
    stackedbarChartView.setDrawBarShadow(false);
    stackedbarChartView.setDescription("");
    ChartValueSelectedListener entrySelectedListener = new ChartValueSelectedListener(
        this.getActivity(), parsedStackedbarChart.getStackedbarData());
    stackedbarChartView.setOnChartValueSelectedListener(entrySelectedListener);
  }

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

  private void setChartDataProperties() {
    ArrayList<Property> properties = parsedStackedbarChart.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 = parsedStackedbarChart.getXAxis().isVisible();
    boolean isYAxisVisible = parsedStackedbarChart.getYAxis().isVisible();

    if (!isXAxisVisible) {
      stackedbarChartView.setDrawXLabels(false);
      stackedbarChartView.setDrawBorder(true);
    } else {
      XLabels xLabels = stackedbarChartView.getXLabels();
      xLabels.setPosition(XLabelPosition.BOTTOM);
      xLabels.setCenterXLabelText(true);
      xLabels.setSpaceBetweenLabels(0);
    }

    if (!isYAxisVisible) {
      stackedbarChartView.setDrawYLabels(false);
    }
  }

  private void loadChartLegend() {
    Legend l = stackedbarChartView.getLegend();
    l.setPosition(LegendPosition.BELOW_CHART_LEFT);
    l.setXEntrySpace(7f);
    l.setYEntrySpace(5f);
    l.setTextSize(15f);
  }

  private void setChartGridlines(Property property) {
    boolean displayGridlines = Boolean.parseBoolean(property.getValue());

    if (displayGridlines) {
      stackedbarChartView.setDrawHorizontalGrid(true);
      stackedbarChartView.setDrawVerticalGrid(true);
    } else {
      stackedbarChartView.setDrawHorizontalGrid(false);
      stackedbarChartView.setDrawVerticalGrid(false);
    }
  }

  private void setBarLabelColor(Property property) {
    String labelColor = property.getValue();
    XLabels xLabels = stackedbarChartView.getXLabels();
    xLabels.setTextColor(Color.parseColor(labelColor));
    YLabels yLabels = stackedbarChartView.getYLabels();
    yLabels.setTextColor(Color.parseColor(labelColor));
  }

  private void setBarLabelFontSize(Property property) {
    String barLabelFontSize = property.getValue();
    XLabels xl = stackedbarChartView.getXLabels();
    YLabels yl = stackedbarChartView.getYLabels();

    if (barLabelFontSize.equals("small")) {
      xl.setTextSize(12f);
      yl.setTextSize(12f);
    } else if (barLabelFontSize.equals("medium")) {
      xl.setTextSize(15f);
      yl.setTextSize(15f);
    } else if (barLabelFontSize.equals("large")) {
      xl.setTextSize(18f);
      yl.setTextSize(18f);
    }
  }

  private void loadChartData() {
    ArrayList<String> xValues = new ArrayList<String>();
    ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
    ArrayList<Bar> chartBars = parsedStackedbarChart.getStackedbarData();

    ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
    for (int i = 0; i < chartBars.size(); i++) {
      Bar stackedbar = chartBars.get(i);
      xValues.add(stackedbar.getName());

      String[] parsedStackedbarYValues = stackedbar.getYValues().split(
          ",");
      float[] floatYValues = new float[parsedStackedbarYValues.length];
      for (int j = 0; j < parsedStackedbarYValues.length; j++) {
        floatYValues[j] = Float.parseFloat(parsedStackedbarYValues[j]
            .trim());
      }
      yValues.add(new BarEntry(floatYValues, i));
    }

    BarDataSet dataSet = new BarDataSet(yValues, parsedStackedbarChart
        .getXAxis().getxAxisName());

    dataSet.setColors(getDataSetsColors());
    dataSet.setStackLabels(getLegendLabels());
    dataSets.add(dataSet);

    BarData data = new BarData(xValues, dataSets);
    stackedbarChartView.setData(data);
  }

  private int[] getDataSetsColors() {
    ArrayList<de.hrf.workforcemanagement.models.Color> parsedDataSetsColors = parsedStackedbarChart
        .getStackedbarData().get(0).getColors();
    int[] dataSetsColors = new int[parsedDataSetsColors.size()];

    for (int i = 0; i < dataSetsColors.length; i++) {
      dataSetsColors[i] = Color.parseColor(parsedDataSetsColors.get(i)
          .getColorHexCode());
    }

    return dataSetsColors;
  }

  private String[] getLegendLabels() {
    ArrayList<Dimension> dataSetsDimensions = parsedStackedbarChart
        .getYAxis().getDimensions();
    String[] legendLabels = new String[dataSetsDimensions.size()];

    for (int i = 0; i < legendLabels.length; i++) {
      legendLabels[i] = dataSetsDimensions.get(i).getText();
    }

    return legendLabels;
  }
}




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