2016-03-07 65 views
1

如此折线图image所示。如何使弹出窗口显示x和y轴的值。当我触摸折线图中的节点时,我希望在相应的位置上显示相应的X,Y值的弹出窗口。就像上面的图片链接一样。如何使弹出窗口显示x和y轴的值

final LineChart lineChart = (LineChart) findViewById(R.id.chart); 

    XAxis xl = lineChart.getXAxis(); 
    xl.setPosition(XAxis.XAxisPosition.BOTTOM); 
    ArrayList<Entry> entries = new ArrayList<>(); 
    entries.add(new Entry(4f, 0)); 
    entries.add(new Entry(8f, 1)); 
    entries.add(new Entry(6f, 2)); 
    entries.add(new Entry(2f, 3)); 
    entries.add(new Entry(18f, 4)); 
    entries.add(new Entry(9f, 5)); 

    final LineDataSet dataset = new LineDataSet(entries , "y-axies"); 
    // creating labels 
    final ArrayList<String> labels = new ArrayList<String>(); 
    labels.add("January"); 
    labels.add("February"); 
    labels.add("March"); 
    labels.add("April"); 
    labels.add("May"); 
    labels.add("June"); 

    LineData data = new LineData(labels, dataset); 
    lineChart.setData(data); // set the data and list of lables into chart 
    lineChart.setDescription("Description"); // set the description 
    dataset.setDrawCubic(true); 
    dataset.setDrawFilled(true); 
    dataset.setHighlightEnabled(true); 
    lineChart.setTouchEnabled(true); 
    dataset.setColors(ColorTemplate.COLORFUL_COLORS); 
    lineChart.animateY(5000); 
+0

你面临什么问题... – SSH

+0

欢迎来到SOF亲爱的! @Bhagya Shri –

+0

你想在弹出窗口中显示折线图? –

回答

-1

如果使用MPAndroidChart库,可以使用原生MarkerView类实现此目的。描述你可以在Github托管的项目上找到wiki。

编辑: 我添加了一些信息,我们如何实现这一目标: 1.您必须实现MarkerView类,以便在图表上绘制弹出窗口。 Example类:

public class CustomMarkerView extends MarkerView { 

private TextView tvContent; 

public CustomMarkerView (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) { 
    tvContent.setText("" + e.getVal()); // set the entry-value as the display text 
} 

@Override 
public int getXOffset(float xpos) { 
    // this will center the marker-view horizontally 
    return -(getWidth()/2); 
} 

@Override 
public int getYOffset(float ypos) { 
    // this will cause the marker-view to be above the selected value 
    return -getHeight(); 
} 

2.如果你有这个,下次你必须定义你的弹出窗口的布局xml文件。示例XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="40dp" android:background="@drawable/markerImage" > <TextView android:id="@+id/tvContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="" android:textSize="12dp" android:textColor="@android:color/white" android:ellipsize="end" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout>

  • 后,您只能附加您的自定义类chartView:
  • CustomMarkerView mv = new CustomMarkerView(Context, R.layout.custom_marker_view_layout); chart.setMarkerView(mv);

    之后,你将有充分的功能弹出式菜单。链接到网站进一步的信息:https://github.com/PhilJay/MPAndroidChart/wiki/MarkerView

    +0

    虽然此链接可能回答此问题,但最好在此处包含答案的基本部分并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/14054890) –

    +0

    正如你所建议的,我添加了一个实现的例子。不要投票。 – MiXen