Draw a bar graph and a line graph at the same time with MPAndroidChart

I'll put the code on it for the time being (I'll write it clean later)

List<String> labels = new ArrayList<>();
List<BarEntry> bar = new ArrayList<>();
List<Entry> line = new ArrayList<>();
Random random = new Random();
for (int i=0; i<10; i++) {
	//The label for the x-axis is List<String>
    labels.add(String.valueOf(i));
    
    //Each data is List<BarEntry>, List<Entry>Store in etc.
    // Entry(float value, int index)
    //A run-time error occurs if you specify an index longer than the length of labels
    bar.add(new BarEntry((float)random.nextInt(10), i));
    line.add(new Entry((float)random.nextInt(5)+3f, i));
}

//List of entries(List<Entry>)+ Name(String) => DataSet
//List of labels(List<String>) + DataSet => Data

//bar graph
BarDataSet barDataSet = new BarDataSet(bar, "bar");
BarData barData = new BarData(labels, barDataSet);

//Line graph
LineDataSet lineDataSet = new LineDataSet(line, "line");
lineDataSet.setColor(Color.BLACK);
lineDataSet.setLineWidth(1.5f);
lineDataSet.setDrawCircles(false);

LineData lineData = new LineData(labels, lineDataSet);
lineData.setDrawValues(false);

//combine
CombinedData data = new CombinedData(labels);
data.setData(lineData);
data.setData(barData);
chart.setData(data);

//chart
CombinedChart chart = (CombinedChart)findViewById(R.id.combined_chart);
chart.setBackgroundColor(Color.WHITE);
chart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);

chart.setTouchEnabled(true);
chart.setPinchZoom(false);
chart.setDoubleTapToZoomEnabled(false);

chart.setHighlightEnabled(false);
chart.setDescription("");

//x-axis setting
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setSpaceBetweenLabels(0);

//Right y-axis setting
chart.getAxisRight().setEnabled(false);

//animation
chart.invalidate();
chart.animateY(2000, Easing.EasingOption.EaseInBack);

Recommended Posts

Draw a bar graph and a line graph at the same time with MPAndroidChart
Draw a graph with Sinatra and Chartkick
How to delete child elements associated with a parent element at the same time
Form and process file and String data at the same time with Spring Boot + Java
Check the actual date and time at parse with Java's SimpleDateFormat
Setting to start multiple units at the same time with Vagrant
When I made a bar graph with MPAndroidChart, the x-axis label was misaligned for some reason
Create a simple bar chart with MPAndroidChart
[Spring Boot] Post files and other data at the same time [Axios]
[Ruby] Define the hierarchy at the same time as Hash initialization with tap method
Behavior noticed when adding RadioButton and initial check at the same time in code
[Personal application work memo] How to display a bar graph and a line graph in one graph
[Swift 5] Draw a line.
[Java] Method call error when inheritance and interface implementation are performed at the same time
Implementation method of linking multiple images to one post and posting at the same time
Set the date and time from the character string with POI
Why put a line break at the end of the file
With the software I've been making for a long time ...
Modeling a Digimon with DDD for the first time Part 1
[Rails] Get access_token at the time of Twitter authentication with Sorcery and save it in DB
Build a NAS with DLNA function at the speed of a second with Raspberry Pi and Docker Compose
I tried to express the result of before and after of Date class with a number line
Launch webpack-dev-server and docker-compose at the same time on Windows Terminal and display them in another pane
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Draw a gradient with CAGradientLayer
About the same and equivalent
Logic to draw a circle with ASCII art on the console
Lock_version may be used for tables that tend to access and edit the same record at the same time
How to download and run a Jar package directly from the Maven repository with just the command line