版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android優(yōu)秀圖標(biāo)庫MPAndroidChart之柱狀圖(適應(yīng)百分之八十項(xiàng)目需求)
前言在項(xiàng)目當(dāng)中很多時(shí)候要對數(shù)據(jù)進(jìn)行分析就要用到圖表,在gitHub上有很多優(yōu)秀的圖表開源庫,今天給大家分享的就是MPAndroidChart中的柱狀圖。簡單介紹一下MPAndroidChart:他可以實(shí)現(xiàn)圖表的拖動(dòng),3D,局部查看,數(shù)據(jù)動(dòng)態(tài)展示等功能。官方源碼地址:/PhilJay/MPA…廢話就不多說了,先給看大家看看效果圖操作步驟第一步:需要將依賴的庫添加到你的項(xiàng)目中implementation'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'implementation'com.google.android.material:material:1.0.0'implementation'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'implementation'com.google.android.material:material:1.0.0'第二步:xml中
<com.github.mikephil.charting.charts.BarChart
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
{/**
*
<b>DO
NOT
USE</b>,
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);
}/**
*
<b>DO
NOT
USE</b>,
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());
}
}第四步:MyValueFormatter
public
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);
}
}
}
復(fù)制代碼第五步:MainAcyivity
package
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
ponents.XAxis;
import
ponents.YAxis;
import
com.github.mikephil.charting.data.BarData;
import
com.github.mikephil.charting.data.BarDataSet;
import
com.github.mikephil.charting.data.BarEntry;
import
erfaces.datasets.IBarDataSet;
import
erfaces.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);
//設(shè)置最大值條目,超出之后不會(huì)有值
chart.setMaxVisibleValueCount(60);
//分別在x軸和y軸上進(jìn)行縮放
chart.setPinchZoom(true);
//設(shè)置剩余統(tǒng)計(jì)圖的陰影
chart.setDrawBarShadow(false);
//設(shè)置網(wǎng)格布局
chart.setDrawGridBackground(true);
//通過自定義一個(gè)x軸標(biāo)簽來實(shí)現(xiàn)2,015
有分割符符bug
ValueFormatter
custom
=
new
MyValueFormatter("
");
//獲取x軸線
XAxis
xAxis
=
chart.getXAxis();
//設(shè)置x軸的顯示位置
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//設(shè)置網(wǎng)格布局
xAxis.setDrawGridLines(true);
//圖表將避免第一個(gè)和最后一個(gè)標(biāo)簽條目被減掉在圖表或屏幕的邊緣
xAxis.setAvoidFirstLastClipping(false);
//繪制標(biāo)簽
指x軸上的對應(yīng)數(shù)值
默認(rèn)true
xAxis.setDrawLabels(true);
xAxis.setValueFormatter(custom);
//縮放后x
軸數(shù)據(jù)重疊問題
xAxis.setGranularityEnabled(true);
//獲取右邊y標(biāo)簽
YAxis
axisRight
=
chart.getAxisRight();
axisRight.setStartAtZero(true);
//獲取左邊y軸的標(biāo)簽
YAxis
axisLeft
=
chart.getAxisLeft();
//設(shè)置Y軸數(shù)值
從零開始
axisLeft.setStartAtZero(true);
chart.getAxisLeft().setDrawGridLines(false);
//設(shè)置動(dòng)畫時(shí)間
chart.animateXY(600,600);
chart.getLegend().setEnabled(true);
getData();
//設(shè)置柱形統(tǒng)計(jì)圖上的值
chart.getData().setValueTextSize(10);
for
(IDataSet
set
:
chart.getData().getDataSets()){
set.setDrawValues(!set.isDrawValuesEnabled());
}
}public
void
getData(){
ArrayList<BarEntry>
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
bar
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- √lwj-企業(yè)如何做好人才管理工作?-1.001H-2024.8.1
- 專項(xiàng)素養(yǎng)綜合全練(一) 聲速的測量與應(yīng)用教學(xué)設(shè)計(jì)2024-2025學(xué)年蘇科版(2024)物理八年級上冊
- 7 《靜夜思》教學(xué)設(shè)計(jì)-2023-2024學(xué)年語文一年級下冊統(tǒng)編版
- 3-2-2與山為鄰(山地之國) 教案
- 【核心素養(yǎng)】人音版四年級上冊第3課《乒乓變奏曲》教案
- 八年級歷史下冊 第三學(xué)習(xí)主題 建設(shè)中國特色社會(huì)主義 第12課 沿著中國特色社會(huì)主義道路前進(jìn)教學(xué)設(shè)計(jì)1 川教版
- 蛋品加工未來發(fā)展建議
- 小學(xué)課后服務(wù)口才訓(xùn)練:基礎(chǔ)篇第10課(教學(xué)設(shè)計(jì))
- 山東省郯城縣郯城街道初級中學(xué)初中信息技術(shù) 運(yùn)動(dòng)補(bǔ)間動(dòng)畫1教案
- 2 周末巧安排 教學(xué)設(shè)計(jì)-2024-2025學(xué)年道德與法治二年級上冊(統(tǒng)編版)
- 2024至2030年廣西建筑行業(yè)市場調(diào)研分析及投資風(fēng)險(xiǎn)研究預(yù)測報(bào)告
- 網(wǎng)約車聯(lián)合經(jīng)營協(xié)議書范本
- 安徽民航機(jī)場集團(tuán)有限公司招聘筆試真題2023
- 2024至2030年中國洗浴服務(wù)行業(yè)市場競爭態(tài)勢及發(fā)展戰(zhàn)略研究預(yù)測報(bào)告
- 2.5動(dòng)蕩變化中的春秋時(shí)期 課件 2024-2025學(xué)年統(tǒng)編版七年級歷史上冊
- 人教版(2024)七年級地理上冊3.2《世界的地形》精美課件
- 2025屆安徽省部分學(xué)校高三摸底考試(一模)語文試題(解析版)
- 2024年公務(wù)員300道公務(wù)員面試試題及答案
- DB61-T 1839-2024 中華蜜蜂囊狀幼蟲病防治技術(shù)規(guī)范
- 6~10的認(rèn)識和加減法(教學(xué)設(shè)計(jì))-2023-2024學(xué)年一年級上冊數(shù)學(xué)人教版
- 八年級物理上冊 第二章 聲現(xiàn)象 第2節(jié) 聲音的特性說課稿 (新版)新人教版
評論
0/150
提交評論