问题描述
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<!--line graph view where we will
be displaying our data-->
<com.jjoe64.graphview.GraphView
android:id="@+id/idGraphView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</RelativeLayout>
main_activity.java
package com.example.quicklinegraph;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.CoreComponentFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
//1. Import Libraries For The Code
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GridLabelRenderer;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.helper.StaticLabelsFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.DataPointInterface;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.OnDataPointTapListener;
import com.jjoe64.graphview.series.Series;
public class MainActivity extends AppCompatActivity {
//2. Make a global Variable.
GraphView graphView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
graphView = findViewById(R.id.idGraphView);
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graphView);
staticLabelsFormatter.setHorizontalLabels(new String[] {"Mon","Tues","Wed","Thurs","Fri","Sat","Sun"});
//staticLabelsFormatter.setVerticalLabels(new String[] {"5","10","15","20","25"});
graphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
graphviewSettings(graphView);
final LineGraphSeries<DataPoint> series1 = Series1();
final LineGraphSeries<DataPoint> series2 = Series2();
series1.setTitle("Max Temp");
series2.setTitle("Min Temp");
graphView.addSeries(series1);
graphView.addSeries(series2);
series1.setonDataPointTapListener(new OnDataPointTapListener() {
@Override
public void onTap(Series series,DataPointInterface dataPoint) {
series1.setColor(Color.BLACK);
series2.setColor(Color.GRAY);
//c
}
});
series2.setonDataPointTapListener(new OnDataPointTapListener() {
@Override
public void onTap(Series series,DataPointInterface dataPoint) {
series2.setColor(Color.BLACK);
series1.setColor(Color.GRAY);
//Toast.makeText(MainActivity.this,"Series2: On Data Point clicked: "+dataPoint,Toast.LENGTH_SHORT).show();
}
});
graphView.setonClickListener(new DoubleClickListener() {
@Override
public void onSingleClick(View v) {
}
@Override
public void ondoubleclick(View v) {
Toast.makeText(MainActivity.this,"Double Tap",Toast.LENGTH_SHORT).show();
}
});
}
public LineGraphSeries<DataPoint> Series1()
{
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{
// on below line we are adding
// each point on our x and y axis.
new DataPoint(1,28),new DataPoint(2,26),new DataPoint(3,23),new DataPoint(4,24),new DataPoint(5,new DataPoint(6,27),new DataPoint(7,28)
});
series.setDrawDataPoints(true);
series.setDataPointsRadius(10);
series.setThickness(8);
series.setColor(Color.RED);
return series;
}
public LineGraphSeries<DataPoint> Series2()
{
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{
// on below line we are adding
// each point on our x and y axis.
new DataPoint(1,15),13),10),8),11),});
series.setDrawDataPoints(true);
series.setDataPointsRadius(10);
series.setThickness(8);
series.setColor(Color.GREEN);
return series;
}
public void graphviewSettings(GraphView graphView)
{
//graphView.setTitle("My Graph View");
graphView.getViewport().setscalable(true);
GridLabelRenderer gridLabel = graphView.getGridLabelRenderer();
gridLabel.setHorizontalAxisTitle("Week Days");
gridLabel.setVerticalAxisTitle("Temperature");
graphView.setTitleColor(R.color.colorPrimaryDark);
graphView.setTitleTextSize(18);
graphView.getLegendRenderer().setVisible(true);
graphView.getLegendRenderer().setAlign(LegendRenderer.Legendalign.TOP);
}
public abstract class DoubleClickListener implements View.OnClickListener {
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
ondoubleclick(v);
lastClickTime = 0;
} else {
onSingleClick(v);
}
lastClickTime = clickTime;
}
public abstract void onSingleClick(View v);
public abstract void ondoubleclick(View v);
}
}
输出
问题
但是当我缩放和滚动时,这些静态标签显示两次,是否有任何解决方法或代码调整?我会很感激的。我想在适当的地方显示一次标签。因此,缩放和滚动不会影响标签。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)