[JAVA] Create a simple bar chart with MPAndroidChart

wrap up

If you want to make a simple graph with MPAndroidChart, On the contrary, it was difficult to make a simple graph because of the rich functions. It became ver3.0.2 and it was a little different from the previous version (x-axis label display etc ...)

What I wanted to do

I wanted to make a simple bar graph like this. Screenshot (2017%2F09%2F08 午前10-18-41).png

Especially difficult to understand

・ Display label on x-axis ・ Set any color for each graph

manner

public class MainActivity extends AppCompatActivity {

    protected BarChart chart;

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

        chart = (BarChart) findViewById(R.id.chart1);

        //Display data acquisition
        BarData data = new BarData(getBarData());
        chart.setData(data);

        //Y axis(left)
        YAxis left = chart.getAxisLeft();
        left.setAxisMinimum(0);
        left.setAxisMaximum(100);
        left.setLabelCount(5);
        left.setDrawTopYLabelEntry(true);
        //In integer display
        left.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return "" + (int)value;
            }
        });

        //Y axis(right)
        YAxis right = chart.getAxisRight();
        right.setDrawLabels(false);
        right.setDrawGridLines(false);
        right.setDrawZeroLine(true);
        right.setDrawTopYLabelEntry(true);

        //X axis
        XAxis xAxis = chart.getXAxis();
        //List of Labels to display on the X-axis(the first""Is the position of the origin)
        final String[] labels = {"","National language", "Math", "English"};
                xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
        XAxis bottomAxis = chart.getXAxis();
        bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        bottomAxis.setDrawLabels(true);
        bottomAxis.setDrawGridLines(false);
        bottomAxis.setDrawAxisLine(true);

        //Display on the graph
        chart.setDrawValueAboveBar(true);
        chart.getDescription().setEnabled(false);
        chart.setClickable(false);

        //Usage Guide
        chart.getLegend().setEnabled(false);

        chart.setScaleEnabled(false);
        //animation
        chart.animateY(1200, Easing.EasingOption.Linear);
    }

    //Get bar chart data
    private List<IBarDataSet> getBarData(){
                //Data to be displayed
        ArrayList<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(1, 60));
        entries.add(new BarEntry(2, 80));
        entries.add(new BarEntry(3, 70));
        List<IBarDataSet> bars = new ArrayList<>();
        BarDataSet dataSet = new BarDataSet(entries, "bar");

        //Display as an integer
        dataSet.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                return "" + (int) value;
            }
        });
        //Don't highlight
        dataSet.setHighlightEnabled(false);

        //Set the color of the bar
        dataSet.setColors(new int[]{R.color.material_blue, R.color.material_green, R.color.material_yellow}, this);
        bars.add(dataSet);

        return bars;
    }

}

To display a label on the x-axis It feels like converting the numerical value displayed on the x-axis of the data into a label. Arbitrary color setting for each graph can be executed by setting the color list in the data list. In addition, by default, you can zoom in and out, tap to highlight, etc. I had to do quite a lot to adjust it because there were a lot of scales.

reference Labels on the x-axis (https://github.com/PhilJay/MPAndroidChart/issues/2044) Color for each graph (https://stackoverflow.com/questions/38872181/mpandroidchart-bar-chart-how-to-change-color-of-each-label)

Recommended Posts

Create a simple bar chart with MPAndroidChart
Create a simple web application with Dropwizard
[Rails withdrawal] Create a simple withdrawal function with rails
Create a simple search app with Spring Boot
Create a simple bulletin board with Java + MySQL
Create a playground with Xcode 12
Create a simple demo site with Spring Security with Spring Boot 2.1
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Create a Vue3 environment with Docker!
Draw a pie chart with Chart.js
Create exceptions with a fluid interface
Create a Maven project with a command
Create a simple web server with the Java standard library com.sun.net.httpserver
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ④ ~ Customize error messages ~
Create a jar file with the command
[Rails6] Create a new app with Rails [Beginner]
Create a MySQL environment with Docker from 0-> 1
Create a temporary class with new Object () {}
[docker] [nginx] Make a simple ALB with nginx
[Rails] Let's create a super simple Rails API
[Rails 5] Create a new app with Rails [Beginner]
Create a prefectural select bar using active_hash
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ② ~ Screen and function creation ~
Draw a bar graph and a line graph at the same time with MPAndroidChart
[Ruby on Rails] Create a pie chart for each column with Chartkick
Create a simple gateway server by setting masquerade with firewall-cmd of CentOS8
[Memo] Create a CentOS 8 environment easily with Docker
Create a CSR with extended information in Java
[Rails] rails new to create a database with PostgreSQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
Create a simple batch processing framework in Eclipse.
[Java] Create a collection with only one element
Create a team chat with Rails Action Cable
Create a SandBox account with fastlane spaces ip
Create a multi-key map with the standard library
Show a simple Hello World with SpringBoot + IntelliJ
A simple rock-paper-scissors game with JavaFX and SceneBuilder
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Create a docker image that runs a simple Java app
A simple CRUD app made with Nuxt / Laravel (Docker)
Practice making a simple chat app with Docker + Sinatra
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Create a widget template for iOS14 with Intent Configuration.
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Create a user with an empty password on CentOS7
[Note] Create a java environment from scratch with docker
Create a Service with an empty model Liferay 7.0 / DXP
Try create with Trailblazer
[Java] Create a filter
Create a blog with Jekyll and GitHub Pages @ Theme setting
I tried to create a java8 development environment with Chocolatey
Tutorial to create a blog with Rails for beginners Part 1
Create a page control that can be used with RecyclerView
Create a Docker image with the Oracle JDK installed (yum
[Rails] I tried to create a mini app with FullCalendar
[Beginner] Try to make a simple RPG game with Java ①
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
Create a Hello World web app with Spring framework + Jetty