微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Graphview 中滚动和缩放时禁用 X 轴静态标签的重复

如何解决在 Graphview 中滚动和缩放时禁用 X 轴静态标签的重复

我想在图表上针对温度和工作日显示两条线。这是我的代码

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

输出

Actual Line Chart

问题

但是当我缩放和滚动时,这些静态标签显示两次,是否有任何解决方法代码调整?我会很感激的。我想在适当的地方显示一次标签。因此,缩放和滚动不会影响标签

Scrolling and Scaling Problem

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?