Android实现天气预报温度/气温折线趋势图

天气预报的APP应用中,难免会遇到绘制天气温度/气温,等关于数据趋势的折线或者曲线图,这类关于气温/温度的折线图,通常会有两条线。一条是高温线,一条是低温线。

我之前介绍了一个Android平台上第三方开源框架的统计图表库MPAndroidChart(文章链接地址:http://blog.csdn.net/zhangphil/article/details/47656521 ),详细用法详情请看这篇文章。

如今基于Android平台上的MPAndroidChart实现气温/温度折线图。

主Activity:MainActivity.java的所有代码:

package zhangphil.chart;

import java.text.DecimalFormat;
import java.util.ArrayList; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle; import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ValueFormatter; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LineChart mChart = (LineChart) findViewById(R.id.chart);
setChart(mChart); // 制作5个数据点。
setData(mChart, 5); Legend l = mChart.getLegend();
l.setForm(LegendForm.LINE);
l.setTextSize(12f);
l.setTextColor(Color.BLACK);
l.setPosition(LegendPosition.BELOW_CHART_CENTER); XAxis xAxis = mChart.getXAxis(); // 将X坐标轴的标尺刻度移动底部。
xAxis.setPosition(XAxisPosition.BOTTOM); // X轴之间数值的间隔
xAxis.setSpaceBetweenLabels(1); xAxis.setTextSize(12f);
xAxis.setTextColor(Color.BLACK); YAxis leftAxis = mChart.getAxisLeft();
setYAxisLeft(leftAxis); YAxis rightAxis = mChart.getAxisRight();
setYAxisRight(rightAxis);
} private void setChart(LineChart mChart) {
mChart.setDescription("@ http://blog.csdn.net/zhangphil");
mChart.setNoDataTextDescription("假设传递的数值是空,那么你将看到这段文字。");
mChart.setHighlightEnabled(true);
mChart.setTouchEnabled(true);
mChart.setDragDecelerationFrictionCoef(0.9f);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(true);
mChart.setHighlightPerDragEnabled(true);
mChart.setPinchZoom(true);
mChart.setBackgroundColor(Color.LTGRAY);
mChart.animateX(3000);
} private void setYAxisLeft(YAxis leftAxis) {
// 在左側的Y轴上标出4个刻度值
leftAxis.setLabelCount(4, true); // Y坐标轴轴线的颜色
leftAxis.setGridColor(Color.RED); // Y轴坐标轴上坐标刻度值的颜色
leftAxis.setTextColor(Color.RED); // Y坐标轴最大值
leftAxis.setAxisMaxValue(50); // Y坐标轴最小值
leftAxis.setAxisMinValue(10); leftAxis.setStartAtZero(false); leftAxis.setDrawLabels(true);
} private void setYAxisRight(YAxis rightAxis) {
// Y坐标轴上标出8个刻度值
rightAxis.setLabelCount(8, true); // Y坐标轴上刻度值的颜色
rightAxis.setTextColor(Color.BLUE); // Y坐标轴上轴线的颜色
rightAxis.setGridColor(Color.BLUE); // Y坐标轴最大值
rightAxis.setAxisMaxValue(30); // Y坐标轴最小值
rightAxis.setAxisMinValue(-5); rightAxis.setStartAtZero(false);
rightAxis.setDrawLabels(true);
} private void setData(LineChart mChart, int count) { ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < count; i++) {
xVals.add("某月" + (i + 1) + "日");
} ArrayList<Entry> yHigh = new ArrayList<Entry>();
LineDataSet high = new LineDataSet(yHigh, "高温");
setHighTemperature(high, yHigh, count); ArrayList<Entry> yLow = new ArrayList<Entry>();
LineDataSet low = new LineDataSet(yLow, "低温");
setLowTemperature(low, yLow, count); ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(high);
dataSets.add(low); LineData data = new LineData(xVals, dataSets);
data.setValueTextColor(Color.DKGRAY);
data.setValueTextSize(10f);
mChart.setData(data);
} private void setHighTemperature(LineDataSet high, ArrayList<Entry> yVals,
int count) { for (int i = 0; i < count; i++) {
float val = (float) Math.random() + 30;
yVals.add(new Entry(val, i));
} // 以左边的Y坐标轴为准
high.setAxisDependency(AxisDependency.LEFT); high.setLineWidth(5f);
high.setColor(Color.RED);
high.setCircleSize(8f);
high.setCircleColor(Color.YELLOW);
high.setCircleColorHole(Color.DKGRAY);
high.setDrawCircleHole(true); // 设置折线上显示数据的格式。假设不设置,将默认显示float数据格式。
high.setValueFormatter(new ValueFormatter() { @Override
public String getFormattedValue(float value) {
DecimalFormat decimalFormat = new DecimalFormat(".0");
String s = "高温" + decimalFormat.format(value);
return s;
}
}); } private void setLowTemperature(LineDataSet low, ArrayList<Entry> yVals,
int count) { for (int i = 0; i < count; i++) {
float val = (float) Math.random() + 5;
yVals.add(new Entry(val, i));
} // 以右边Y坐标轴为准
low.setAxisDependency(AxisDependency.RIGHT); // 折现的颜色
low.setColor(Color.GREEN); // 线宽度
low.setLineWidth(3f); // 折现上点的圆球颜色
low.setCircleColor(Color.BLUE); // 填充圆球中心部位洞的颜色
low.setCircleColorHole(Color.LTGRAY); // 圆球的尺寸
low.setCircleSize(5f); low.setDrawCircleHole(true); low.setValueFormatter(new ValueFormatter() { @Override
public String getFormattedValue(float value) {
DecimalFormat decimalFormat = new DecimalFormat(".0");
String s = "低温" + decimalFormat.format(value);
return s;
}
});
}
}

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.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout>

执行结果如图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

须要对MPAndroidChart的坐标体系加以说明。MPAndroidChart的Y纵坐标轴分为左右两条纵坐标:左纵坐标轴(chart的getAxisLeft()获得的YAxis)和右纵坐标轴(chart的getAxisRight()获得的YAxis)。尽管它们都是MPAndroidChart中的Yaxis一个类代表。但它们在详细使用中是相互独立的。但它们共用X坐标轴(横坐标)。

比方在本例中,左边的红色Y纵坐标轴独立代表高温折线。它有自己独立执行的一套画图机制;同理。右边蓝色Y纵坐标轴独立代表的低温折线,它也有自己独立执行的一套画图机制。不要将两者混为一谈。

Android实现天气预报温度/气温折线趋势图的更多相关文章

  1. Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图

    Android图表库MPAndroidChart(八)--饼状图的扩展:折线饼状图 我们接着上文,饼状图的扩展,增加折现的说明,来看下我们要实现的效果 因为之前对MPAndroidChart的熟悉,所 ...

  2. [Android] 免费天气预报接口

    [Android] 免费天气预报接口 这是 国家气象局提供的天气预报接口 [免费] 当然,网上有很多的收费API或者每天定次数的接口 使用 国家气象局 的步骤如下: 1.首先获取城市ID号 北京:10 ...

  3. Locust 测试结果通过Matplotlib生成趋势图

    目的: 相信大家对于使用Loadrunner测试后的结果分析详细程度还是有比较深刻的感受的,每个请求,每个事务点等都会有各自的趋势指标,在同一张图标中展示.如下图: 而Locust自身提供的chart ...

  4. 使用highcharts实现无其他信息纯趋势图实战实例

    使用highcharts实现无其他信息纯趋势图实战实例 Highcharts去掉或者隐藏掉y轴的刻度线yAxis : { gridLineWidth: 0, labels:{ //enabled:fa ...

  5. python3 读取txt文件数据,绘制趋势图,matplotlib模块

    python3 读取txt文件数据,绘制趋势图 test1.txt内容如下: 时间/min cpu使用率/% 内存使用率/% 01/12-17:06 0.01 7.61 01/12-17:07 0.0 ...

  6. csv2ECharts,**一行命令查看数据趋势图 工具分享**

    csv2ECharts 一行命令查看数据趋势图! 联系:luomgf@163.com,欢迎交流提出建议 只有一个文件,基于shell,实现将CSV格式数据转化为数据图.运维中尝尝需要查看某个监控指标的 ...

  7. 【开源】专业K线绘制[K线主副图、趋势图、成交量、滚动、放大缩小、MACD、KDJ等)

    这是一个iOS项目雅黑深邃的K线的绘制. 实现功能包括K线主副图.趋势图.成交量.滚动.放大缩小.MACD.KDJ,长按显示辅助线等功能 预览图 最后的最后,这是项目的开源地址:https://git ...

  8. Excel应该这么玩——7、我是预言家:绘制趋势图

    让我们先看一个场景:你是公司销售部的员工,你手里有公司最近几年的销售额相关的数据,经理希望你预测下个月的销售额.盯着一堆数据,你或许会想到画一张XY坐标图,然后将每个月份的销售额标定为一个坐标.但是下 ...

  9. 【mysql】一维数据TopN的趋势图

    创建数据表语句 数据表数据 对上述数据进行TopN排名 select severity,sum(count) as sum from widgt_23 where insertTstamp>=' ...

随机推荐

  1. Dynamics CRM2013 6.1.1.1143版本号插件注冊器的一个bug

    近期在做的项目客户用的是CRM2013sp1版本号,所以插件注冊器使用的也是与之相应的6.1.1.1143,悲剧的事情也因此而開始. 在插件中注冊step时,工具里有个run in user's co ...

  2. 初次使用Android Studio时的配置

    一.第一次安装: Android Studio安装完毕后,第一次启动AS前.为了避免又一次下载新版本号的SDK.操作例如以下: AS启动前.请先将bin文件夹的idea.properties文件里添加 ...

  3. 【翻译自mos文章】 11gR1版本号 asmcmd的新命令--cp、md_backup、md_restore

    11gR1版本号 asmcmd的新命令--cp.md_backup.md_restore 參考原文: ASMCMD - New commands in 11gR1 (Doc ID 451900.1) ...

  4. 2015.05.15,外语,学习笔记-《Word Power Made Easy》 01 “如何讨论人格特点”

    2015.03.17,外语,读书笔记-<Word Power Made Easy> 01 “如何讨论人格特点”学习笔记 SESSIONS 1 本来这些章节都是在一两年前学习的,现在趁给友人 ...

  5. File and Folder Permissions

    https://msdn.microsoft.com/en-us/library/bb727008.aspx On NTFS volumes, you can set security permiss ...

  6. shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言

    博客背景和目的 最近在用C++写一个底层的东西,需要读取和创建shp文件.虽然接触shp文件已经几年了,但是对于shp文件内到底包含什么东西一直是一知半解.以前使用shp文件都是利用软件(如ArcGI ...

  7. matlab基本语法

    MATLAB基本语法 点乘运算 , 常与其他运算符 点乘运算,常与其他运算符联合使用(如.\) 矩阵生成 矩阵生成 向量生成或子阵提取本节将会介绍一些MATLAB的基本语法的使用. 持续更新... 在 ...

  8. Redis学习笔记(十) 命令进阶:事务操作

    原文链接:http://doc.redisfans.com/transaction/index.html Redis中也提供了对于事务的支持,由于Redis是单线程处理Client的请求,所以实现起来 ...

  9. wampserver的安装及使用配置方法

    在安装wampserver的过程中,根据本人在安装过程中遇到的问题,在此做个总结,与小伙伴们分享一下下~~~. 1. 何处获得Wamp ★ 在自己的Windows电脑上Web服务器软件 - Wamp. ...

  10. BottomSheetBehavior 结合CoordinatorLayout实现底部栏

    1.xml <?xml version="1.0" encoding="utf-8"?> <android.support.design.wi ...