import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.DataPointInterface;
import com.jjoe64.graphview.series.PointsGraphSeries;
public class MainActivity extends AppCompatActivity {
GraphView graphView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
graphView = findViewById(R.id.graphview);
// For creating Point Graph Series We use PointGraphSeries
PointsGraphSeries<DataPoint> series = new PointsGraphSeries<>(getDataPoint());
graphView.addSeries(series);
// we use this method to define the shape that
// will be used for data points
// series.setShape(PointsGraphSeries.Shape.TRIANGLE);
// we use this method to define the size of the shape
series.setSize(50);
// we use this method to set the color
series.setColor(Color.RED);
// we use this method to define the custom shape,
// we create our own shape
series.setCustomShape(new PointsGraphSeries.CustomShape() {
@Override
public void draw(Canvas canvas, Paint paint, float x, float y, DataPointInterface dataPoint) {
paint.setStrokeWidth(5);
// we are initialising the shape structure of data points
canvas.drawLine(x - 20, y, x, y - 20, paint);
canvas.drawLine(x, y - 20, x + 20, y, paint);
canvas.drawLine(x + 20, y, x, y + 20, paint);
canvas.drawLine(x - 20, y, x, y + 20, paint);
}
});
}
// initialising the data points
private DataPoint[] getDataPoint() {
DataPoint[] dp = new DataPoint[]{
new DataPoint(0, 1),
new DataPoint(2, 1),
new DataPoint(3, 5),
new DataPoint(6, 2),
new DataPoint(7, 8),
};
return dp;
}
}