de.aw.monma.charts.ChartTemplate.java Source code

Java tutorial

Introduction

Here is the source code for de.aw.monma.charts.ChartTemplate.java

Source

/*
 * MonMa: Eine freie Android-Application fuer die Verwaltung privater Finanzen
 *
 * Copyright [2015] [Alexander Winkler, 2373 Dahme/Germany]
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if
 * not, see <http://www.gnu.org/licenses/>.
 */
package de.aw.monma.charts;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

import de.aw.awlib.fragments.AWLoaderFragment;
import de.aw.monma.R;
import de.aw.monma.monmamain.MonMaInterface;

/**
 * Template fuer Charts.
 */
public abstract class ChartTemplate extends AWLoaderFragment
        implements MonMaInterface, OnChartValueSelectedListener {
    private static final int layout = R.layout.chart_default;
    private Chart mChart;
    /**
     * Listener fuer Click auf Chart. Kann Activity implementieren, dann wird diese direkt
     * aufgerufen. Siehe auch {@link de.aw.monma.charts.ChartTemplate.OnChartValueSelectedListener}
     */
    private OnChartValueSelectedListener mOnChartValueSelectedListener;

    /**
     * Liefert die Daten fuer das Chart. Wird nach erstellen des Cursors aufgerufen. Aufgerufene
     * Methode muss {@link Chart#setData(ChartData)} auf das mitgelieferte Chart aufrufen.
     *
     * @param c
     *         Cursor mit Daten
     * @param chart
     *         Chart, welches in {@link ChartTemplate#getChart()} geliefert wurde.
     */
    protected abstract void generateData(Cursor c, Chart chart);

    /**
     * @return das Chart
     */
    protected abstract Chart getChart();

    /**
     * Erstellt eine Defaultbelegung der Chart-Farben.
     *
     * @return ArrayList mit Farben.
     */
    protected ArrayList<Integer> getChartColors() {
        ArrayList<Integer> colors = new ArrayList<>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS) {
            colors.add(c);
        }
        for (int c : ColorTemplate.COLORFUL_COLORS) {
            colors.add(c);
        }
        for (int c : ColorTemplate.LIBERTY_COLORS) {
            colors.add(c);
        }
        for (int c : ColorTemplate.PASTEL_COLORS) {
            colors.add(c);
        }
        colors.add(ColorTemplate.getHoloBlue());
        return colors;
    }

    /**
     * @return ChartTyp gemaess {@link ChartTyp}
     */
    protected abstract ChartTyp getChartTyp();

    /**
     * @return Beschreibung des Charts
     */
    protected abstract String getDescription();

    /**
     * @return Selection, wie auf Datenbank zugegriffen werden soll
     */
    protected abstract String getSelection();

    @Override
    public void onAttach(Context activity) {
        super.onAttach(activity);
        try {
            mOnChartValueSelectedListener = (OnChartValueSelectedListener) activity;
        } catch (ClassCastException e) {
            //Nix tun - muss Activity nicht implementieren
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        super.onLoadFinished(loader, cursor);
        generateData(cursor, mChart);
        mChart.invalidate();
    }

    @Override
    public void onNothingSelected() {
    }

    @Override
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
        if (mOnChartValueSelectedListener != null) {
            mOnChartValueSelectedListener.onChartValueSelected(getChartTyp(), e, dataSetIndex, h);
        }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewGroup mContainer = view.findViewById(R.id.chartDetails);
        mChart = getChart();
        setAttributes();
        mContainer.addView(mChart);
    }

    /**
     * Setzt die allgemeinen Chart-Attribute.
     * <p/>
     * Gesetzt werden: Textgroesse Description, Textgroesse und Position Legende, NoDataText
     */
    private void setAttributes() {
        if (mChart instanceof PieChart) {
            setChartAttributes((PieChart) mChart);
        }
        if (mChart instanceof BarChart) {
            setChartAttributes((BarChart) mChart);
        }
        mChart.setDescription(getDescription());
        mChart.setDescriptionTextSize(16f);
        mChart.setNoDataText(getString(R.string.awlib_tvNoEntries));
        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.LEFT_OF_CHART);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(5f);
        l.setTextSize(13f);
        mChart.setOnChartValueSelectedListener(this);
    }

    /**
     * Setzt allgemeine Attribute fuer PieCharts
     *
     * @param chart
     *         Chart
     */
    protected void setChartAttributes(PieChart chart) {
        chart.setUsePercentValues(false);
        chart.setDrawHoleEnabled(false);
        chart.setTransparentCircleColor(Color.WHITE);
        chart.setTransparentCircleRadius(61f);
    }

    /**
     * Setzt allgemeine Attribute fuer BarCharts
     *
     * @param chart
     *         Chart
     */
    protected void setChartAttributes(BarChart chart) {
        chart.setDrawGridBackground(false);
        chart.setDrawBarShadow(false);
        chart.getAxisRight().setEnabled(false);
    }

    @Override
    public void setCursorLoaderArguments(int p1, Bundle args) {
        args.putString(SELECTION, getSelection());
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putInt(LAYOUT, layout);
    }

    public interface OnChartValueSelectedListener {
        /**
         * Wird bei Click auf einen Chart gerufen.
         *
         * @param chartTyp
         *         Ermittelt aus {@link ChartTemplate#getChartTyp()}.
         * @param e
         *         Entry. Siehe {@link Entry}
         * @param dataSetIndex
         *         The index in the datasets array of the data object the Entrys DataSet is in.
         * @param h
         *         the corresponding highlight object that contains information about the
         *         highlighted position
         */
        void onChartValueSelected(ChartTyp chartTyp, Entry e, int dataSetIndex, Highlight h);
    }
}