david ortiz steroids oral steroids what do steroids do to your body moon face steroids liver steroids steroids for bronchitis types of steroids

Android Line Chart – How to Draw Line Chart in Android

Last Updated on July 16, 2018

Hi and welcome to another tutorial from Codingdemos, in this tutorial you will learn how to draw line chart in android. You will be building an app that will will show a monthly sales of a company for the year inside a line chart.

Android Line Chart

By the end of this tutorial, you will have an app that looks like this. (Large preview)

In this tutorial you will be using a 3rd party library called Hellocharts-Android, this library will help you build Android line chart.

In this tutorial we will be using the following:

    – Android studio version 3.0.1
    – Android emulator Nexus 5X with API 26
    – Minimum SDK API 16

1- Open up Android Studio and open any project that you have in your computer.

android studio welcome screen

Create new Android Studio project or open existing project. (Large preview)

2- Open up build.gradle (module:app) and add the library in the dependencies.


implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'

3- Next you need to open up build.gradle (Project) and add Jcenter because this library is available through it.


allprojects {
    repositories {
        google()
        jcenter()
    }
}

4- Now sync your project by clicking on Sync Now.

Android studio sync project

Android studio sync project. (Large preview)

5- Open up colors.xml file to change the colors of the main app.


< color name="colorPrimary">#9C27B0< /color>
< color name="colorPrimaryDark">#7B1FA2< /color>
< color name="colorAccent">#03A9F4< /color>

6- Build and run the app to see the new colors.

Android line chart

New colors for the app. (Large preview)

7- Open up activity_main.xml and add the following code.


< lecho.lib.hellocharts.view.LineChartView
    android:id="@+id/chart"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

This is the code which is use to draw line chart.

8- Open up MainActivity.java to reference LineChartView.


LineChartView lineChartView = findViewById(R.id.chart);

9- Next you need to initialize the actual data which will appear later inside Android line chart. First declare and initialize the data for Axis.


String[] axisData = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept",
            "Oct", "Nov", "Dec"};

10- Now you need to declare and initialize the data for Y-Axis.


int[] yAxisData = {50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18};

11- Next declare 2 types of List like the following.


List yAxisValues = new ArrayList();
List axisValues = new ArrayList();

These lists will be used to hold the data for Axis and Y-Axis.

12- Declare and initialize the line which appears inside graph chart, this line will hold the values of Y-Axis.


Line line = new Line(yAxisValues);

13- Next you need to add Axis and Y-Axis data inside yAxisValues and axisValues lists.


        for(int i = 0; i < axisData.length; i++){
            axisValues.add(i, new AxisValue(i).setLabel(axisData[i]));
        }

        for (int i = 0; i < yAxisData.length; i++){
            yAxisValues.add(new PointValue(i, yAxisData[i]));
        }

14- Declare a list of a type Line.


List lines = new ArrayList();
lines.add(line);

This list will hold the line of the graph chart.

15- Now you can add the graph line to the overall data chart.


LineChartData data = new LineChartData();
data.setLines(lines);

16- Now you need to add the following code to be able to see the Android line chart.


lineChartView.setLineChartData(data);

17- Build and run the app to see the output.

draw line chart in android

Line chart in android. (Large preview)

18- Now that you managed to draw the line chart, the only thing missing is the Axis and Y-Axis values.

19- Let's first work on getting the Axis values to show up in the line chart graph.


Axis axis = new Axis();
axis.setValues(axisValues);
data.setAxisXBottom(axis);

Here you've declared Axis model, then you've initialize it with axisValues data and finally you set axis data to be positioned at the bottom.

20- Now build and run the app to see the result.

android line chart graph

Android line chart graph with Axis data. (Large preview)

21- Don't worry about the text size as we will fix that later. Now let's work on Y-Axis values.


Axis yAxis = new Axis();
data.setAxisYLeft(yAxis);

22- Build and run the app to see the output.

android line char graph

Android line chart graph with Y-Axis data. (Large preview)

How to change the color of Android line chart

23- Let's change the color of line chart to something nice πŸ™‚ Modify Line declaration code that you did in step 12 to the following.


Line line = new Line(yAxisValues).setColor(Color.parseColor("#9C27B0"));

24- Build and run the app to see the changes.

android line chart color

New color for line chart. (Large preview)

How to increase the text size of Axis data

25- Let's increase the text size of the Axis data to be able to see it clearly. You can do that by adding the following code.


axis.setTextSize(16);

26- Build and run the app to see the output.

line chart in android

Increased text size of Axis line chart. (Large preview)

How to change the text color of Axis data

27- You can change the text color to any color that you like that matches your app just by adding the following code.


axis.setTextColor(Color.parseColor("#03A9F4"));

28- Build and run the app to see how it looks like.

android line chart text color

Changed text color of Axis line chart. (Large preview)

29- Change the text size and color for Y-Axis data by adding the following code.


yAxis.setTextColor(Color.parseColor("#03A9F4"));
yAxis.setTextSize(16);

30- Now build and run the app to see the result.

android line chart text color

Changed text size & color of Y-Axis line chart. (Large preview)

How to add a label next to Y-Axis data

31- You can add a label that describe the type of data this line chart have by adding the following code.


yAxis.setName("Sales in millions");

32- Build and run the app to see the output.

line chart in android

Added label to Y-Axis data in line chart. (Large preview)

33- If you had noticed that Y-Axis data inside the line chart doesn't show it's full data based on what you initialized in the beginning. You can fix it by adding the following code.


Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
viewport.top =110;
lineChartView.setMaximumViewport(viewport);
lineChartView.setCurrentViewport(viewport);

What you did here is you explicitly specified a value that is higher than the Y-Axis actual data which is in this example is (90).

34- Build and run the app to see the changes.

draw line chart in android

Y-Axis data in line chart is fully shown. (Large preview)

35- Here is the full code for activity_main.xml file.


< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout 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"
  android:orientation="vertical"
  tools:context="com.codingdemos.codingdemos.MainActivity">

  < lecho.lib.hellocharts.view.LineChartView
    android:id="@+id/chart"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

< /LinearLayout>

36- Here is the full code for MainActivity.java file.


package com.codingdemos.codingdemos;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

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.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class MainActivity extends AppCompatActivity {

    LineChartView lineChartView;
    String[] axisData = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept",
            "Oct", "Nov", "Dec"};
    int[] yAxisData = {50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lineChartView = findViewById(R.id.chart);

        List yAxisValues = new ArrayList();
        List axisValues = new ArrayList();


        Line line = new Line(yAxisValues).setColor(Color.parseColor("#9C27B0"));

        for (int i = 0; i < axisData.length; i++) {
            axisValues.add(i, new AxisValue(i).setLabel(axisData[i]));
        }

        for (int i = 0; i < yAxisData.length; i++) {
            yAxisValues.add(new PointValue(i, yAxisData[i]));
        }

        List lines = new ArrayList();
        lines.add(line);

        LineChartData data = new LineChartData();
        data.setLines(lines);

        Axis axis = new Axis();
        axis.setValues(axisValues);
        axis.setTextSize(16);
        axis.setTextColor(Color.parseColor("#03A9F4"));
        data.setAxisXBottom(axis);

        Axis yAxis = new Axis();
        yAxis.setName("Sales in millions");
        yAxis.setTextColor(Color.parseColor("#03A9F4"));
        yAxis.setTextSize(16);
        data.setAxisYLeft(yAxis);

        lineChartView.setLineChartData(data);
        Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
        viewport.top = 110;
        lineChartView.setMaximumViewport(viewport);
        lineChartView.setCurrentViewport(viewport);
    }

}

37- I hope you find this tutorial helpful and if you have any question please post them in the comment below.

24 Comments

  1. Thanks man, it’s well explained. Just a question, is there a way to show the data by pressing the dots or at least placing some label near them? Thanks

  2. Hi
    Very new to this

    When I place the object in the activity_main.xml I get a “Attribute is missing the Android namespace prefix” error, not sure what this is caused by. Hope this can be fixed as the rest of the tutorial looks great.

    Thanks
    Kind Regards

  3. When we change linecolor of x axis ,it is not changed according our requirment ,and when we change color of text of x axis it change the text color ,but x axis line color also contain the same color as text color

    I want to give both different color to x axis text and x axis line?

  4. Hi, I have to refresh the chart in the fragment , not main, but I can’t use
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Can you help me please?

  5. Hi I tried to do the debug and the app crash in

    lineChartView.setLineChartData(data);

    What can I do?
    (Sorry for my horrible english but I’m Italian)

  6. how to add different color to line or how to change the color of the line depending on the values

  7. hi , your tutorial is nice, but how to clear the graph , i used following idea but it did not worked for me

    linecharview.getLinechatdata().finish();

  8. How to show 2 line graphs on same view? I want to show 2 products sales in a month, so how can add second product values ?

  9. Hi, how can I use the information saved in a class?

    public class OperacoesAtributos implements Serializable {

    private Long id;
    private String data;
    private String moeda;
    private String valor;
    private String resultado;
    private String numeroDaOperacao;

    private String dataTemp;
    private String moedaTemp;
    private String valorTemp;
    private String resultadoTemp;
    private String numeroDaOperacaoTemp;

    public String getDataTemp() {
    return dataTemp;
    }

    public void setDataTemp(String dataTemp) {
    this.dataTemp = dataTemp;
    }

    public String getMoedaTemp() {
    return moedaTemp;
    }

    public void setMoedaTemp(String moedaTemp) {
    this.moedaTemp = moedaTemp;
    }

    public String getValorTemp() {
    return valorTemp;
    }

    public void setValorTemp(String valorTemp) {
    this.valorTemp = valorTemp;
    }

    public String getResultadoTemp() {
    return resultadoTemp;
    }

    public void setResultadoTemp(String resultadoTemp) {
    this.resultadoTemp = resultadoTemp;
    }

    public String getNumeroDaOperacaoTemp() {
    return numeroDaOperacaoTemp;
    }

    public void setNumeroDaOperacaoTemp(String numeroDaOperacaoTemp) {
    this.numeroDaOperacaoTemp = numeroDaOperacaoTemp;
    }

    public Long getId() {
    return id;
    }

    public void setId(Long id) {
    this.id = id;
    }

    public String getData() {
    return data;
    }

    public void setData(String data) {
    this.data = data;
    }

    public String getMoeda() {
    return moeda;
    }

    public void setMoeda(String moeda) {
    this.moeda = moeda;
    }

    public String getValor() {
    return valor;
    }

    public void setValor(String valor) {
    this.valor = valor;
    }

    public String getResultado() {
    return resultado;
    }

    public void setResultado(String resultado) {
    this.resultado = resultado;
    }

    public String getNumeroDaOperacao() {
    return numeroDaOperacao;
    }

    public void setNumeroDaOperacao(String numeroDaOperacao) {
    this.numeroDaOperacao = numeroDaOperacao;
    }
    }

    I would like to use “moeda” and “valor” instead of using the entire months and bar values ​​in the Y direction

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>