First of all, please attach the address of helloCharts framework. I also learned the comprehension test by downloading that demo. Please kindly point out if there is anything I have taken into account. If you want to learn more about the framework, download the Demo and learn about it yourself.
Less nonsense, on the effect:
Step 1: Import the dependency packages
implementation 'com. Making. Lecho: hellocharts - android: v1.5.8'
Copy the code
Step 2: Configure the XML file
It’s a little bit easier here, nothing to say, right
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LineChartActivity" >
<lecho.lib.hellocharts.view.LineChartView
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></lecho.lib.hellocharts.view.LineChartView>
</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code
Step 3: Activity code (key)
First of all, attach all the code, the following parameters are explained in detail:
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.List;
import lecho.lib.hellocharts.animation.ChartAnimationListener;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.LineChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.Chart;
import lecho.lib.hellocharts.view.LineChartView;
public class LineChartActivity extends AppCompatActivity {
private LineChartView chart;
private final int maxNumberOfLines = 4;
private final int numberOfPoints = 6;
private final int number=60;
float[][] randomNumbersTab = new float[maxNumberOfLines][numberOfPoints];
private ValueShape shape = ValueShape.CIRCLE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
chart=findViewById(R.id.chart);
generateValues();
generateData();
resetViewport();
}
// Set the y axis from left to number
private void resetViewport(a) {
final Viewport v = new Viewport(chart.getMaximumViewport());
v.bottom = 0;
v.top = number;
v.left = 0;
v.right = numberOfPoints - 1;
chart.setMaximumViewport(v);
chart.setCurrentViewport(v);
}
private void generateValues(a) {
for (int i = 0; i < maxNumberOfLines; ++i) {
for (int j = 0; j < numberOfPoints; ++j) {
randomNumbersTab[i][j] = (float) Math.random() * number; }}}private void generateData(a) {
List<Line> lines = new ArrayList<Line>();
List<AxisValue> axisXValues = new ArrayList<AxisValue>();
for (int i = 0; i <= numberOfPoints; i++)
axisXValues.add(i, new AxisValue(i).setLabel(i + "Month"));
int numberOfLines = 1;
for (int i = 0; i < numberOfLines; ++i) {
List<PointValue> values = new ArrayList<PointValue>();
for (int j = 0; j < numberOfPoints; j++) {
values.add(new PointValue(j, randomNumbersTab[i][j]));
}
Line line = new Line(values);
line.setColor(ChartUtils.pickColor()); // Set the color to random
line.setShape(shape); // Set the shape
line.setCubic(true); // set line to curve and vice versa
line.setFilled(true); // The Settings are filled
line.setHasLabels(true); // Display the sticky note
line.setHasLabelsOnlyForSelected(true);
line.setHasLines(true);
line.setHasPoints(true);
lines.add(line);
}
LineChartData data = new LineChartData(lines);
data.setAxisXBottom(new Axis(axisXValues).setHasLines(true).setTextColor(Color.BLACK).setName("Date").setHasTiltedLabels(true).setMaxLabelChars(4));
data.setAxisYLeft(new Axis().setHasLines(true).setName("Income").setTextColor(Color.BLACK).setMaxLabelChars(2)); data.setBaseValue(Float.NEGATIVE_INFINITY); chart.setLineChartData(data); }}Copy the code
1. Get the picture
2. The detail
1. ResetViewport method
Set the Y-axis from 0 to number, which I'm going to set to 60.
2. GenerateValues method
Generate random numbers from 0 to 60, maxNumberOfLines set the number of lines I set to one, numberOfPoints set the numberOfPoints I set to 6
3. The generateData method (Focus on
) maximum content
Illustration: == MY consideration may not be very thoughtful, you can try more, maybe I can learn more functions. = =
If I have time, I’ll talk about statistics, pie charts, etc.