Java Code Examples for com.github.mikephil.charting.data.PieDataSet#setValueFormatter()

The following examples show how to use com.github.mikephil.charting.data.PieDataSet#setValueFormatter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChartSettings.java    From BrainPhaser with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Applies the specified format to the PieDataSet Object.
 *
 * @param dataset the dataset which will be formatted
 * @param type    the statistic type of the chart the format is applied to
 */
public void applyDataSetSettings(PieDataSet dataset, StatisticType type) {
    dataset.setSliceSpace(SLICE_SPACE);
    dataset.setValueTextSize(VALUE_TEXT_SIZE);
    dataset.setSelectionShift(SELECTION_SHIFT);
    if (type == StatisticType.TYPE_STAGE) {
        dataset.setColors(mColorsetStage);
    } else if (type == StatisticType.TYPE_DUE) {
        dataset.setColors(mColorsetDue);
    } else {
        dataset.setColors(mColorsetPlayed);
    }

    dataset.setValueFormatter(new CustomizedFormatter());
}
 
Example 2
Source File: StatisticsActivity.java    From ActivityDiary with GNU General Public License v3.0 4 votes vote down vote up
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)

    List<PieEntry> entries = new ArrayList<>();
    List<Integer> colors = new ArrayList<>();

    int portion_idx = data.getColumnIndex(ActivityDiaryContract.DiaryStats.PORTION);
    int name_idx = data.getColumnIndex(ActivityDiaryContract.DiaryStats.NAME);
    int col_idx = data.getColumnIndex(ActivityDiaryContract.DiaryStats.COLOR);
    int dur_idx = data.getColumnIndex(ActivityDiaryContract.DiaryStats.DURATION);

    if ((data != null) && data.moveToFirst()) {
        float acc = 0.0f;
        float acc_po = 0.0f;
        while (!data.isAfterLast()) {
            float portion = data.getFloat(portion_idx);
            long duration = data.getLong(dur_idx);
            if(portion > 3.0f){
                PieEntry ent = new PieEntry((float)duration, data.getString(name_idx));
                entries.add(ent);
                colors.add(data.getInt(col_idx));
            }else{
                // accumulate the small, not shown entries
                acc += duration;
                acc_po += portion;
            }
            data.moveToNext();
        }
        if(acc_po > 2.0f) {
            entries.add(new PieEntry(acc, getResources().getString(R.string.statistics_others)));
            colors.add(Color.GRAY);
        }
    }

    PieDataSet set = new PieDataSet(entries, getResources().getString(R.string.activities));
    PieData dat = new PieData(set);
    set.setColors(colors);

    set.setValueFormatter(new IValueFormatter() {
        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
            PieEntry e = (PieEntry)entry;
            return TimeSpanFormatter.format((long)e.getValue());
        }
    });
    chart.setData(dat);
    chart.setUsePercentValues(true);
    chart.setRotationAngle(180.0f);
    chart.invalidate(); // refresh

}