Android统计图表之柱状图(条形图)

简介: Android统计图表之柱状图(条形图)柱状图是统计图表中经常用到的一种图表,比如降雨量之类的统计展示。我之前写了一些关于Android上的统计图表库MPAndroidChart,附录了一些我自己写的技术文档,在这些文档中介绍了MPAndroidChart的详细内容。


Android统计图表之柱状图(条形图)

柱状图是统计图表中经常用到的一种图表,比如降雨量之类的统计展示。我之前写了一些关于Android上的统计图表库MPAndroidChart,附录了一些我自己写的技术文档,在这些文档中介绍了MPAndroidChart的详细内容。

文章:
1,《Android统计图表MPAndroidChart》,http://blog.csdn.net/zhangphil/article/details/47656521
2,《基于Android MPAndroidChart实现腾讯QQ群数据统计报表核心功能》,http://blog.csdn.net/zhangphil/article/details/47685515
3,《Android实现天气预报温度/气温折线趋势图》,http://blog.csdn.net/zhangphil/article/details/47702245

现在基于Android平台上的MPAndroidChart,在Android上实现柱状图,以降雨量为例,制作一个简单的降雨量柱状图(条形图)。

测试的主MainActivity.java

package zhangphil.barchart;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ValueFormatter;

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

public class MainActivity extends ActionBarActivity {

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

		BarChart mBarChart = (BarChart) findViewById(R.id.bar_chart);

		setBarChartStyle(mBarChart);

		// 制作10个数据点。
		setData(mBarChart, 10);
	}

	private void setBarChartStyle(BarChart mBarChart) {

		mBarChart.setDrawBarShadow(false);
		mBarChart.setDrawValueAboveBar(true);
		mBarChart.setDescription("@ http://blog.csdn.net/zhangphil");
		mBarChart.setMaxVisibleValueCount(60);
		mBarChart.setPinchZoom(false);
		mBarChart.setDrawGridBackground(false);

		XAxis xAxis = mBarChart.getXAxis();
		xAxis.setPosition(XAxisPosition.BOTTOM);
		xAxis.setDrawGridLines(false);
		xAxis.setSpaceBetweenLabels(2);

		YAxis leftAxis = mBarChart.getAxisLeft();
		leftAxis.setLabelCount(5, false);
		leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
		leftAxis.setSpaceTop(15f);
		leftAxis.setTextColor(Color.BLUE);

		YAxis rightAxis = mBarChart.getAxisRight();
		rightAxis.setDrawGridLines(false);
		rightAxis.setLabelCount(5, false);
		rightAxis.setSpaceTop(15f);
		rightAxis.setTextColor(Color.GREEN);

		Legend mLegend = mBarChart.getLegend();
		mLegend.setPosition(LegendPosition.BELOW_CHART_CENTER);
		mLegend.setForm(LegendForm.SQUARE);
		mLegend.setFormSize(15f);
		mLegend.setTextSize(12f);
		mLegend.setXEntrySpace(5f);
	}

	private void setData(BarChart mBarChart, int count) {

		ArrayList<String> xVals = new ArrayList<String>();
		for (int i = 0; i < count; i++) {
			xVals.add(i, i + "");
		}

		ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();

		for (int i = 0; i < count; i++) {
			float val = (float) (Math.random() * 100);
			yVals.add(new BarEntry(val, i));
		}

		BarDataSet mBarDataSet = new BarDataSet(yVals, "柱状图测试数据");

		// 如果是0f,那么柱状图之间将紧密无空隙的拼接在一起形成一片。
		mBarDataSet.setBarSpacePercent(30f);

		// 柱状图柱的颜色
		mBarDataSet.setColor(Color.RED);

		// 当柱状图某一柱被选中时候的颜色
		mBarDataSet.setHighLightColor(Color.YELLOW);

		mBarDataSet.setValueFormatter(new ValueFormatter() {

			@Override
			public String getFormattedValue(float value) {
				DecimalFormat decimalFormat = new DecimalFormat(".0");
				String s = decimalFormat.format(value) + "毫米";

				return s;
			}
		});

		ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
		dataSets.add(mBarDataSet);

		BarData mBarData = new BarData(xVals, dataSets);
		mBarData.setValueTextSize(12f);

		mBarChart.setData(mBarData);
	}
}




MainActivity.java需要的布局文件activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/bar_chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>


运行结果如图:


相关文章
|
Android开发
Android统计图表MPAndroidChart:动态添加数据更新【6】
 Android统计图表MPAndroidChart:动态添加数据更新【6】 Android MPAndroidChart的LineDataSet代表一条统计图表中统计折线,一张统计图表可以同时存在若干条统计折线,其在内存中存储的模型类型数组,从0开始下标。
3008 0
|
前端开发 Android开发 Kotlin
Android 进阶自定义View(3)图表统计BarChartView柱状图的实现
《一》导语: 最近写了几个统计数据相关的图表,刚好放在自定义View这块的跟大家分享一下。对于图表这块,可能对于很多App的开发都用得不是很多,但是对于一些有数据分析统计需求相关的,例如P2P类型的,就比较常用了。
3977 0
|
Android开发 Java
Android统计图表绘制:基于新版MPAndroidChart绘制数理统计柱状图
Android统计图表绘制:基于新版MPAndroidChart绘制数理统计柱状图 github上的开源项目MPAndroidChart在最新的3.0.3中,绘制图表的机制有所改变,从数据到图形,需要以新的方式绘制。
2364 0
|
前端开发 Android开发 索引
Android自定义View--自己撸一个柱状图也没那么难
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/53771477 本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发。
1039 0
|
Android开发
Android图表库MPAndroidChart(十三)——简约的底部柱状图
Android图表库MPAndroidChart(十三)——简约的底部柱状图 我们继续上一讲,今天还是说下柱状图,这个图的话应该是用的比较多的,所有拿出来溜溜,先看下效果 我们还是来看下基本实现 一.
1269 0
|
Android开发
Android统计图表MPAndroidChart:为多条统计折线动态更新数据,以高温低温曲线为例【7】
 Android统计图表MPAndroidChart:为多条统计折线动态更新数据,以高温低温曲线为例【7】 本文在附录文章6的基础上,为Android统计图表MPAndroidChart的同一个LineChart中同时增加两条统计折线,动态的为这两条折线同时增加数据并更新结果。
2453 0
|
Java Android开发 数据格式
Android统计图表MPAndroidChart
 Android统计图表MPAndroidChart MPAndroidChart是在Android平台上开源的第三方统计图表库,可以绘制样式复杂、丰富的各种统计图表,如一般常见的折线图、饼状图、柱状图、散点图、金融股票中使用的的“蜡烛”图、“泡泡”统计图、雷达状统计饼状图等等。
1249 0
|
21天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
1月前
|
设计模式 人工智能 开发工具
安卓应用开发:构建未来移动体验
【2月更文挑战第17天】 随着智能手机的普及和移动互联网技术的不断进步,安卓应用开发已成为一个热门领域。本文将深入探讨安卓平台的应用开发流程、关键技术以及未来发展趋势。通过分析安卓系统的架构、开发工具和框架,本文旨在为开发者提供全面的技术指导,帮助他们构建高效、创新的移动应用,以满足不断变化的市场需求。
18 1
|
12天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。