LineChart:
package com.example.air_conditioner;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import java.util.List;
/**
* 动态折线图
*/
public class LineChart {
private GraphicalView mGraphicalView;
private XYMultipleSeriesDataset multipleSeriesDataset;// 数据集容器
private XYMultipleSeriesRenderer multipleSeriesRenderer;// 渲染器容器
private XYSeries mSeries;// 单条曲线数据集
private XYSeriesRenderer mRenderer;// 单条曲线渲染器
private Context context;
public LineChart(Context context) {
this.context = context;
}
/**
* 获取图表
*
* @return
*/
public GraphicalView getGraphicalView() {
mGraphicalView = ChartFactory.getCubeLineChartView(context, multipleSeriesDataset, multipleSeriesRenderer, 0.1f);
return mGraphicalView;
}
/**
* 获取数据集,及xy坐标的集合
*
* @param curveTitle 曲线名称
*/
public void setXYMultipleSeriesDataset(String curveTitle) {
multipleSeriesDataset = new XYMultipleSeriesDataset();
mSeries = new XYSeries(curveTitle);
multipleSeriesDataset.addSeries(mSeries);
}
/**
* 获取渲染器
*
* @param maxX
* x轴最大值
* @param maxY
* y轴最大值
* @param chartTitle
* 曲线的标题
* @param xTitle
* x轴标题
* @param yTitle
* y轴标题
* @param axeColor
* 坐标轴颜色
* @param labelColor
* 标题颜色
* @param curveColor
* 曲线颜色
* @param gridColor
* 网格颜色
*/
public void setXYMultipleSeriesRenderer(double maxX, double maxY,
String chartTitle, String xTitle, String yTitle, int axeColor,
int labelColor, int curveColor, int gridColor) {
multipleSeriesRenderer = new XYMultipleSeriesRenderer();
if (chartTitle != null) {
multipleSeriesRenderer.setChartTitle(chartTitle);
}
// 渲染器容器
multipleSeriesRenderer.setXTitle(xTitle);
multipleSeriesRenderer.setYTitle(yTitle);
// 设置xy坐标轴的范围
multipleSeriesRenderer.setRange(new double[] { 0, maxX, 0, maxY });//xy轴的范围
// 标题颜色
multipleSeriesRenderer.setLabelsColor(labelColor);
multipleSeriesRenderer.setXLabels(10);
multipleSeriesRenderer.setYLabels(10);
multipleSeriesRenderer.setXLabelsAlign(Align.RIGHT);
multipleSeriesRenderer.setYLabelsAlign(Align.RIGHT);
// 设置坐标轴的标题字体大小
multipleSeriesRenderer.setAxisTitleTextSize(20);
// 设置坐标轴的标题大小
multipleSeriesRenderer.setChartTitleTextSize(20);
multipleSeriesRenderer.setLabelsTextSize(20);
multipleSeriesRenderer.setLegendTextSize(20);
multipleSeriesRenderer.setPointSize(2f);//曲线描点尺寸
multipleSeriesRenderer.setFitLegend(true);
multipleSeriesRenderer.setMargins(new int[] { 20, 30, 15, 20 });
// 设置网格
multipleSeriesRenderer.setShowGrid(true);
multipleSeriesRenderer.setZoomEnabled(true, false);
// 设置坐标轴颜色
multipleSeriesRenderer.setAxesColor(axeColor);
multipleSeriesRenderer.setGridColor(gridColor);
multipleSeriesRenderer.setAxesColor(Color.rgb(128, 138, 135)); //坐标轴颜色
multipleSeriesRenderer.setLabelsColor(Color.WHITE); //标签颜色
multipleSeriesRenderer.setMarginsColor(Color.rgb(37, 40, 46)); //图表周围颜色
multipleSeriesRenderer.setGridColor(Color.DKGRAY); //表格颜色
// multipleSeriesRenderer.setBackgroundColor(Color.BLACK);//背景色
// multipleSeriesRenderer.setMarginsColor(Color.BLACK);//边距背景色,默认背景色为黑色,这里修改为白色
mRenderer = new XYSeriesRenderer();
// 设置曲线颜色
mRenderer.setColor(curveColor);
mRenderer.setPointStyle(PointStyle.POINT);//描点风格,可以为圆点,方形点等等
multipleSeriesRenderer.addSeriesRenderer(mRenderer);
}
// TODO:
/**
* 设置图表属性和数据
*/
public void setChartSettings(XYMultipleSeriesRenderer renderer) {
// 添加自定义限制线
renderer.addYTextLabel(4.5, "Custom Line", 0);// 在 y=4.5 的位置添加一条标题为 "Custom Line" 的限制线
renderer.setChartTitle("Chart demo");
renderer.setXTitle("xValues");
renderer.setYTitle("yValues");
renderer.setXAxisMin(0.5);
renderer.setXAxisMax(10.5);
renderer.setYAxisMin(0);
renderer.setYAxisMax(12);
}
/**
* 根据新加的数据,更新曲线,只能运行在主线程
*
* @param x
* 新加点的x坐标
* @param y
* 新加点的y坐标
*/
// public void updateChart(double x, double y) {
// mSeries.add(x, y);
// mGraphicalView.repaint(); //此处也可以调用invalidate()
//
// }
/**
* 根据新加的数据,更新曲线,并将图表滚动到最新的点处,只能运行在主线程
*
* @param x 新加点的x坐标
* @param y 新加点的y坐标
*/
public void updateChart(double x, double y) {
mSeries.add(x, y);
// 扩大 x 轴范围以显示整个折线图
double maxX = x + 50;
multipleSeriesRenderer.setRange(new double[] { 0, maxX, 0, multipleSeriesRenderer.getYAxisMax()});
if (mSeries.getItemCount() > 1) {
// 将 x 轴范围设为最新点的位置附近
double newMaxX = x + (multipleSeriesRenderer.getXAxisMax() - multipleSeriesRenderer.getXAxisMin()) / 50;
multipleSeriesRenderer.setRange(new double[] { newMaxX - 50, newMaxX, 0, multipleSeriesRenderer.getYAxisMax() });
}
mGraphicalView.repaint();
}
/**
* 添加新的数据,多组,更新曲线,只能运行在主线程
* @param yList
*/
public void updateChart(List<Float> yList) {
int flag = 10;
for (int i = 0; i < yList.size(); i++) {
mSeries.add(flag, yList.get(i));
flag += 10;
}
mGraphicalView.repaint();//此处也可以调用invalidate()
}
public void updateChart(int x,int[] yList) {
for (int i = 0; i < yList.length; i++) {
mSeries.add(x, yList[i]);
x++;
}
mGraphicalView.repaint();//此处也可以调用invalidate()
}
/**
* 清除曲线数据
*/
public void clearChart() {
mSeries.clear();
mGraphicalView.repaint();
}
}
DataFragment:
package com.example.air_conditioner;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.fragment.app.Fragment;
import org.achartengine.GraphicalView;
import java.util.ArrayList;
import java.util.List;
public class DataFragment extends Fragment {
/**
* 存放x、y、z轴加速度的曲线的布局容器
**/
private LinearLayout temperatureLayout; // 温度
private LinearLayout humidityLayout; // 湿度
private LinearLayout electricityLayout; // 电流
private LinearLayout voltageLayout; // 电压
private LinearLayout powerLayout; // 功率
/**
* 折线图
*/
private LineChart temperatureLineChart;
private LineChart humidityLineChart;
private LineChart electricityLineChart;
private LineChart voltageLineChart;
private LineChart powerLineChart;
/**
* 用来加载到布局器的view
**/
private GraphicalView temperature_wave_view;
private GraphicalView humidity_wave_view;
private GraphicalView electricity_wave_view;
private GraphicalView voltage_wave_view;
private GraphicalView power_wave_view;
/**
* 数据集合
*/
private List<Float> tList,vList,pList,eList,hList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.linechart, container, false);
initView(view);
return view;
}
private void initView(View view) {
temperatureLayout = view.findViewById(R.id.temperature);
humidityLayout = view.findViewById(R.id.humidity);
electricityLayout = view.findViewById(R.id.electricity);
voltageLayout = view.findViewById(R.id.voltage);
powerLayout = view.findViewById(R.id.power);
// 初始化图表
initChart();
temperatureLayout.addView(temperature_wave_view,new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
humidityLayout.addView(humidity_wave_view,new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
electricityLayout.addView(electricity_wave_view,new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
voltageLayout.addView(voltage_wave_view,new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
powerLayout.addView(power_wave_view,new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
private void initChart() {
temperatureLineChart = new LineChart(getContext());
powerLineChart = new LineChart(getContext());
humidityLineChart = new LineChart(getContext());
voltageLineChart = new LineChart(getContext());
electricityLineChart = new LineChart(getContext());
electricityLineChart.setXYMultipleSeriesDataset("电流曲线");
humidityLineChart.setXYMultipleSeriesDataset("湿度曲线");
powerLineChart.setXYMultipleSeriesDataset("功率曲线");
voltageLineChart.setXYMultipleSeriesDataset("电压曲线");
temperatureLineChart.setXYMultipleSeriesDataset("温度曲线");
electricityLineChart.setXYMultipleSeriesRenderer(120,10,"电流曲线","时间/min","电流/A",
Color.BLACK, Color.RED, Color.RED, Color.BLACK);;
temperatureLineChart.setXYMultipleSeriesRenderer(120,30,"温度曲线","时间/min","温度/°C",
Color.BLACK, Color.RED, Color.RED, Color.BLACK);
humidityLineChart.setXYMultipleSeriesRenderer(120,10,"湿度曲线","时间/min","湿度/RH",
Color.BLACK, Color.GREEN, Color.GREEN, Color.BLACK);
powerLineChart.setXYMultipleSeriesRenderer(120,10,"功率曲线","时间/min","功率/W",
Color.BLACK, Color.GREEN, Color.GREEN, Color.BLACK);
voltageLineChart.setXYMultipleSeriesRenderer(120,10,"电压曲线","时间/min","电压/V",
Color.BLACK, Color.WHITE, Color.WHITE, Color.BLACK);
temperature_wave_view = temperatureLineChart.getGraphicalView();
power_wave_view = powerLineChart.getGraphicalView();
voltage_wave_view = voltageLineChart.getGraphicalView();
humidity_wave_view = humidityLineChart.getGraphicalView();
electricity_wave_view = electricityLineChart.getGraphicalView();
// 初始化数据
initData();
}
private void initData() {
pList = new ArrayList<>();
vList = new ArrayList<>();
eList = new ArrayList<>();
hList = new ArrayList<>();
tList = new ArrayList<>();
for (int i = 0; i < 12; i++) {
pList.add(i, (float) (Math.random()*10));
vList.add(i, (float) (Math.random()*10));
eList.add(i, (float) (Math.random()*10));
hList.add(i, (float) (Math.random()*10));
tList.add(i, (float) (Math.random()*10)+10);
}
electricityLineChart.updateChart(eList);
humidityLineChart.updateChart(hList);
temperatureLineChart.updateChart(tList);
voltageLineChart.updateChart(vList);
powerLineChart.updateChart(pList);
}
}
linechart:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#25282e"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- 显示界面 -->
<LinearLayout
android:id="@+id/temperature"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
/>
<LinearLayout
android:id="@+id/humidity"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
/>
<LinearLayout
android:id="@+id/electricity"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
/>
<LinearLayout
android:id="@+id/voltage"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
/>
<LinearLayout
android:id="@+id/power"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
效果图: