com.github.mikephil.charting.formatter.PercentFormatter Java Examples

The following examples show how to use com.github.mikephil.charting.formatter.PercentFormatter. 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: PredictionsFragment.java    From SEAL-Demo with MIT License 6 votes vote down vote up
/**
 * Updates the data in the pie chart with all the runs.
 */
private void updatePieChart() {
    ArrayList<PieEntry> entries = filterPieEntries();
    PieDataSet dataSet = new PieDataSet(entries, "Predictions");
    dataSet.setDrawIcons(false);
    dataSet.setSliceSpace(2f);
    dataSet.setIconsOffset(new MPPointF(0, 40));
    dataSet.setSelectionShift(5f);
    dataSet.setColors(new int[]{R.color.classification_low_intensity, R.color.classification_medium_intensity, R.color.classification_high_intensity}, getActivity());
    PieData data = new PieData(dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(22f);
    data.setValueTextColor(Color.WHITE);
    mChart.setData(data);
    mChart.invalidate();
}
 
Example #2
Source File: HalfPieChartActivity.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
private void setData(int count, float range) {

        ArrayList<PieEntry> values = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            values.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i % parties.length]));
        }

        PieDataSet dataSet = new PieDataSet(values, "Election Results");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        data.setValueTypeface(tfLight);
        chart.setData(data);

        chart.invalidate();
    }
 
Example #3
Source File: RealmBaseActivity.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
protected void setup(Chart<?> chart) {

        mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

        // no description text
        chart.setDescription("");
        chart.setNoDataTextDescription("You need to provide data for the chart.");

        // enable touch gestures
        chart.setTouchEnabled(true);

        if (chart instanceof BarLineChartBase) {

            BarLineChartBase mChart = (BarLineChartBase) chart;

            mChart.setDrawGridBackground(false);

            // enable scaling and dragging
            mChart.setDragEnabled(true);
            mChart.setScaleEnabled(true);

            // if disabled, scaling can be done on x- and y-axis separately
            mChart.setPinchZoom(false);

            YAxis leftAxis = mChart.getAxisLeft();
            leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
            leftAxis.setTypeface(mTf);
            leftAxis.setTextSize(8f);
            leftAxis.setTextColor(Color.DKGRAY);
            leftAxis.setValueFormatter(new PercentFormatter());

            XAxis xAxis = mChart.getXAxis();
            xAxis.setTypeface(mTf);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setTextSize(8f);
            xAxis.setTextColor(Color.DKGRAY);

            mChart.getAxisRight().setEnabled(false);
        }
    }
 
Example #4
Source File: NodeInfoFragment.java    From android-wallet-app with GNU General Public License v3.0 5 votes vote down vote up
private void setData(long tips, long transactionsToRequest) {

        ArrayList<PieEntry> entries = new ArrayList<>();

        entries.add(new PieEntry(tips, getString(R.string.tips) + " " + "(" + tips + ")"));
        entries.add(new PieEntry(transactionsToRequest, getString(R.string.transactions_to_request) + " " + "(" + transactionsToRequest + ")"));

        PieDataSet dataSet = new PieDataSet(entries, getString(R.string.transactions) + "\n(" + (tips + transactionsToRequest) + ")");

        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        // add a lot of colors

        ArrayList<Integer> colors = new ArrayList<>();

        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);

        dataSet.setColors(colors);

        dataSet.setValueLinePart1OffsetPercentage(80.f);
        dataSet.setValueLinePart1Length(0.2f);
        dataSet.setValueLinePart2Length(0.4f);
        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setValueTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(12f);
        data.setValueTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
        chart.setData(data);

        // undo all highlights
        chart.highlightValues(null);
        chart.invalidate();
    }
 
Example #5
Source File: ProductDetailActivity.java    From FaceT with Mozilla Public License 2.0 5 votes vote down vote up
private void setData(PieChart colorPie, float value) {

        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        float result = value / 5f;
        Log.d(TAG + " result ", result + "");
        entries.add(new PieEntry(result, 0));
        entries.add(new PieEntry(1 - result, 1));
        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.

//        colorPie.setCenterTextTypeface(mTfLight);
        int centerTextColor = android.graphics.Color.argb(255, 57, 197, 193);
        colorPie.setCenterTextColor(centerTextColor);

        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(3f);

        // add a lot of colors
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(Color.argb(120, 57, 197, 193));
        colorPie.setCenterText(value + "");
        colorPie.setCenterTextSize(30);

        colors.add(Color.argb(100, 214, 214, 214));
        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(0f);
        data.setValueTextColor(Color.WHITE);
        colorPie.setData(data);

        // undo all highlights
        colorPie.highlightValues(null);

        colorPie.invalidate();
    }
 
Example #6
Source File: Results.java    From NoiseCapture with GNU General Public License v3.0 5 votes vote down vote up
private void setNEIData() {

        ArrayList<Entry> yVals1 = new ArrayList<Entry>();

        // IMPORTANT: In a PieChart, no values (Entry) should have the same
        // xIndex (even if from different DataSets), since no values can be
        // drawn above each other.
        yVals1.add(new Entry( record.getLeqMean(), 0));

        ArrayList<String> xVals = new ArrayList<String>();

        xVals.add(catNE[0 % catNE.length]);

        PieDataSet dataSet = new PieDataSet(yVals1, "NEI");
        dataSet.setSliceSpace(3f);
        int nc=getNEcatColors(record.getLeqMean());    // Choose the color category in function of the sound level
        dataSet.setColor(NE_COLORS[nc]);   // Apply color category for the corresponding sound level

        PieData data = new PieData(xVals, dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);
        data.setDrawValues(false);

        neiChart.setData(data);
        neiChart.setCenterText(String.format(Locale.getDefault(), "%.1f", record.getLeqMean())
                .concat(" dB(A)" + ""));
        neiChart.invalidate();
    }
 
Example #7
Source File: StatisticsFragment.java    From Expense-Tracker-App with MIT License 5 votes vote down vote up
private void setCategoriesPieChart() {

        List<String> categoriesNames = new ArrayList<>();
        List<Entry> categoryPercentagesEntries = new ArrayList<>();

        for (int i=0; i < mCategoryList.size(); i++) {
            float percentage = Expense.getExpensesCategoryPercentage(DateManager.getInstance().getDateFrom(), DateManager.getInstance().getDateTo(), mCategoryList.get(i));
            if( percentage > 0) {
                categoriesNames.add(mCategoryList.get(i).getName());
                Entry pieEntry = new Entry(percentage, categoriesNames.size()-1);
                categoryPercentagesEntries.add(pieEntry);
            }
        }
        if (categoriesNames.isEmpty()) {
            tvPcCategoriesEmpty.setVisibility(View.VISIBLE);
            bcCategories.setVisibility(View.GONE);
        } else {
            tvPcCategoriesEmpty.setVisibility(View.GONE);
            bcCategories.setVisibility(View.VISIBLE);
        }

        PieDataSet dataSet = new PieDataSet(categoryPercentagesEntries, "Categories");
        dataSet.setSliceSpace(1f);
        dataSet.setSelectionShift(5f);

        dataSet.setColors(Util.getListColors());

        PieData data = new PieData(categoriesNames, dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(getResources().getColor(R.color.primary_dark));
        pcCategories.setData(data);
        pcCategories.invalidate();

    }
 
Example #8
Source File: StatisticActivity.java    From memorize with MIT License 4 votes vote down vote up
@Override
public void setStatData(int total, int memorized, int favorited, int active) {

    ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

    entries.add(new PieEntry((float) memorized,
            "Цээжилсэн үг " + memorized,
            getResources().getDrawable(R.drawable.ic_timeline)));
    entries.add(new PieEntry((float) favorited,
            "Цээжилж байгаа " + favorited,
            getResources().getDrawable(R.drawable.ic_timeline)));
    entries.add(new PieEntry((float) active,
            "Цээжлээгүй " + active,
            getResources().getDrawable(R.drawable.ic_timeline)));

    PieDataSet dataSet = new PieDataSet(entries, "Memorize results");

    dataSet.setDrawIcons(false);

    dataSet.setSliceSpace(3f);
    dataSet.setIconsOffset(new MPPointF(0, 40));
    dataSet.setSelectionShift(5f);

    // add a lot of colors

    ArrayList<Integer> colors = new ArrayList<Integer>();

    colors.add(getResources().getColor(R.color.chartGreen));
    colors.add(getResources().getColor(R.color.chartBlue));
    colors.add(getResources().getColor(R.color.chartPink));
    dataSet.setColors(colors);
    //dataSet.setSelectionShift(0f);

    PieData data = new PieData(dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(11f);
    data.setValueTextColor(Color.WHITE);

    SpannableString s = new SpannableString("Нийт үг :" + total);
    s.setSpan(new RelativeSizeSpan(1.7f), 0, s.length(), 0);
    s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 0, s.length(), 0);


    pieData = new PieData(dataSet);
}
 
Example #9
Source File: PieChartItem.java    From memorize with MIT License 4 votes vote down vote up
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, Context c) {

    ViewHolder holder;

    if (convertView == null) {

        holder = new ViewHolder();

        convertView = LayoutInflater.from(c).inflate(
                R.layout.stats_item_piechart, null);
        holder.chart = convertView.findViewById(R.id.chart);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // apply styling
    holder.chart.getDescription().setEnabled(false);
    holder.chart.setHoleRadius(52f);
    holder.chart.setTransparentCircleRadius(57f);
    holder.chart.setCenterText(mCenterText);
    holder.chart.setCenterTextSize(9f);
    holder.chart.setUsePercentValues(true);
    holder.chart.setExtraOffsets(5, 10, 50, 10);

    mChartData.setValueFormatter(new PercentFormatter());
    mChartData.setValueTextSize(11f);
    mChartData.setValueTextColor(Color.WHITE);
    // set data
    holder.chart.setData((PieData) mChartData);

    Legend l = holder.chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);

    holder.chart.animateY(900);

    return convertView;
}
 
Example #10
Source File: PieChartItem.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, Context c) {

    ViewHolder holder;

    if (convertView == null) {

        holder = new ViewHolder();

        convertView = LayoutInflater.from(c).inflate(
                R.layout.list_item_piechart, null);
        holder.chart = convertView.findViewById(R.id.chart);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // apply styling
    holder.chart.getDescription().setEnabled(false);
    holder.chart.setHoleRadius(52f);
    holder.chart.setTransparentCircleRadius(57f);
    holder.chart.setCenterText(mCenterText);
    holder.chart.setCenterTextTypeface(mTf);
    holder.chart.setCenterTextSize(9f);
    holder.chart.setUsePercentValues(true);
    holder.chart.setExtraOffsets(5, 10, 50, 10);

    mChartData.setValueFormatter(new PercentFormatter());
    mChartData.setValueTypeface(mTf);
    mChartData.setValueTextSize(11f);
    mChartData.setValueTextColor(Color.WHITE);
    // set data
    holder.chart.setData((PieData) mChartData);

    Legend l = holder.chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);

    // do not forget to refresh the chart
    // holder.chart.invalidate();
    holder.chart.animateY(900);

    return convertView;
}
 
Example #11
Source File: PieChartActivity.java    From iMoney with Apache License 2.0 4 votes vote down vote up
@Override
protected void initData() {
    ivBack.setVisibility(View.VISIBLE);
    ivSetting.setVisibility(View.GONE);
    tvTitle.setText("饼状图");

    // 初始化字体库
    mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

    pieChart.setDescription("目前android市场的占比情况");
    // 设置内部圆的半径
    pieChart.setHoleRadius(52f);
    // 设置包裹内部圆的半径
    pieChart.setTransparentCircleRadius(67f);

    pieChart.setCenterText("Android\n市场占比");
    // 设置中间显示的文本的字体
    pieChart.setCenterTextTypeface(mTf);
    // 设置中间显示的文本的字体大小
    pieChart.setCenterTextSize(18f);
    // 显示的各个部分的占比和是否为100%
    pieChart.setUsePercentValues(true);

    // 产生饼状图的数据
    PieData mChartData = generateDataPie();

    // 设置显示数据的格式
    mChartData.setValueFormatter(new PercentFormatter());
    mChartData.setValueTypeface(mTf);
    // 设置显示各个部分的文字的字体大小
    mChartData.setValueTextSize(11f);
    // 设置显示各个部分的文字的字体颜色
    mChartData.setValueTextColor(Color.RED);
    // set data
    pieChart.setData(mChartData);

    //获取图示的说明结构
    Legend l = pieChart.getLegend();
    //设置显示的位置
    l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    //设置几项说明在Y轴方向的间距
    l.setYEntrySpace(10f);
    //设置第一项距离y轴顶部的间距
    l.setYOffset(30f);

    // do not forget to refresh the chart
    // pieChart.invalidate();
    pieChart.animateXY(900, 900);
}
 
Example #12
Source File: RealmBaseActivity.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
protected void styleData(ChartData data) {
    data.setValueTypeface(mTf);
    data.setValueTextSize(8f);
    data.setValueTextColor(Color.DKGRAY);
    data.setValueFormatter(new PercentFormatter());
}
 
Example #13
Source File: PieChartItem.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
public View getView(int position, View convertView, Context c) {

    ViewHolder holder = null;

    if (convertView == null) {

        holder = new ViewHolder();

        convertView = LayoutInflater.from(c).inflate(
                R.layout.list_item_piechart, null);
        holder.chart = (PieChart) convertView.findViewById(R.id.chart);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // apply styling
    holder.chart.setDescription("");
    holder.chart.setHoleRadius(52f);
    holder.chart.setTransparentCircleRadius(57f);
    holder.chart.setCenterText(mCenterText);
    holder.chart.setCenterTextTypeface(mTf);
    holder.chart.setCenterTextSize(9f);
    holder.chart.setUsePercentValues(true);
    holder.chart.setExtraOffsets(5, 10, 50, 10);

    mChartData.setValueFormatter(new PercentFormatter());
    mChartData.setValueTypeface(mTf);
    mChartData.setValueTextSize(11f);
    mChartData.setValueTextColor(Color.WHITE);
    // set data
    holder.chart.setData((PieData) mChartData);

    Legend l = holder.chart.getLegend();
    l.setPosition(LegendPosition.RIGHT_OF_CHART);
    l.setYEntrySpace(0f);
    l.setYOffset(0f);

    // do not forget to refresh the chart
    // holder.chart.invalidate();
    holder.chart.animateY(900);

    return convertView;
}
 
Example #14
Source File: YAxisChartBase.java    From react-native-mp-android-chart with MIT License 4 votes vote down vote up
protected void setYAxisConfig(YAxis axis, ReadableMap propMap) {
    if (BridgeUtils.validate(propMap, ReadableType.Number, "axisMaxValue")) {
        axis.setAxisMaxValue((float) propMap.getDouble("axisMaxValue"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "axisMinValue")) {
        axis.setAxisMinValue((float) propMap.getDouble("axisMinValue"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Boolean, "inverted")) {
        axis.setInverted(propMap.getBoolean("inverted"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "spaceTop")) {
        axis.setSpaceTop((float) propMap.getDouble("spaceTop"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "spaceBottom")) {
        axis.setSpaceBottom((float) propMap.getDouble("spaceBottom"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Boolean, "showOnlyMinMax")) {
        axis.setShowOnlyMinMax(propMap.getBoolean("showOnlyMinMax"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "labelCount")) {
        boolean labelCountForce = false;
        if (BridgeUtils.validate(propMap, ReadableType.Boolean, "labelCountForce")) {
            labelCountForce = propMap.getBoolean("labelCountForce");
        }
        axis.setLabelCount(propMap.getInt("labelCount"), labelCountForce);
    }
    if (BridgeUtils.validate(propMap, ReadableType.String, "position")) {
        axis.setPosition(YAxis.YAxisLabelPosition.valueOf(propMap.getString("position")));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "granularity")) {
        axis.setGranularity((float) propMap.getDouble("granularity"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Boolean, "granularityEnabled")) {
        axis.setGranularityEnabled(propMap.getBoolean("granularityEnabled"));
    }


    // formatting
    if (BridgeUtils.validate(propMap, ReadableType.String, "valueFormatter")) {
        String valueFormatter = propMap.getString("valueFormatter");

        if ("largeValue".equals(valueFormatter)) {
            axis.setValueFormatter(new LargeValueFormatter());
        } else if ("percent".equals(valueFormatter)) {
            axis.setValueFormatter(new PercentFormatter());
        } else {
            axis.setValueFormatter(new CustomFormatter(valueFormatter));
        }
    }

    // TODO docs says the remaining config needs to be applied before setting data. Test it
    // zero line
    if (BridgeUtils.validate(propMap, ReadableType.Map, "zeroLine")) {
        ReadableMap zeroLineConfig = propMap.getMap("zeroLine");

        if (BridgeUtils.validate(zeroLineConfig, ReadableType.Boolean, "enabled")) {
            axis.setDrawZeroLine(zeroLineConfig.getBoolean("enabled"));
        }
        if (BridgeUtils.validate(zeroLineConfig, ReadableType.Number, "lineWidth")) {
            axis.setZeroLineWidth((float) zeroLineConfig.getDouble("lineWidth"));
        }
        if (BridgeUtils.validate(zeroLineConfig, ReadableType.String, "lineColor")) {
            axis.setZeroLineColor(Color.parseColor(zeroLineConfig.getString("lineColor")));
        }
    }
}
 
Example #15
Source File: ColorDetectionActivity.java    From FaceT with Mozilla Public License 2.0 4 votes vote down vote up
private void setData(PieChart colorPie, int order, int color) {

        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        float colorValue = color / 255f;
        entries.add(new PieEntry(colorValue, 0));
        entries.add(new PieEntry(1 - colorValue, 1));
        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.

//        colorPie.setCenterTextTypeface(mTfLight);
        colorPie.setCenterTextColor(ColorTemplate.getHoloBlue());

        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(3f);

        // add a lot of colors
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.clear();
        switch (order) {
            case 0:
                colors.add(Color.argb(100, color, 0, 0));
                colorPie.setCenterText("Red");
                break;
            case 1:
                colors.add(Color.argb(100, 0, color, 0));
                colorPie.setCenterText("Green");
                break;
            case 2:
                colors.add(Color.argb(100, 0, 0, color));
                colorPie.setCenterText("Blue");
                break;
        }
        colors.add(Color.argb(80, 214, 214, 214));
        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(0f);
        data.setValueTextColor(Color.WHITE);
        colorPie.setData(data);

        // undo all highlights
        colorPie.highlightValues(null);

        colorPie.invalidate();
    }