android柱状图第三方,Android 优秀图标库MPAndroidChart之柱状图(适应百分之八十项目需求)...

前言

在项目当中很多时候要对数据进行分析就要用到图表,在gitHub上有很多优秀的图表开源库,今天给大家分享的就是MPAndroidChart中的柱状图。简单介绍一下MPAndroidChart:他可以实现图表的拖动,3D,局部查看,数据动态展示等功能。

废话就不多说了,先给看大家看看效果图

format,png

format,png

format,png

操作步骤

第一步:需要将依赖的库添加到你的项目中implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'

implementation 'com.google.android.material:material:1.0.0'

第二步:xml中

android:id="@+id/chart1"

android:layout_width="match_parent"

android:layout_height="300dp"

/>

第三步:ValueFormatter.java/**

* Class to format all values before they are drawn as labels.

*/

public abstract class ValueFormatter implements IAxisValueFormatter, IValueFormatter {/**

DO NOT USE, only for backwards compatibility and will be removed in future versions.

*

* @param value the value to be formatted

* @param axis  the axis the value belongs to

* @return formatted string label

*/@Override@Deprecatedpublic String getFormattedValue(float value, AxisBase axis) {    return getFormattedValue(value);

}/**

DO NOT USE, only for backwards compatibility and will be removed in future versions.

* @param value           the value to be formatted

* @param entry           the entry the value belongs to - in e.g. BarChart, this is of class BarEntry

* @param dataSetIndex    the index of the DataSet the entry in focus belongs to

* @param viewPortHandler provides information about the current chart state (scale, translation, ...)

* @return formatted string label

*/@Override@Deprecatedpublic String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {    return getFormattedValue(value);

}/**

* Called when drawing any label, used to change numbers into formatted strings.

*

* @param value float to be formatted

* @return formatted string label

*/public String getFormattedValue(float value) {    return String.valueOf(value);

}/**

* Used to draw axis labels, calls {@link #getFormattedValue(float)} by default.

*

* @param value float to be formatted

* @param axis  axis being labeled

* @return formatted string label

*/public String getAxisLabel(float value, AxisBase axis) {    return getFormattedValue(value);

}/**

* Used to draw bar labels, calls {@link #getFormattedValue(float)} by default.

*

* @param barEntry bar being labeled

* @return formatted string label

*/public String getBarLabel(BarEntry barEntry) {    return getFormattedValue(barEntry.getY());

}/**

* Used to draw stacked bar labels, calls {@link #getFormattedValue(float)} by default.

*

* @param value        current value to be formatted

* @param stackedEntry stacked entry being labeled, contains all Y values

* @return formatted string label

*/public String getBarStackedLabel(float value, BarEntry stackedEntry) {    return getFormattedValue(value);

}/**

* Used to draw line and scatter labels, calls {@link #getFormattedValue(float)} by default.

*

* @param entry point being labeled, contains X value

* @return formatted string label

*/public String getPointLabel(Entry entry) {    return getFormattedValue(entry.getY());

}/**

* Used to draw pie value labels, calls {@link #getFormattedValue(float)} by default.

*

* @param value    float to be formatted, may have been converted to percentage

* @param pieEntry slice being labeled, contains original, non-percentage Y value

* @return formatted string label

*/public String getPieLabel(float value, PieEntry pieEntry) {    return getFormattedValue(value);

}/**

* Used to draw radar value labels, calls {@link #getFormattedValue(float)} by default.

*

* @param radarEntry entry being labeled

* @return formatted string label

*/public String getRadarLabel(RadarEntry radarEntry) {    return getFormattedValue(radarEntry.getY());

}/**

* Used to draw bubble size labels, calls {@link #getFormattedValue(float)} by default.

*

* @param bubbleEntry bubble being labeled, also contains X and Y values

* @return formatted string label

*/public String getBubbleLabel(BubbleEntry bubbleEntry) {    return getFormattedValue(bubbleEntry.getSize());

}/**

* Used to draw high labels, calls {@link #getFormattedValue(float)} by default.

*

* @param candleEntry candlestick being labeled

* @return formatted string label

*/public String getCandleLabel(CandleEntry candleEntry) {    return getFormattedValue(candleEntry.getHigh());

}

}

第四步:MyValueFormatterpublic class MyValueFormatter extends ValueFormatter{private final DecimalFormat mFormat;private String suffix;public MyValueFormatter(String suffix) {

mFormat = new DecimalFormat("0000");    this.suffix = suffix;

}@Overridepublic String getFormattedValue(float value) {    return mFormat.format(value) + suffix;

}@Overridepublic String getAxisLabel(float value, AxisBase axis) {    if (axis instanceof XAxis) {        return mFormat.format(value);

} else if (value > 0) {        return mFormat.format(value) + suffix;

} else {        return mFormat.format(value);

}

}

}

复制代码

第五步:MainAcyivitypackage detongs.hbqianze.him.linechart;  import android.os.Bundle;  import android.util.Log;  import android.view.WindowManager;  import android.widget.TextView;  import androidx.appcompat.app.AppCompatActivity;  import com.github.mikephil.charting.charts.BarChart;  import com.github.mikephil.charting.components.XAxis;  import com.github.mikephil.charting.components.YAxis;  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.interfaces.datasets.IBarDataSet;  import  com.github.mikephil.charting.interfaces.datasets.IDataSet;  import com.github.mikephil.charting.utils.ColorTemplate;  import java.util.ArrayList;  import detongs.hbqianze.him.linechart.chart.MyValueFormatter;  import detongs.hbqianze.him.linechart.chart.ValueFormatter;  public class MainActivity extends AppCompatActivity {private BarChart chart;private TextView te_cache;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);

chart = findViewById(R.id.chart1);

te_cache = findViewById(R.id.te_cache);

chart.getDescription().setEnabled(false);    //设置最大值条目,超出之后不会有值

chart.setMaxVisibleValueCount(60);    //分别在x轴和y轴上进行缩放

chart.setPinchZoom(true);    //设置剩余统计图的阴影

chart.setDrawBarShadow(false);    //设置网格布局

chart.setDrawGridBackground(true);    //通过自定义一个x轴标签来实现2,015 有分割符符bug

ValueFormatter custom = new MyValueFormatter(" ");    //获取x轴线

XAxis xAxis = chart.getXAxis();    //设置x轴的显示位置

xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);    //设置网格布局

xAxis.setDrawGridLines(true);    //图表将避免第一个和最后一个标签条目被减掉在图表或屏幕的边缘

xAxis.setAvoidFirstLastClipping(false);    //绘制标签  指x轴上的对应数值 默认true

xAxis.setDrawLabels(true);

xAxis.setValueFormatter(custom);    //缩放后x 轴数据重叠问题

xAxis.setGranularityEnabled(true);    //获取右边y标签

YAxis axisRight = chart.getAxisRight();

axisRight.setStartAtZero(true);    //获取左边y轴的标签

YAxis axisLeft = chart.getAxisLeft();    //设置Y轴数值 从零开始

axisLeft.setStartAtZero(true);

chart.getAxisLeft().setDrawGridLines(false);    //设置动画时间

chart.animateXY(600,600);

chart.getLegend().setEnabled(true);

getData();    //设置柱形统计图上的值

chart.getData().setValueTextSize(10);    for (IDataSet set : chart.getData().getDataSets()){

set.setDrawValues(!set.isDrawValuesEnabled());

}

}public void getData(){

ArrayList values = new ArrayList<>();

Float aFloat = Float.valueOf("2015");

Log.v("xue","aFloat+++++"+aFloat);

BarEntry barEntry = new BarEntry(aFloat,Float.valueOf("100"));

BarEntry barEntry1 = new BarEntry(Float.valueOf("2016"),Float.valueOf("210"));

BarEntry barEntry2 = new BarEntry(Float.valueOf("2017"),Float.valueOf("300"));

BarEntry barEntry3 = new BarEntry(Float.valueOf("2018"),Float.valueOf("450"));

BarEntry barEntry4 = new BarEntry(Float.valueOf("2019"),Float.valueOf("300"));

BarEntry barEntry5 = new BarEntry(Float.valueOf("2020"),Float.valueOf("650"));

BarEntry barEntry6 = new BarEntry(Float.valueOf("2021"),Float.valueOf("740"));

values.add(barEntry);

values.add(barEntry1);

values.add(barEntry2);

values.add(barEntry3);

values.add(barEntry4);

values.add(barEntry5);

values.add(barEntry6);

BarDataSet set1;if (chart.getData() != null &&

chart.getData().getDataSetCount() > 0) {

set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);

set1.setValues(values);

chart.getData().notifyDataChanged();

chart.notifyDataSetChanged();

} else {

set1 = new BarDataSet(values, "点折水");

set1.setColors(ColorTemplate.VORDIPLOM_COLORS);

set1.setDrawValues(false);

ArrayList dataSets = new ArrayList<>();

dataSets.add(set1);

BarData data = new BarData(dataSets);

chart.setData(data);

chart.setFitBars(true);

}    //绘制图表chart.invalidate();

}

}最后

文章写到这里就结束了,如果你觉得文章写得不错就给个关注呗?你的支持是我最大的动力!如果还有想了解MPAndroidChart其他属性的小伙伴也可以给我留言。

format,png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值