在 MPAndroidChart piechart/barchart 中显示点击 slice/bar 的 Y 值
Show Y-Value of clicked slice/bar in MPAndroidChart piechart/barchart
我正在使用 MPAndroidChart 库在我的应用程序中显示一些图形(饼图和条形图)。
我不想要条形图和切片的任何文本描述,除非用户单击 slice/bar。然后只应显示此确切 slice/bar 的描述。
现在我有了某种解决方法,只要用户点击 slice/bar:
,就会 displays/undisplays 所有 Y 值
sessionsPieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
sessionsPieChart.setUsePercentValues(true);
pieDataSet.setValueTextSize(12);
}
@Override
public void onNothingSelected() {
sessionsPieChart.setUsePercentValues(false);
pieDataSet.setValueTextSize(0);
}
});
如何只显示点击的 Y 值slice/bar?
首先在您的项目中创建 MyMarkerView.java class 作为:
public class MyMarkerView extends MarkerView {
private TextView tvContent;
public MyMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// this markerview only displays a textview
tvContent = (TextView) findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight)
{
// here you can change whatever you want to show in following line as x/y or both
tvContent.setText("x: " + e.getX() + " , y: " + e.getY()); // set the entry-value as the display text
}
}
之后创建以下布局文件custom_marker.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/colorPrimary"
>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:text=""
android:textSize="12dp"
android:textColor="@android:color/black"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
在创建图表的地方添加以下代码:
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker);
combinedChart.setMarker(mv);
我正在使用 MPAndroidChart 库在我的应用程序中显示一些图形(饼图和条形图)。 我不想要条形图和切片的任何文本描述,除非用户单击 slice/bar。然后只应显示此确切 slice/bar 的描述。
现在我有了某种解决方法,只要用户点击 slice/bar:
,就会 displays/undisplays 所有 Y 值sessionsPieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
sessionsPieChart.setUsePercentValues(true);
pieDataSet.setValueTextSize(12);
}
@Override
public void onNothingSelected() {
sessionsPieChart.setUsePercentValues(false);
pieDataSet.setValueTextSize(0);
}
});
如何只显示点击的 Y 值slice/bar?
首先在您的项目中创建 MyMarkerView.java class 作为:
public class MyMarkerView extends MarkerView {
private TextView tvContent;
public MyMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// this markerview only displays a textview
tvContent = (TextView) findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight)
{
// here you can change whatever you want to show in following line as x/y or both
tvContent.setText("x: " + e.getX() + " , y: " + e.getY()); // set the entry-value as the display text
}
}
之后创建以下布局文件custom_marker.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/colorPrimary"
>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:text=""
android:textSize="12dp"
android:textColor="@android:color/black"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
在创建图表的地方添加以下代码:
MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker);
combinedChart.setMarker(mv);