Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
实际上这样就导致了我们的代码是比较类似的,先来看下我们的基本实现
一.基本实现
布局还是那个布局,只不过是横向的了
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/mHorizontalBarChart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
而初始化这些也都是大同小异
//正负堆叠条形图
mHorizontalBarChart = (HorizontalBarChart) findViewById(R.id.mHorizontalBarChart);
mHorizontalBarChart.setOnChartValueSelectedListener(this);
mHorizontalBarChart.setDrawGridBackground(false);
mHorizontalBarChart.getDescription().setEnabled(false);
// 扩展现在只能分别在x轴和y轴
mHorizontalBarChart.setPinchZoom(false);
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
mHorizontalBarChart.setDrawBarShadow(false);
mHorizontalBarChart.setDrawValueAboveBar(true); mHorizontalBarChart.setHighlightFullBarEnabled(false);
mHorizontalBarChart.getAxisLeft().setEnabled(false); mHorizontalBarChart.getAxisRight().setAxisMaximum(25f); mHorizontalBarChart.getAxisRight().setAxisMinimum(-25f); mHorizontalBarChart.getAxisRight().setDrawGridLines(false); mHorizontalBarChart.getAxisRight().setDrawZeroLine(true); mHorizontalBarChart.getAxisRight().setLabelCount(7, false); mHorizontalBarChart.getAxisRight().setValueFormatter(new CustomFormatter()); mHorizontalBarChart.getAxisRight().setTextSize(9f);
XAxis xAxis = mHorizontalBarChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setTextSize(9f);
xAxis.setAxisMinimum(0f);
xAxis.setAxisMaximum(110f);
xAxis.setCenterAxisLabels(true);
xAxis.setLabelCount(12);
xAxis.setGranularity(10f);
//日期格式化
xAxis.setValueFormatter(new IAxisValueFormatter() {
private DecimalFormat format = new DecimalFormat("###");
@Override
public String getFormattedValue(float value, AxisBase axis) {
return format.format(value) + "-" + format.format(value + 10);
}
@Override
public int getDecimalDigits() {
return 0;
}
});
Legend l = mHorizontalBarChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
l.setFormSize(8f);
l.setFormToTextSpace(4f);
l.setXEntrySpace(6f);
// 重要:当使用负值在堆叠酒吧,总是确保-值数组中的第一个
ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
yValues.add(new BarEntry(5, new float[]{-10, 10}));
yValues.add(new BarEntry(15, new float[]{-12, 13}));
yValues.add(new BarEntry(25, new float[]{-15, 15}));
yValues.add(new BarEntry(35, new float[]{-17, 17}));
yValues.add(new BarEntry(45, new float[]{-19, 20}));
yValues.add(new BarEntry(55, new float[]{-19, 19}));
yValues.add(new BarEntry(65, new float[]{-16, 16}));
yValues.add(new BarEntry(75, new float[]{-13, 14}));
yValues.add(new BarEntry(85, new float[]{-10, 11}));
yValues.add(new BarEntry(95, new float[]{-5, 6}));
yValues.add(new BarEntry(105, new float[]{-1, 2}));
BarDataSet set = new BarDataSet(yValues, "全国人口普查");
set.setValueFormatter(new CustomFormatter());
set.setValueTextSize(7f);
set.setAxisDependency(YAxis.AxisDependency.RIGHT);
set.setColors(new int[]{Color.rgb(99, 67, 72), Color.rgb(14, 181, 136)});
set.setStackLabels(new String[]{"男人", "女人"});
String[] xLabels = new String[]{"0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100+"};
BarData data = new BarData(set);
data.setBarWidth(8.5f);
mHorizontalBarChart.setData(data);
mHorizontalBarChart.invalidate();
这里我并没有写setData的方法,直接就模拟了一些数据,可以看出,这个图标是异常的简单,当我们运行之后就可以知道,这个实现的效果和上图的效果是一致的
二.显示顶点值
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
现在我把源码贴上
activity_statkedbar_negative.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://www.77cn.com.cn/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.github.mikephil.charting.charts.HorizontalBarChart android:id="@+id/mHorizontalBarChart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_show_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顶点显示值"/>
Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
<Button
相关推荐:
- [教学研究]2012西拉科学校团少队工作总结
- [教学研究]建筑工程公司档案管理制度
- [教学研究]小学数学人教版六年级上册圆的周长和面
- [教学研究]ERP电子行业解决方案
- [教学研究]钢支撑租赁合同范本
- [教学研究]预应力自动张拉系统用户手册Rev1.0
- [教学研究]MOOC课程:金瓶梅人物写真(每章节课后
- [教学研究]追加被执行人申请书(适用追加夫妻关系)
- [教学研究]2014年驾考科目一考试最新题库766
- [教学研究]2013-2014学年度九年级物理第15章《电
- [教学研究]新版中日交流标准日本语初级下26课-客
- [教学研究]小导管注浆施工作业指导书
- [教学研究]一般财务人员能力及人岗匹配评估表
- [教学研究]打1.2.页 小学一年级暑假口算100以内加
- [教学研究]学习贯彻《中国共产党党和国家机关基层
- [教学研究]2012年呼和浩特市中考试卷_35412
- [教学研究]最简易的电线电缆购销合同范本
- [教学研究]如何开展安全标准化建设
- [教学研究]工作分析与人岗匹配
- [教学研究]2016-2017学年高中历史第七单元现代中
- 山东省义务教育必修地方课程小学三年级
- 台湾宜兰大学互联网交换技术课程 01_In
- 思想品德:第一课《我知我家》课件(人
- SAR合成孔径雷达图像点目标仿真报告(附
- 利辛县“十三五”规划研究报告
- 2015-2020年中国手机APP行业市场发展趋
- 广告策略、创意表现、媒体方案
- 企业如何申请专利的的几点思考
- 《中国教育简史》网上作业
- 高中历史第二单元西方人文精神的起源及
- 年终晚会必备_精彩的主持稿_精心整理_
- 信息工程专业自荐书
- 2019高考历史人教版一轮练习:第十二单
- JAVA俱乐部管理系统软件需求规格说明书
- 2016-2021年中国小型板料折弯机行业市
- (人教新课标)六上_比的基本性质课件PPT
- 辽宁省公务员考试网申论备考技巧:名言
- 神经阻滞麻醉知情同意书
- 施工企业信息填报、审核和发布的相关事
- 初一(七年级)英语完形填空100篇




