Android studio Graphview live plot from FIrebase

330 views Asked by At

I am trying to make an app that retrieves real-time data from Firebase and makes with it a line chart. The code below works partially as the generated plot contains only the point but no line is drawn between them, as shown. line chart appears as a point chart.

Any ideas? Thank You!

protected void onCreate(Bundle savedInstanceState) {

.........

    myRef_1.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            String value = snapshot.getValue(String.class);
            textView.setText(value);
            ArrayList<String> value_string = new ArrayList<String>();
            value_string.add(value);

            GraphView graph = findViewById(R.id.graph);

            LineGraphSeries<DataPoint> series = new LineGraphSeries<>();
            DataPoint point = new DataPoint(2*Double.parseDouble(value_string.get(0)), Double.parseDouble(value_string.get(0)));
            series.appendData(point, true, 10);

            graph.addSeries(series);
            graph.getViewport().setScalable(true); // enables horizontal zooming and scrolling
            graph.getViewport().setScalableY(true); //
            graph.getViewport().setYAxisBoundsManual(true); // Prevents auto-rescaling the Y-axis
            graph.getViewport().setXAxisBoundsManual(true);
            series.setThickness(8);
            Viewport viewport = graph.getViewport();
            viewport.setYAxisBoundsManual(true);
            viewport.setMinY(0);
            viewport.setMinX(0);
            viewport.setScrollable(true);
            viewport.setScalable(true);
            series.setDrawBackground(true);
            series.setColor(Color.rgb(117,53,173));
            series.setDataPointsRadius(5);
            series.setAnimated(false);
            series.setDrawDataPoints(true);
            series.setDrawAsPath(true);
            GridLabelRenderer gridLabel = graph.getGridLabelRenderer();
          
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(MainActivity.this, "Nu s-a reusit extragerea datelor din baza de date", Toast.LENGTH_SHORT).show();
        }
    });

.......... }

1

There are 1 answers

0
Asen Bonev On

I suggest you to add a custom line path like this one:

import java.awt.Color;
import java.awt.imagr.ColorFilter;
import java.awt.Stroke;
import java.awt.image.ColorFilter;
import java.awt.Composite;
import java.awt.AlphaComposite;

public class LineStyle {
    private LineType lineType;
    private Color color;
    private float width;
    private float alpha;
    private Stroke style;
    private ColorFilter colorFilter;
    private Composite blendMode;

public LineStyle() {
    this.lineType = new LineType.SmoothCurve(false);
    this.color = Color.BLACK;
    this.width = 8f;
    this.alpha = 1.0f;
    this.style = new BasicStroke(width);
    this.colorFilter = null;
    this.blendMode = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
}

public LineStyle(LineType lineType, Color color, float width, float alpha, Stroke style, ColorFilter colorFilter, Composite blendMode) {
    this.lineType = lineType;
    this.color = color;
    this.width = width;
    this.alpha = alpha;
    this.style = style;
    this.colorFilter = colorFilter;
    this.blendMode = blendMode;
}

public LineType getLineType() {
    return lineType;
}

public void setLineType(LineType lineType) {
    this.lineType = lineType;
}

public Color getColor() {
    return color;
}

public void setColor(Color color) {
    this.color = color;
}

public float getWidth() {
    return width;
}

public void setWidth(float width) {
    this.width = width;
}

public float getAlpha() {
    return alpha;
}

public void setAlpha(float alpha) {
    this.alpha = alpha;
}

public Stroke getStyle() {
    return style;
}

public void setStyle(Stroke style) {
    this.style = style;
}

public ColorFilter getColorFilter() {
    return colorFilter;
}

public void setColorFilter(ColorFilter colorFilter) {
    this.colorFilter = colorFilter;
}

public Composite getBlendMode() {
    return blendMode;
}

public void setBlendMode(Composite blendMode) {
    this.blendMode = blendMode;
}}

You can also define a different type of the line itself:

import java.util.Arrays;

public abstract class LineType {
    public abstract boolean  isDotted();
    public abstract float[] getIntervals();

    public static class Straight extends LineType {
    private boolean isDotted;
    private float[] intervals;

    public Straight(boolean isDotted, float[] intervals) {
        this.isDotted = isDotted;
        this.intervals = intervals;
    }

    public boolean isDotted() {
        return isDotted;
    }

    public float[] getIntervals() {
        return intervals;
    }
}

public static class SmoothCurve extends LineType {
    private boolean isDotted;
    private float[] intervals;

    public SmoothCurve(boolean isDotted, float[] intervals) {
        this.isDotted = isDotted;
        this.intervals = intervals;
    }

    public boolean isDotted() {
        return isDotted;
    }

    public float[] getIntervals() {
        return intervals;
    }
}}