我正在尝试制作一个从 Firebase 检索实时数据并用其制作折线图的应用程序。下面的代码部分起作用,因为生成的绘图仅包含点,但它们之间没有绘制线,如图所示。 。
有什么想法吗? 谢谢!
protected void onCreate(Bundle savingInstanceState) {
.........
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();
}
});
...... }
我建议您添加一个自定义线路路径,如下所示:
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;
}}
您还可以定义线条本身的不同类型:
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;
}
}}