gpt4 book ai didi

java - 如何在折线图中的标记 View 上显示 X 标签?

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:44 26 4
gpt4 key购买 nike

如何在标记 View 上的折线图中显示 x 轴值。我使用 getVal() 函数来获取 y 轴值,但如何在标记 View 上获取 x 轴值。下面是我的标记 View 代码。

    public class MymarkerView extends MarkerView
{
private TextView indices;

public MymarkerView(Context context, int layoutResource) {
super(context, layoutResource);
indices = (TextView) findViewById(R.id.indices);
}

// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {

indices.setText("Indices:" +e.getVal());
}

@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();
}
public void draw(Canvas canvas, float posx, float posy)
{
// take offsets into consideration
posx += getXOffset(posx);
posy=0;
// AVOID OFFSCREEN
if(posx<65)
posx=65;
if(posx>350)
posx=350;

// translate to the correct position and draw
canvas.translate(posx, posy);
draw(canvas);
canvas.translate(-posx, -posy);
}

}

最佳答案

MPAndroidChart 2.2.x 中没有直接获取 x 值的方法通过将 xvalues 数组发送到 MarkerView 您可以轻松获取它。请参阅下面的 MarkerView 实现。

public class MymarkerView extends MarkerView
{
ArrayList<String> mXLabels;

public MymarkerView(Context context, int layoutResource, ArrayList<String> xLabels) {
super(context, layoutResource);
mXLabels = xLabels;
}

@Override
public void refreshContent(Entry e, Highlight highlight) {
//get x value
String xVal= mXLabels.get(e.getXIndex());
}

@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();
}
}

将“MarkerView”设置为图表

ArrayList<String> xVals = new ArrayList<>();
//add labels to xVals
....
....

LineData lineData = new LineData(xVals, dataSets);

lineChart.setMarkerView(new MymarkerView(context, R.layout.custom_marker_view_layout, xVals);
lineChart.setData(lineData);

或者,如果您使用MPAndroidChart 3.0.0,请像这样获取x值

@Override
public void refreshContent(Entry e, Highlight highlight) {
//get x value
float xVal = e.getX();
}

关于java - 如何在折线图中的标记 View 上显示 X 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38412851/

26 4 0