A Method of Drawing Multiple Curves by Android LineChart

Time:2019-10-5

In this paper, we share the specific code of Android LineChart drawing multiple curves for your reference. The details are as follows.

Target effect:

1. Create a new custom_marker_view.xml page as a pop-up page:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="40dp"
 android:background="@drawable/marker2" >
 
 <TextView
 android:id="@+id/tvContent"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="7dp"
 android:layout_marginLeft="5dp"
 android:layout_marginRight="5dp"
 android:text=""
 android:textSize="12dp"
 android:textColor="@android:color/white"
 android:ellipsize="end"
 android:singleLine="true"
 android:textAppearance="?android:attr/textAppearanceSmall" />
 
</RelativeLayout>

2. Activity_main.xml page:


<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"
 android:paddingBottom="10dp"
 android:paddingLeft="10dp"
 android:paddingRight="10dp"
 android:paddingTop="10dp"
 tools:context=".MainActivity" >
 
 <com.github.mikephil.charting.charts.LineChart
 android:id="@+id/chartTall"
 android:layout_width="match_parent"
 android:layout_height="400dp"
 android:layout_marginTop="20dp" />
 
 
</RelativeLayout>

3. New MyMarkerView. Java rewrite MarkView control:


package com.example.weixu.drawline;
 
import android.content.Context;
import android.widget.TextView;
 
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.utils.MarkerView;
import com.github.mikephil.charting.utils.Utils;
 
public class MyMarkerView extends MarkerView {
 
 private TextView tvContent;
 
 public MyMarkerView(Context context, int layoutResource) {
 super(context, layoutResource);
 
 tvContent = (TextView) findViewById(R.id.tvContent);
 }
 
 @Override
 public void refreshContent(Entry e, int dataSetIndex) {
 
 if (e instanceof CandleEntry) {
 CandleEntry ce = (CandleEntry) e;
 tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
 } else {
 tvContent.setText("" +e.getVal());
 }
 }
}

4. MainActivity. Java page:

package com.example.weixu.drawline;
 
import java.util.ArrayList;
 
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.WindowManager;
 
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendForm;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.github.mikephil.charting.utils.YLabels;
 
public class MainActivity extends Activity {
 
 private LineChart chartTall;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 WindowManager.LayoutParams.FLAG_FULLSCREEN);
 setContentView(R.layout.activity_main);
 
 chartTall = (LineChart) findViewById(R.id.chartTall);
 
 setType();
 
 // Refresh Charts
 chartTall.invalidate();
 }
 
 private void setType() {
 // Is it displayed from 0 on the Y axis?
 chartTall.setStartAtZero(true);
 // Whether the data is displayed on the Y axis is the data on the curve.
 chartTall.setDrawYValues(true);
 // Setting up grid
 chartTall.setDrawBorder(true);
 chartTall.setBorderPositions(new BarLineChartBase.BorderPosition[] {
 BorderPosition.BOTTOM});
 // Description in the lower right corner of the chart
 ChartTall. setDescription ("height curve");
 // Setting up units on the Y-axis
 chartTall.setUnit("cm");
 // Setting Transparency
 chartTall.setAlpha(0.8f);
 // Setting up grid底下的那条线的颜色
 chartTall.setBorderColor(Color.rgb(213, 216, 214));
 // Setting Y-axis forward and backward inversion
 chartTall.setInvertYAxisEnabled(false);
 // Set Highlight Display
 chartTall.setHighlightEnabled(true);
 // settings can be touched, for example, false can not be dragged, zoomed, etc.
 chartTall.setTouchEnabled(true);
 // settings can be dragged, zoomed.
 chartTall.setDragEnabled(true);
 chartTall.setScaleEnabled(true);
 // Can the settings be enlarged and enlarged?
 chartTall.setPinchZoom(true);
 // Set up pop-up annotation for data corresponding to click chart graph
 MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
 mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight());
 chartTall.setMarkerView(mv);
 chartTall.setHighlightIndicatorEnabled(false);
 // Set font formats, such as block letters
 Typeface tf = Typeface.createFromAsset(getAssets(),
 "OpenSans-Regular.ttf");
 chartTall.setValueTypeface(tf);
 
 XLabels xl = chartTall.getXLabels();
 Xl. setPosition (XLabelPosition. BOTTOM); //Set X-axis data to display at the bottom
 Xl. setTypeface (tf); // Set fonts
 Xl. setTextSize (10f); // Set font size
 Xl. setSpace Between Labels (3); // Set the spacing between data
 
 YLabels yl = chartTall.getYLabels();
 Yl. setTypeface (tf); // Set fonts
 Yl. setTextSize (10f); // s Sets font size
 Yl. setLabelCount (5); //Set the maximum number of data displayed on the Y-axis
 // Loading data
 setData();
 // Animation from the X-axis
 chartTall.animateX(4000);
 ChartTall. animateY (3000); // Animation from the Y axis
 ChartTall. animateXY (3000, 3000); // Animation entering together from the XY axis
 
 // set minimum scale.
 chartTall.setScaleMinima(0.5f, 1f);
 
 }
 
 private void setData() {
 String [] babAge = X-axis data for {"0", "1", "2", "3", "4", "5", "6";//
 String[] babyTall = {"50","60","90","110","130","135","140"};
 String [] usuaTall = Y-axis data for {"55", "65", "95", "115", "125", "135", "145";//
 
 LineData data = new LineData (babAge, setLine (babAge, babyTall, 1,'Baby Height'); // Create LineData entity class and add the first curve
 Data. addDataSet (setLine (babAge, usuaTall, 2,'Normal Height'); // Add a second curve
 chartTall.setData(data);
 }
 
 // draw lines
 private LineDataSet setLine(String[] babAge, String[] Tall,int flag,String name) {
 ArrayList<String> xValsAge = new ArrayList<String>();
 for (int i = 0; i < babAge.length; i++) {
 xValsAge.add(babAge[i]);
 }
 ArrayList<Entry> yValsBabyTall = new ArrayList<Entry>();
 for (int i = 0; i < Tall.length; i++) {
 yValsBabyTall.add(new Entry(Float.parseFloat(Tall[i]), i));
 }
 // Setting the growth curve of baby
 LineDataSet setData = new LineDataSet(yValsBabyTall,name);
 SetData. setDrawCubic (true); // Set the curve to be a smooth line
 setData.setCubicIntensity(0.2f);
 SetData. setDrawFilled (false); // Set the range area to be covered to fill the color
 SetData. setDrawCircles (true); // Set a dot
 SetData. setLineWidth (2f); // Set the width of the line
 SetData. setCircleSize (5f); // Set the size of the circle
 setData.setHighLightColor(Color.rgb(244, 117, 117));
 // Setting Curve Colors
 if(flag==1)
 SetData. setColor (Color. RGB (104, 241, 175); // Baby Height Curve Color
 else if(flag==2)
 SetData. setColor (Color. RGB (255, 0, 0)); // Ordinary Height Curve Color
 Return setData; // Return Curve
 }
}

Source: Click to open the link

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.