{"id":4123,"date":"2020-05-31T15:54:31","date_gmt":"2020-05-31T05:54:31","guid":{"rendered":"https:\/\/learntodroid.com\/?p=4123"},"modified":"2020-06-05T15:33:42","modified_gmt":"2020-06-05T05:33:42","slug":"how-to-display-a-line-chart-in-your-android-app","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-display-a-line-chart-in-your-android-app\/","title":{"rendered":"How to Display a Line Chart in Your Android App"},"content":{"rendered":"\n
\"\"<\/figure><\/div>\n\n\n\n

Adding line charts to your Android app offers a great way to present data that is changing over time to your users.<\/p>\n\n\n\n

MPAndroidChart<\/a> is the leading open source, free to use, Android native charting library that supports many different chart types including line charts, bar charts<\/a>, combined line and bar charts, pie charts, scatter plots, candlestick charts, bubble charts and others.<\/p>\n\n\n\n

In this blog post I will show you how to display a line chart in your Android app by following the steps below.<\/p>\n\n\n\n

  1. Add the dependencies for MPAndroidChart to your Android project<\/strong><\/li>
  2. Add the line chart to your activity or fragment layout resource<\/strong><\/li>
  3. Configure the appearance of the line chart<\/strong><\/li>
  4. Create the data set to show in the line chart<\/strong><\/li>
  5. Provide the data set to the line chart to display in the user interface<\/strong><\/li><\/ol>\n\n\n\n

    I have include code samples in this post and I have also created a public code repository on GitHub<\/a> with the all of the code shared in this post.<\/p>\n\n\n\n

    How to Create a Line Chart in Android<\/h2>\n\n\n\n

    In this tutorial showing how to create a line chart in Android using the MPAndroidChart library.<\/p>\n\n\n\n

    We will be creating an app that will retrieve historical prices of a stock using the Yahoo Finance API and display the results in a line chart.<\/p>\n\n\n\n

    The user will be able to enter a stock ticker, select a historical period of results and showing the high, low and close price of the stock over a particular time interval inside a line chart.<\/p>\n\n\n\n

    See a sample screenshot below of the app we will be creating.<\/p>\n\n\n\n

    \"\"<\/figure><\/div>\n\n\n\n

    Step 1: Retrieve the MPAndroidChart Dependencies Using Gradle<\/h3>\n\n\n\n

    The MPAndroidChart library can be retrieved into your Android project using Gradle by updating the app level build.gradle file and the project level build.gradle file.<\/p>\n\n\n\n

    Starting with the project level build.gradle file located in the root directory of your Android project, you will need to modify the allprojects section and add an entry for maven jitpack as per the code excerpt below.<\/p>\n\n\n