gpt4 book ai didi

java - 如何在饼图之外显示测试

转载 作者:行者123 更新时间:2023-12-02 09:31:10 25 4
gpt4 key购买 nike

我正在使用 MP Android Chart Lib,我想隐藏购物车内的 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 步:(项目)

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

}
}

如果有效,请重播我

谢谢。
亚什·谢卡达

关于java - 如何在饼图之外显示测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com