Android Open Source - HRForecast-WFM Line 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. j av a2s . c  o m
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
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.linechart.LinePoint;
import de.hrf.workforcemanagement.R;

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

  private Chart chart;
  private ProgressDialog loadingDialog;
  private LineChart lineChartView;
  private LineDataSet lineDataSet;
  private de.hrf.workforcemanagement.models.linechart.LineChart lineChartData;

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

    lineChartView = (LineChart) rootView.findViewById(R.id.line_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();
        lineChartView.invalidate();
        lineChartView.animateXY(1500, 1500);
      }
    });
  }

  /**
   * Load {@link LineChart} related properties.
   */
  private void loadChartViewProperties() {
    lineChartView.setDescription("");
    lineChartView.setDrawYValues(true);
    lineChartView.setDrawUnitsInChart(true);
    lineChartView.setPinchZoom(true);
    lineChartView.getXLabels().setPosition(XLabelPosition.BOTTOM);
    lineChartView.getLegend();
  }

  /**
   * 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() {
            lineChartView.setDrawGridBackground(false);
            lineChartView.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() {
              lineChartView
                  .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.linechart.LineChart} data.
   * 
   * @param lineChartView
   */
  private void loadChartData() {
    lineChartData = (de.hrf.workforcemanagement.models.linechart.LineChart) chart
        .getChartList().get(0);
    ArrayList<String> xVals = new ArrayList<String>();
    ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>();
    String[] linePointYVals = new String[0];
    LinePoint linePoint = new LinePoint();
    // Adds Y Values
    for (int index = 0; index < lineChartData.getLineYaxis()
        .getDimensions().size(); index++) {
      ArrayList<Entry> yVals = new ArrayList<Entry>();
      for (int i = 0; i < lineChartData.getLineData().size(); i++) {
        linePoint = lineChartData.getLineData().get(i);
        linePointYVals = linePoint.getyValue().split(",");
        yVals.add(new Entry(Float.parseFloat(linePointYVals[index]), i));
      }
      lineDataSet = new LineDataSet(yVals, lineChartData.getLineYaxis()
          .getDimensions().get(index).getText());
      // Formats DataSet
      lineDataSet.setCircleColor(Color.BLACK);
      lineDataSet.setLineWidth(4f);
      lineDataSet.setCircleSize(6f);
      lineDataSet.setFillAlpha(65);
      lineDataSets.add(lineDataSet);
    }

    // Adds X Values
    for (int i = 0; i < lineChartData.getLineData().size(); i++) {
      linePoint = lineChartData.getLineData().get(i);
      xVals.add(linePoint.getxValue());
    }
    LineData data = new LineData(xVals, lineDataSets);
    lineChartView.setData(data);
    setLineDataColor(lineDataSets);
  }

  private void setChartDataProperties() {
    ArrayList<Property> lineProperties = lineChartData.getProperties();
    for (Property property : lineProperties) {
      if (property.getType().equals("line-gridLines")) {
        if (property.getValue().equals("false")) {
          lineChartView.setDrawHorizontalGrid(false);
          lineChartView.setDrawVerticalGrid(false);
        }
      } else if (property.getType().equals("line-label-fontSize")) {
        if (property.getValue().equals("small")) {
          lineChartView.setValueTextSize(6f);
          lineChartView.getXLabels().setTextSize(6f);
        } else if (property.getValue().equals("normal")) {
          lineChartView.setValueTextSize(12f);
          lineChartView.getXLabels().setTextSize(12f);
        } else if (property.getValue().equals("large")) {
          lineChartView.setValueTextSize(16f);
          lineChartView.getXLabels().setTextSize(16f);
        }
      } else if (property.getType().equals("line-pointWidth")) {
        // to do
      } else if (property.getType().equals("line-label-color")) {
        lineChartView.setValueTextColor(Color.parseColor(property
            .getValue()));
        lineChartView.getXLabels().setTextColor(
            Color.parseColor(property.getValue()));
      }

    }

  }

  private void setChartAxisProperties() {
    if (lineChartData.getLineXAxis().isVisible() == false) {
      lineChartView.setDrawXLabels(false);
    }

    if (lineChartData.getLineYaxis().isVisible() == false) {
      lineChartView.setDrawYLabels(false);
      lineChartView.setDrawYValues(false);
    }

    if ((lineChartData.getLineXAxis().isVisible() == false)
        && (lineChartData.getLineYaxis().isVisible() == false)) {
      lineChartView.setDrawYValues(false);
      lineChartView.setDrawXLabels(false);
      lineChartView.setDrawYLabels(false);
    }
  }

  private void setLineDataColor(ArrayList<LineDataSet> lineDataSets) {
    ArrayList<de.hrf.workforcemanagement.models.Color> lineColors = lineChartData
        .getLineData().get(0).getColors();
    for (int index = 0; index < lineColors.size(); index++) {
      lineDataSets.get(index).setColor(
          Color.parseColor(lineColors.get(index).getColorHexCode()));
    }
  }
}




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