我在Android Studio中使用MPAndroiChart v3.0.3库并将LineChart代码放在onViewCreated()内的片段中,但是黄色文本" rt data av"正在屏幕中心显示,在我的高亮值文本下方。如何删除它并自定义显示的文本以增加其宽度,因为它只在一行中显示4个字符,其余的在其他行中。我试过" android:singleLine =" true""在.xml中但它只剪切了文本。
LineChart的代码:
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>