Android Open Source - HRForecast-WFM Bubble 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 w w  w .j a  v a2 s.c o  m*/
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import com.github.mikephil.charting.charts.LineChart;

import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.BubbleChartData;
import lecho.lib.hellocharts.model.BubbleValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.view.BubbleChartView;
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;

/**
 * {@link Fragment} responsible for showing the details of the selected chart
 * type. It receives a call whenever new chart type is selected
 */
public class BubbleChartFragment extends Fragment {

  private Chart chart;
  private ProgressDialog loadingDialog;
  private BubbleChartView bubbleChartView;
  private de.hrf.workforcemanagement.models.bubblechart.BubbleChart bubbleChartData;
  private Axis axisX;
  private Axis axisY;

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

    bubbleChartView = (BubbleChartView) rootView
        .findViewById(R.id.bubble_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();
        bubbleChartView.invalidate();
      }
    });
  }

  /**
   * Load {@link LineChart} related properties.
   */
  private void loadChartViewProperties() {
    bubbleChartView.setZoomEnabled(true);
    bubbleChartView.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);

  }

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

  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() {
            bubbleChartView.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() {
              bubbleChartView
                  .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.models.bubblechart.BubbleChart} data.
   * 
   * @param bubbleChartView
   */
  private void loadChartData() {
    bubbleChartData = (de.hrf.workforcemanagement.models.bubblechart.BubbleChart) chart
        .getChartList().get(0);
    BubbleChartData data;
    ArrayList<BubbleValue> bubbleDataSets = new ArrayList<BubbleValue>();
    ArrayList<AxisValue> xVals = new ArrayList<AxisValue>();
    float xVal = 0f;
    float yVal = 0f;
    float bubbleDiameter = 0f;
    // Start always from zero
    xVals.add(new AxisValue(0f));

    for (int index = 0; index < bubbleChartData.getBubbleData().size(); index++) {
      xVal = Float.parseFloat(bubbleChartData.getBubbleData().get(index)
          .getBubbleXvalue());
      xVals.add(new AxisValue(xVal));

      yVal = (float) bubbleChartData.getBubbleData().get(index)
          .getBubbleYvalue();
      bubbleDiameter = (float) bubbleChartData.getBubbleData().get(index)
          .getBubbleDiameter();
      // Adds data for 1 bubble
      BubbleValue bubbleValue = new BubbleValue(xVal, yVal,
          bubbleDiameter);
      bubbleValue.setShape(ValueShape.CIRCLE);
      // Sets Bubble Color
      bubbleValue.setColor(Color.parseColor(bubbleChartData
          .getBubbleData().get(index).getBubbleColor()));
      bubbleDataSets.add(bubbleValue);
    }
    data = new BubbleChartData(bubbleDataSets);
    data.setHasLabels(false);
    // Adds data to X axis
    axisX = new Axis(xVals);
    axisY = new Axis();
    setChartAxisProperties(data);
    bubbleChartView.setBubbleChartData(data);
  }

  private void setChartAxisProperties(BubbleChartData data) {
    if (bubbleChartData.getBubbleXAxis().isVisible()) {
      axisX.setName(bubbleChartData.getBubbleXAxis().getName());
      data.setAxisXBottom(axisX);
    } else {
      data.setAxisXBottom(null);
    }

    if (bubbleChartData.getBubbleYaxis().isVisible()) {
      axisY.setName(bubbleChartData.getBubbleYaxis().getName());
      data.setAxisYLeft(axisY);
    } else {
      data.setAxisYLeft(null);
    }
    if ((bubbleChartData.getBubbleYaxis().isVisible())
        && (bubbleChartData.getBubbleXAxis().isVisible())) {
      axisX.setName(bubbleChartData.getBubbleXAxis().getName());
      data.setAxisXBottom(axisX);
      axisY.setName(bubbleChartData.getBubbleYaxis().getName());
      data.setAxisYLeft(axisY);
    } else {
      data.setAxisXBottom(null);
      data.setAxisYLeft(null);
    }
  }

  private void setChartDataProperties() {
    ArrayList<Property> bubbleProperties = bubbleChartData.getProperties();
    for (Property property : bubbleProperties) {
      if (property.getType().equals("bubble-gridLines")) {
        if (Boolean.parseBoolean(property.getValue())) {
          axisX.setHasLines(true);
          axisY.setHasLines(true);
        }
      }
    }
  }
}




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