如何显示测试外饼图

时间:2019-09-16 11:51:25

标签: java android android-studio mpandroidchart

我正在使用MP Android图表库,我想在购物车中隐藏textview并在图表外显示文本

像这样- image here

如何在饼图外显示文本并在其中隐藏文本。 并且可以使用图片中显示的图片

我使用的代码-

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    PieChart pieChart = (PieChart) findViewById(R.id.piechart);
        pieChart.setUsePercentValues(true);

        datatext = (TextView)findViewById(R.id.datatext);

        datatitle = (TextView)findViewById(R.id.datatitle);

        // 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.
    ArrayList<Entry> yvalues = new ArrayList<Entry>();
        yvalues.add(new Entry(24f, 0));
        yvalues.add(new Entry(15f, 1));
        yvalues.add(new Entry(19f, 2));
        yvalues.add(new Entry(22f, 3));
        yvalues.add(new Entry(20f, 4));
        //yvalues.add(new Entry(17f, 5));

    PieDataSet dataSet = new PieDataSet(yvalues, "Balances");

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

        xVals.add("Monthly Expenses");
        xVals.add("Phonepe");
        xVals.add("Uber");
        xVals.add("Paytm");
        xVals.add("Savings");
       // xVals.add("Ola");
        dataSet.setDrawValues(false);
    PieData data = new PieData(xVals, dataSet);
    // In Percentage term
        data.setValueFormatter(new PercentFormatter());
    // Default value
    //data.setValueFormatter(new DefaultValueFormatter(0));
        pieChart.setData(data);
        pieChart.setDescription("Savings");

        pieChart.setDrawHoleEnabled(true);
        pieChart.setTransparentCircleRadius(25f);
        pieChart.setHoleRadius(25f);

        dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);

        data.setValueTextSize(13f);
        data.setValueTextColor(Color.DKGRAY);
        pieChart.setOnChartValueSelectedListener(this);

        pieChart.animateXY(1400, 1400);


}

    @Override
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

        
        if (e == null)
            return;
        Log.i("VAL SELECTED",
                "Value: " + e.getVal() + ", xIndex: " + e.getXIndex()
                        + ", DataSet index: " + dataSetIndex);

        float datatt = e.getVal();
        Toast.makeText(this, ""+datatt, Toast.LENGTH_SHORT).show();
        datatext.setText(""+datatt);
        //datatitle.setText(""+title);
    }

请帮助我

1 个答案:

答案 0 :(得分:1)

我很乐意为您服务,

尝试一下

步骤1 :(项目)

 maven { url 'https://jitpack.io' }

第2步:(模块)

 implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

步骤3:(activity_main.xml)

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

步骤4:(MainActivity.java)

package com.jdevio.test;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;


import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
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;

public class MainActivity extends AppCompatActivity implements
        OnChartValueSelectedListener {
    PieChart pieChart ;
    ArrayList<PieEntry> entries ;
    ArrayList<String> PieEntryLabels ;
    PieDataSet pieDataSet ;
    PieData pieData ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        entries = new ArrayList<PieEntry>();

      pieChart=(PieChart)findViewById(R.id.chart1);

        PieEntryLabels = new ArrayList<String>();

        AddValuesToPIEENTRY();



        PieDataSet dataSet = new PieDataSet(entries, "Money Bank 2019");

        dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
        dataSet.setSliceSpace(2f);
        dataSet.setValueTextColor(Color.WHITE);
        dataSet.setValueTextSize(12f);

        PieData pdataSet = new PieData(dataSet);

        pieChart.setData(pdataSet);

        pieChart.animateY(300);

    }

    public void AddValuesToPIEENTRY(){

        entries.add(new PieEntry(24f, "Monthly Expenses  " ));
        entries.add(new PieEntry(15f, "Phonepe"));
        entries.add(new PieEntry(19f, "Uber"));
        entries.add(new PieEntry(22f, "Paytm"));
        entries.add(new PieEntry(20f, "Savings"));
        entries.add(new PieEntry(17f, "Ola"));

    }



    @Override
    public void onValueSelected(Entry e, Highlight h) {

       // selected value

    }


    @Override
    public void onNothingSelected() {

    }
}

如果可以正常播放,请重播我

谢谢。
亚什·谢赫达(Yash shekhada)