有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在饼图外显示测试

我使用的是MP Android图表库,我想将文本视图隐藏在购物车内,并在图表外显示文本

像这样- 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) 个答案

  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() {
    
        }
    }
    

    如果有效,请重播我

    谢谢
    亚什·谢哈达