最初,该图从-10变为10,但是我希望当用户向右滚动时,值会被评估,并且图“增长”。有听众吗?
我的意思是,当用户向右滚动时,我会为功能添加更多点,x始终为y值。当用户向右或向左滚动时,图形将继续构造自身。
最佳答案
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linechart = (LineChart) findViewById(R.id.chart);
new Random(0).nextInt();
linechart.setHighlightPerDragEnabled(true);
linechart.setScaleEnabled(true);
linechart.setPinchZoom(true);
linechart.setDrawGridBackground(false);
linechart.getDescription().setEnabled(false);
linechart.setOnChartGestureListener(new GestureListener());
LineData lineData = new LineData();
lineData.setValueTextColor(Color.WHITE);
linechart.setData(lineData);
Legend legend = linechart.getLegend();
legend.setForm(Legend.LegendForm.LINE);
legend.setTextColor(Color.WHITE);
XAxis xaxis = linechart.getXAxis();
xaxis.setDrawGridLines(true);
xaxis.setTextColor(Color.WHITE);
xaxis.setTextSize(15f);
xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
YAxis yaxis = linechart.getAxisLeft();
yaxis.setTextColor(Color.WHITE);
yaxis.setTextSize(15f);
yaxis = linechart.getAxisRight();
yaxis.setTextColor(Color.WHITE);
yaxis.setTextSize(15f);
linechart.invalidate();
}
private void addEntry(int number) {
data = linechart.getData();
if (data != null)
{
ILineDataSet set = data.getDataSetByIndex(0);
if (set == null) {
set = createSet();
data.addDataSet(set);
}
data.addEntry(new Entry(data.getDataSetByIndex(0).getEntryCount(), number), 0);
data.notifyDataChanged();
linechart.notifyDataSetChanged();
}
}
private LineDataSet createSet() {
LineDataSet set = new LineDataSet(null, "");
set.setCubicIntensity(0.0f);
set.setAxisDependency(YAxis.AxisDependency.RIGHT);
set.setColor(ColorTemplate.getHoloBlue());
set.setCircleColor(ColorTemplate.getHoloBlue());
set.setDrawValues(false);
set.setLineWidth(2f);
set.setCircleSize(4f);
set.setFillAlpha(65);
set.disableDashedLine();
return set;
}
class GestureListener implements OnChartGestureListener {
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
addEntry(//add your entries here);
}
@Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
linechart.moveViewToX(data.getEntryCount());
}
@Override
public void onChartLongPressed(MotionEvent me) {
}
@Override
public void onChartDoubleTapped(MotionEvent me) {
}
@Override
public void onChartSingleTapped(MotionEvent me) {
}
@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
}
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
}
@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
}
}
试试看
关于android - MPandroidchart-当用户向上或向下或向右或向左滚动时,我想添加更多点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42503376/