Android开发画折线图 安卓开发图表 您所在的位置:网站首页 qmlchartview Android开发画折线图 安卓开发图表

Android开发画折线图 安卓开发图表

#Android开发画折线图 安卓开发图表| 来源: 网络整理| 查看: 265

Android开发画折线图 安卓开发图表 转载

幸福的地图 2023-05-17 14:22:42

文章标签 android github 数据 文章分类 Android 移动开发

    在这里使用的插件为Mpchart,只以折线图为例。首先需要导入

Android开发画折线图 安卓开发图表_android

包。

    在Layout.xml中设置布局,如下:

  在Activity中设置数据和图表的格式。如下:

package com.example.view; import java.util.ArrayList; import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.view.WindowManager; import com.example.iov.R; import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition; import com.github.mikephil.charting.charts.LineChart; 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.Legend; import com.github.mikephil.charting.utils.Legend.LegendForm; import com.github.mikephil.charting.utils.XLabels; import com.github.mikephil.charting.utils.XLabels.XLabelPosition; import com.github.mikephil.charting.utils.YLabels; public class gaschar extends Activity { private LineChart mChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.gasinfo); mChart = (LineChart) findViewById(R.id.chart1); // 设置在Y轴上是否是从0开始显示 mChart.setStartAtZero(true); //是否在Y轴显示数据,就是曲线上的数据 mChart.setDrawYValues(true); //设置网格 mChart.setDrawBorder(true); mChart.setBorderPositions(new BorderPosition[] { BorderPosition.BOTTOM}); //在chart上的右下角加描述 mChart.setDescription("曲线图"); //设置Y轴上的单位 mChart.setUnit("¥"); //设置透明度 mChart.setAlpha(0.8f); //设置网格底下的那条线的颜色 mChart.setBorderColor(Color.rgb(213, 216, 214)); //设置Y轴前后倒置 mChart.setInvertYAxisEnabled(false); //设置高亮显示 mChart.setHighlightEnabled(true); //设置是否可以触摸,如为false,则不能拖动,缩放等 mChart.setTouchEnabled(true); //设置是否可以拖拽,缩放 mChart.setDragEnabled(true); mChart.setScaleEnabled(true); //设置是否能扩大扩小 mChart.setPinchZoom(true); // 设置背景颜色 // mChart.setBackgroundColor(Color.GRAY); //设置点击chart图对应的数据弹出标注 MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view); // define an offset to change the original position of the marker // (optional) mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight()); // set the marker to the chart mChart.setMarkerView(mv); // enable/disable highlight indicators (the lines that indicate the // highlighted Entry) mChart.setHighlightIndicatorEnabled(false); //设置字体格式,如正楷 Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); mChart.setValueTypeface(tf); XLabels xl = mChart.getXLabels(); // xl.setAvoidFirstLastClipping(true); // xl.setAdjustXLabels(true); xl.setPosition(XLabelPosition.BOTTOM); // 设置X轴的数据在底部显示 xl.setTypeface(tf); // 设置字体 xl.setTextSize(10f); // 设置字体大小 xl.setSpaceBetweenLabels(3); // 设置数据之间的间距 YLabels yl = mChart.getYLabels(); // yl.setPosition(YLabelPosition.LEFT_INSIDE); // set the position yl.setTypeface(tf); // 设置字体 yl.setTextSize(10f); // s设置字体大小 yl.setLabelCount(5); // 设置Y轴最多显示的数据个数 // 加载数据 setData(); //从X轴进入的动画 mChart.animateX(4000); mChart.animateY(3000); //从Y轴进入的动画 mChart.animateXY(3000, 3000); //从XY轴一起进入的动画 //设置最小的缩放 mChart.setScaleMinima(0.5f, 1f); //设置视口 // mChart.centerViewPort(10, 50); // get the legend (only possible after setting data) Legend l = mChart.getLegend(); l.setForm(LegendForm.LINE); //设置图最下面显示的类型 l.setTypeface(tf); l.setTextSize(15); l.setTextColor(Color.rgb(104, 241, 175)); l.setFormSize(30f); // set the size of the legend forms/shapes // 刷新图表 mChart.invalidate(); } private void setData() { String[] aa = {"1","2","3","4","5","6","7","8","9","10","11","12","月"}; String[] bb = {"122.00","234.34","85.67","117.90","332.33","113.33"}; ArrayList xVals = new ArrayList(); for (int i = 0; i < aa.length; i++) { xVals.add(aa[i]); } ArrayList yVals = new ArrayList(); for (int i = 0; i < bb.length; i++) { yVals.add(new Entry(Float.parseFloat(bb[i]), i)); } // create a dataset and give it a type LineDataSet set1 = new LineDataSet(yVals, "DataSet Line"); set1.setDrawCubic(true); //设置曲线为圆滑的线 set1.setCubicIntensity(0.2f); set1.setDrawFilled(false); //设置包括的范围区域填充颜色 set1.setDrawCircles(true); //设置有圆点 set1.setLineWidth(2f); //设置线的宽度 set1.setCircleSize(5f); //设置小圆的大小 set1.setHighLightColor(Color.rgb(244, 117, 117)); set1.setColor(Color.rgb(104, 241, 175)); //设置曲线的颜色 // create a data object with the datasets LineData data = new LineData(xVals, set1); // set data mChart.setData(data); } }

其中MyMarkerView(点击节点类)代码:

package com.example.view; import android.content.Context; import android.widget.TextView; import com.example.iov.R; import com.github.mikephil.charting.data.CandleEntry; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.utils.MarkerView; import com.github.mikephil.charting.utils.Utils; public class MyMarkerView extends MarkerView { private TextView tvContent; public MyMarkerView(Context context, int layoutResource) { super(context, layoutResource); tvContent = (TextView) findViewById(R.id.tvContent); } // callbacks everytime the MarkerView is redrawn, can be used to update the // content @Override public void refreshContent(Entry e, int dataSetIndex) { if (e instanceof CandleEntry) { CandleEntry ce = (CandleEntry) e; tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true)); } else { // tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true)); tvContent.setText("" +e.getVal()); } } }

效果如下:

Android开发画折线图 安卓开发图表_android_02

 

本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。 收藏 评论 分享 举报

上一篇:docker pull 默认存储位置 docker pull 会覆盖原镜像吗

下一篇:Python词典输入 python怎么输入字典



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有