MPAndroidChart库 - 设置MarkerView后显示黄色的“rt data av”

By simon at 2018-02-28 • 0人收藏 • 57人看过

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

1 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

1.在布局目录中将custommarkerview.xml添加到您的项目中。   2.为你的xml文件编码sho就像是:

<?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>
  3.从你的fragment3.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>
你需要使用custom
markerview替换以下行中的fragmenttab3:
//Set Marker
YourMarkerView marker = new YourMarkerView(getContext(),R.layout.custom_marker_view);
lineChart.setMarker(marker);
现在你很好走。完成!快乐编码!

登录后方可回帖

Loading...