MPAndroidChart 库 - "rt data av" 在设置 MarkerView 后显示为黄色
MPAndroidChart Library - "rt data av" in yellow showed after setting MarkerView
我在 Android Studio 中使用 MPAndroiChart v3.0.3 库,并将 LineChart 代码放在片段中的 onViewCreated() 内,但屏幕上显示黄色文本 "rt data av"中心,在我的高亮值文本下方。我怎样才能删除它并自定义显示的文本以增加其宽度,因为它只在一行中显示 4 个字符,其余的在其他行中。我在 .xml 中尝试了 "android:singleLine="true"" 但它只剪切了文本。
折线图的代码:
LineChart lineChart = view.findViewById(R.id.lineChart);
ArrayList<String> xAXES = new ArrayList<>();
ArrayList<Entry> yAXESsin = new ArrayList<>();
ArrayList<Entry> yAXEScos = new ArrayList<>();
double x = 0 ;
int numDataPoints = 256;
for(int i=0;i<numDataPoints;i++){
float sinFunction = Float.parseFloat(String.valueOf(Math.sin(x)));
float cosFunction = Float.parseFloat(String.valueOf(Math.cos(x)));
x = x + 0.1;
yAXESsin.add(new Entry(i,100*sinFunction));
yAXEScos.add(new Entry(i,cosFunction));
xAXES.add(i, String.valueOf(x));
}
ArrayList<ILineDataSet> lineDataSets = new ArrayList<>();
//Customize Data
LineDataSet lineDataSet1 = new LineDataSet(yAXEScos,"cos");
lineDataSet1.setDrawCircles(false);
lineDataSet1.setColor(Color.BLUE);
lineDataSet1.setAxisDependency(YAxis.AxisDependency.RIGHT);
lineDataSet1.setHighLightColor(Color.RED);
LineDataSet lineDataSet2 = new LineDataSet(yAXESsin,"sin");
lineDataSet2.setDrawCircles(false);
lineDataSet2.setColor(Color.BLACK);
lineDataSet2.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet2.setHighLightColor(Color.RED);
lineDataSets.add(lineDataSet1);
lineDataSets.add(lineDataSet2);
LineData lineData = new LineData(lineDataSets);
lineData.setDrawValues(true); //Show values
//Set X Axis
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//Set Marker
YourMarkerView marker = new YourMarkerView(getContext(),R.layout.fragment_tab3);
lineChart.setMarker(marker);
//Customize graph
lineChart.setHighlightPerTapEnabled(true);
lineChart.setHighlightPerDragEnabled(true);
lineChart.setPinchZoom(false);
lineChart.setDoubleTapToZoomEnabled(false);
lineChart.animateXY(1000, 1000);
lineChart.setVisibleXRangeMaximum(65f);
//Send Data
lineChart.setData(lineData);
lineChart.invalidate();
MarkerView 的代码:
public class YourMarkerView extends MarkerView {
private TextView tvContent;
public YourMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// find your layout components
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) {
tvContent.setText("" + e.getY());
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}}
Fragment_tab3.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.mytabs2.Tab3">
<!-- TODO: Update blank fragment layout -->
<com.github.mikephil.charting.charts.LineChart
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lineChart" >
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="@android:color/black" />
</com.github.mikephil.charting.charts.LineChart>
将 xml 布局文件作为 custom_marker_view.xml 添加到您的布局目录中。
您的 xml 文件的代码应该类似于:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="@drawable/markers_bg"
>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="15dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
从您的 fragment_3.xml 文件中删除 editText,使其变为:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.mytabs2.Tab3">
<com.github.mikephil.charting.charts.LineChart
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lineChart" >
</com.github.mikephil.charting.charts.LineChart>
</RelativeLayout>
您需要将以下行中的 fragment_tab3 替换为 custom_marker_view:
//Set Marker
YourMarkerView marker = new YourMarkerView(getContext(),R.layout.custom_marker_view);
lineChart.setMarker(marker);
现在你可以开始了。完成!编码愉快!
我在 Android Studio 中使用 MPAndroiChart v3.0.3 库,并将 LineChart 代码放在片段中的 onViewCreated() 内,但屏幕上显示黄色文本 "rt data av"中心,在我的高亮值文本下方。我怎样才能删除它并自定义显示的文本以增加其宽度,因为它只在一行中显示 4 个字符,其余的在其他行中。我在 .xml 中尝试了 "android:singleLine="true"" 但它只剪切了文本。
折线图的代码:
LineChart lineChart = view.findViewById(R.id.lineChart);
ArrayList<String> xAXES = new ArrayList<>();
ArrayList<Entry> yAXESsin = new ArrayList<>();
ArrayList<Entry> yAXEScos = new ArrayList<>();
double x = 0 ;
int numDataPoints = 256;
for(int i=0;i<numDataPoints;i++){
float sinFunction = Float.parseFloat(String.valueOf(Math.sin(x)));
float cosFunction = Float.parseFloat(String.valueOf(Math.cos(x)));
x = x + 0.1;
yAXESsin.add(new Entry(i,100*sinFunction));
yAXEScos.add(new Entry(i,cosFunction));
xAXES.add(i, String.valueOf(x));
}
ArrayList<ILineDataSet> lineDataSets = new ArrayList<>();
//Customize Data
LineDataSet lineDataSet1 = new LineDataSet(yAXEScos,"cos");
lineDataSet1.setDrawCircles(false);
lineDataSet1.setColor(Color.BLUE);
lineDataSet1.setAxisDependency(YAxis.AxisDependency.RIGHT);
lineDataSet1.setHighLightColor(Color.RED);
LineDataSet lineDataSet2 = new LineDataSet(yAXESsin,"sin");
lineDataSet2.setDrawCircles(false);
lineDataSet2.setColor(Color.BLACK);
lineDataSet2.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet2.setHighLightColor(Color.RED);
lineDataSets.add(lineDataSet1);
lineDataSets.add(lineDataSet2);
LineData lineData = new LineData(lineDataSets);
lineData.setDrawValues(true); //Show values
//Set X Axis
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//Set Marker
YourMarkerView marker = new YourMarkerView(getContext(),R.layout.fragment_tab3);
lineChart.setMarker(marker);
//Customize graph
lineChart.setHighlightPerTapEnabled(true);
lineChart.setHighlightPerDragEnabled(true);
lineChart.setPinchZoom(false);
lineChart.setDoubleTapToZoomEnabled(false);
lineChart.animateXY(1000, 1000);
lineChart.setVisibleXRangeMaximum(65f);
//Send Data
lineChart.setData(lineData);
lineChart.invalidate();
MarkerView 的代码:
public class YourMarkerView extends MarkerView {
private TextView tvContent;
public YourMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// find your layout components
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) {
tvContent.setText("" + e.getY());
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}}
Fragment_tab3.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.mytabs2.Tab3">
<!-- TODO: Update blank fragment layout -->
<com.github.mikephil.charting.charts.LineChart
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lineChart" >
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="@android:color/black" />
</com.github.mikephil.charting.charts.LineChart>
将 xml 布局文件作为 custom_marker_view.xml 添加到您的布局目录中。
您的 xml 文件的代码应该类似于:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="75dp" android:layout_height="75dp" android:background="@drawable/markers_bg" > <TextView android:id="@+id/tvContent" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:paddingBottom="15dp" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout>
从您的 fragment_3.xml 文件中删除 editText,使其变为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.android.mytabs2.Tab3"> <com.github.mikephil.charting.charts.LineChart android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lineChart" > </com.github.mikephil.charting.charts.LineChart> </RelativeLayout>
您需要将以下行中的 fragment_tab3 替换为 custom_marker_view:
//Set Marker
YourMarkerView marker = new YourMarkerView(getContext(),R.layout.custom_marker_view);
lineChart.setMarker(marker);
现在你可以开始了。完成!编码愉快!