我正在制作折线图并且工作正常。 我正在使用这个库: 编译' com.jjoe64:graphview:4.0.1'
当我点击Toast中应用程序中的特定数据点时,我可以显示y轴值。我在x轴上使用日期,并且当用户点击任何数据点时也想显示日期。但是现在它显示任何双值,如(1.3455677)
如果你看到图像,Toast消息显示两个值x值,它应该是日期,因为我在x轴上使用日期但显示其他内容。
以下是我的代码
Date d1 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d2 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d3 = calendar.getTime();
calendar.add(Calendar.DATE, 1);
Date d4 = calendar.getTime();
GraphView graph = (GraphView) findViewById(R.id.graph);
GraphView graph1 = (GraphView) findViewById(R.id.graph1);
graph.setBackgroundColor(Color.argb(50, 50, 0, 200));
// set manual Y bounds
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(10);
graph.getViewport().setMaxY(50);
graph.getViewport().setScrollable(true);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{
new DataPoint(d1, 15),
new DataPoint(d2, 10),
new DataPoint(d3, 48),
new DataPoint(d4, 41)
});
graph.addSeries(series);
series.setTitle("Random Curve 1");
series.setColor(R.color.colorPrimary);
series.setDrawDataPoints(true);
series.setDataPointsRadius(15);
series.setOnDataPointTapListener(new OnDataPointTapListener() {
@Override
public void onTap(Series series, DataPointInterface dataPoint) {
//Toast.makeText(MainActivity.this, "Series1: On Data Point clicked: " + dataPoint, Toast.LENGTH_SHORT).show();
double pointY = dataPoint.getY();
double pointX = dataPoint.getX();
Toast.makeText(MainActivity.this, pointX+" "+pointY, Toast.LENGTH_SHORT).show();
}
});
series.setThickness(8);
答案 0 :(得分:3)
要在点击图表中的数据点时在吐司中显示x轴日期,您必须执行以下步骤。
您必须简单地将X轴数据转换为日期格式。
Date d = new java.sql.Date((long) dataPoint.getX());
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy hh:mm aa");
String formatted = format1.format(d.getTime());
Toast.makeText(MainActivity.this, formatted +" "+ dataPoint.getY(), Toast.LENGTH_SHORT).show();