{"id":844,"date":"2019-12-23T22:45:22","date_gmt":"2019-12-23T12:45:22","guid":{"rendered":"https:\/\/learntodroid.com\/?p=844"},"modified":"2021-01-13T09:45:37","modified_gmt":"2021-01-12T23:45:37","slug":"how-to-display-a-bar-chart-in-your-android-app","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-display-a-bar-chart-in-your-android-app\/","title":{"rendered":"How to Display a Bar Chart in Your Android App"},"content":{"rendered":"\n

Bar charts are most useful for showing a comparison between different groups of numerical data or to track changes to numerical data over time.<\/p>\n\n\n\n

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

To display a bar chart in your Android app you will need to follow these steps.<\/p>\n\n\n\n

  1. Select a charting library for Android, in this tutorial we will use MPAndroidChart<\/strong><\/li>
  2. Add the dependencies for MPAndroidChart to your project<\/strong><\/li>
  3. Add the bar chart to your activity or fragment layout<\/strong><\/li>
  4. Configure the appearance of the bar chart<\/strong><\/li>
  5. Create the data set to show in the bar chart<\/strong><\/li>
  6. Provide the data set to the bar chart to display in the user interface<\/strong><\/li><\/ol>\n\n\n\n

    Later in this article I will take you through a detailed step by step guide to create different types of bar charts in your Android app using the MPAndroidChart.<\/p>\n\n\n\n

    We will cover using MPAndroidChart to create a simple vertical bar chart, a horizontal bar chart, a group bar chart and a stacked bar chart.<\/p>\n\n\n\n

    All the code samples shared in this post are available in the BarChartExamples GitHub repository<\/a> I have created.<\/p>\n\n\n

    <\/p>\n

    <\/div>\n

    <\/p>\n\n\n

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

    In this section, I will provide a detailed step by step guide on how to use the MPAndroidChart charting library to create a vertical bar chart in your Android app that looks like the screenshot taken from the Android emulator below.<\/p>\n\n\n\n

    \"\"<\/a><\/figure>\n\n\n\n

    Step 1: Select a Charting Library<\/h3>\n\n\n\n

    For the purposes of this tutorial we will be using the MPAndroidChart library which is a feature rich, open source, free to use charting library for Android.<\/p>\n\n\n\n

    Quoting directly from the MPAndroidChart GitHub page, the MPAndroidChart library is:<\/p>\n\n\n\n

    A powerful \ud83d\ude80 Android chart view \/ graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, dragging and animations.<\/p> https:\/\/github.com\/PhilJay\/MPAndroidChart<\/a> <\/cite><\/blockquote>\n\n\n\n

    As at the time of writing the official repository for MPAndroidChart on GitHub has 29.5k Stars, 7.6k Forks and 1.1k Watchers.<\/p>\n\n\n\n

    \"\"<\/a><\/figure>\n\n\n\n

    MPAndroidChart is also popular with the StackOverflow community with a total of 1,645 questions asked at the time of writing.<\/p>\n\n\n\n

    \"\"<\/a><\/figure>\n\n\n\n

    Step 2: Add Dependencies for MPAndroidChart<\/h3>\n\n\n\n

    To retrieve the MPAndroidChart dependencies using Gradle you will need to modify the project level build.gradle file and the app level build.gradle file.<\/p>\n\n\n\n

    Inside the project level build.gradle file you will need to include a new repository in the allprojects section. See the excerpt from the project level build.gradle file below.<\/p>\n\n\n