Android Open Source - HRForecast-WFM Line 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;
//from   w  w  w. 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 lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.ValueFormatter;
import lecho.lib.hellocharts.view.ColumnChartView;
import lecho.lib.hellocharts.view.ComboLineColumnChartView;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;

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 android.widget.Toast;
import de.hrf.workforcemanagement.listener.ChartValueSelectedListener;
import de.hrf.workforcemanagement.models.Chart;
import de.hrf.workforcemanagement.models.ChartType;
import de.hrf.workforcemanagement.models.Property;

import de.hrf.workforcemanagement.models.Bar;
import de.hrf.workforcemanagement.models.linechart.LinePoint;

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

  private Chart chart;
  private de.hrf.workforcemanagement.models.barchart.BarChart parsedBarChart;
  private de.hrf.workforcemanagement.models.linechart.LineChart parsedLineChart;
  private ProgressDialog loadingDialog;
  private ComboLineColumnChartView lineBarChartView;
  private ComboLineColumnChartData lineBarChartData;

  private Axis chartXAxis;
  private Axis chartYAxis;

  public LineBarChartFragment(Chart chart) {
    this.chart = chart;

    for (ChartType chartType : chart.getChartList()) {
      if (chartType instanceof de.hrf.workforcemanagement.models.linechart.LineChart) {
        this.parsedLineChart = (de.hrf.workforcemanagement.models.linechart.LineChart) chartType;
      } else {
        this.parsedBarChart = (de.hrf.workforcemanagement.models.barchart.BarChart) chartType;
      }
    }
  }

  @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.line_bar_chart_fragment,
        container, false);
    lineBarChartView = (ComboLineColumnChartView) rootView
        .findViewById(R.id.line_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() {
    loadChartData();
    loadChartProperties();
    loadChartViewProperties();
    this.getActivity().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        loadingDialog.dismiss();
        lineBarChartView.invalidate();
      }
    });
  }

  private void loadChartViewProperties() {
    lineBarChartView.setZoomEnabled(true);
    lineBarChartView.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);
    ChartValueSelectedListener entrySelectedListener = new ChartValueSelectedListener(
        this.getActivity(), parsedBarChart.getDataList());
    
    lineBarChartView.setOnValueTouchListener(entrySelectedListener);
  }

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

  private void setChartDataProperties() {
    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")) {
        setChartLabelColor(property);
      } else if (property.getType().equals("bar-label-fontSize")) {
        setChartLabelFontSize(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 setChartGridLines(Property property) {
    boolean displayGridLines = Boolean.parseBoolean(property.getValue());

    chartXAxis.setHasLines(displayGridLines);
    chartYAxis.setHasLines(displayGridLines);
  }

  private void setChartLabelColor(Property property) {
    int color = Color.parseColor(property.getValue());

    chartXAxis.setTextColor(color);
    chartYAxis.setTextColor(color);
  }

  private void setChartLabelFontSize(Property property) {
    String barLabelFontSize = property.getValue();
    float labelSize = 12f; // default value for "small"

    if (barLabelFontSize.equals("medium")) {
      labelSize = 15f;
    } else if (barLabelFontSize.equals("large")) {
      labelSize = 18f;
    }

    chartXAxis.setTextSize((int) labelSize);
    chartYAxis.setTextSize((int) labelSize);
  }

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

  private void loadChartData() {
    ColumnChartData barChartData = loadBarChartData();
    LineChartData lineChartData = loadLineChartData();

    lineBarChartData = new ComboLineColumnChartData(barChartData,
        lineChartData);

    loadChartAxis();

    lineBarChartView.setComboLineColumnChartData(lineBarChartData);
  }

  private ColumnChartData loadBarChartData() {
    List<Column> columns = new ArrayList<Column>();
    List<SubcolumnValue> values;
    Column column;

    for (Bar bar : parsedBarChart.getDataList()) {
      values = new ArrayList<SubcolumnValue>();
      float value = Float.parseFloat(bar.getYValues());
      values.add(new SubcolumnValue(value, Color.parseColor(bar
          .getColors().get(0).getColorHexCode())));

      column = new Column(values);
      column.setHasLabels(true);
      // column.setHasLabelsOnlyForSelected(false);
      columns.add(column);
    }

    ColumnChartData columnData = new ColumnChartData(columns);

    return columnData;
  }

  private LineChartData loadLineChartData() {
    List<Line> lines = new ArrayList<Line>();

    List<PointValue> values = new ArrayList<PointValue>();
    LinePoint point = null;
    for (int index = 0; index < parsedLineChart.getLineData().size(); index++) {
      point = parsedLineChart.getLineData().get(index);
      values.add(new PointValue(index,
          Float.parseFloat(point.getyValue())));
    }

    Line line = new Line(values);
    line.setColor(Color.parseColor(point.getColors().get(0)
        .getColorHexCode()));
    line.setHasLabels(true);
    line.setHasLines(true);
    line.setHasPoints(true);
    lines.add(line);

    LineChartData lineChartData = new LineChartData(lines);

    return lineChartData;
  }

  private void loadChartAxis() {
    boolean hasXAxis = parsedBarChart.getXAxis().isVisible();
    boolean hasYAxis = parsedBarChart.getYAxis().isVisible();

    ArrayList<AxisValue> xAxisValues = new ArrayList<AxisValue>();
    for (int index = 0; index < parsedBarChart.getDataList().size(); index++) {
      Bar bar = parsedBarChart.getDataList().get(index);
      xAxisValues.add(new AxisValue(index, bar.getName().toCharArray()));
    }

    if (hasXAxis) {
      chartXAxis = new Axis(xAxisValues);
      chartXAxis.setName(parsedBarChart.getXAxis().getxAxisName());
      lineBarChartData.setAxisXBottom(chartXAxis);
    } else {
      lineBarChartData.setAxisXBottom(null);
    }

    if (hasYAxis) {
      chartYAxis = new Axis();
      lineBarChartData.setAxisYLeft(chartYAxis);
    } else {
      lineBarChartData.setAxisXBottom(null);
    }
  }
}




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