網(wǎng)站首頁 編程語言 正文
概述
效果圖,數(shù)據(jù)可平移查看歷史
build.gradle
implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'
layout
直接在布局中加入相應(yīng)的圖表控件
<lecho.lib.hellocharts.view.LineChartView android:id="@+id/ChartLine" android:layout_width="0dp" android:layout_height="300dp" android:padding="6dp"/>
常用圖表類型
LineChartView
PointValue
圖表上每一點的數(shù)據(jù) PointValue(float x, float y)
第一個參數(shù)表示點的位置,第二個參數(shù)表示點上的數(shù)據(jù)。如果是多條折線,則需要為每一條拆線相同位置定義不同的PointValue
PointValue point1 = new PointValue(0, 1000); PointValue point2 = new PointValue(1, 1500); PointValue point1 = new PointValue(0, 1200); PointValue point2 = new PointValue(1, 1600);
setLabel() 可以為數(shù)據(jù)點定義顯示文本
point1.setLabel("202101");
實例
List<PointValue> valuesR = new ArrayList<>(); List<PointValue> valuesS = new ArrayList<>(); for (int i = 0; i < dataEntities.size(); i++) { DataEntity dataEntity = dataEntities.get(i); if (dataEntity.getDataR() < 3000 || dataEntity.getDataS() < 3000) continue; valuesR.add(new PointValue(i, dataEntity.getDataR()).setLabel(String.valueOf(dataEntity.getDataR()))); valuesS .add(new PointValue(i, dataEntity.getDataS()).setLabel(String.valueOf(dataEntity.getDataS()))); }
如果數(shù)據(jù)太小,則跳過。數(shù)據(jù)不加載入數(shù)據(jù)點集合,但 i 索引會增加,折線上會出現(xiàn)大的間隔。如果不想出現(xiàn)此情況,可更換 for 循環(huán)為 foreach 循環(huán),然后手動控制 i 的增長。
Line
定義線條上的數(shù)據(jù)和顏色等
構(gòu)造函數(shù)傳入包含PointValue的列表,有幾條折線就創(chuàng)建幾條并綁定不同的點數(shù)據(jù)列表
List<Line> lines = new ArrayList<>(); lines.add(new Line(valuesR).setColor(Color.RED).setCubic(false).setHasLabelsOnlyForSelected(true)); lines.add(new Line(valuesS).setColor(Color.BLUE).setCubic(false).setHasLabelsOnlyForSelected(true));
常用屬性方法
- setColor() # 設(shè)置折線顏色
- setCubic(true) # 折線是否平滑,即是曲線還是折線
- setShape() # 折線上數(shù)據(jù)點的形狀,有三種 :ValueShape.SQUARE(方) ValueShape.CIRCLE(圓) ValueShape.DIAMOND(菱)
- setFilled(false) # 是否填充曲線的面積
- setHasLabels(true) # 曲線的數(shù)據(jù)坐標是否加上備注
- setHasLabelsOnlyForSelected(true) # 同上,但是只有點擊數(shù)據(jù)坐標提示數(shù)據(jù),但設(shè)置了這個屬性,上面的屬性即失效
- setHasLines(true) # 是否顯示線,為false則只顯示點
- setHasPoints(true) # 是否顯示圓點,如果為false則每個數(shù)據(jù)點都是個大的圓點
Axis
X軸
List<AxisValue> xValue = new ArrayList<>(); for(int i = 0; i < dataEntities.size(); i++){ DataEntity dataEntity = dataEntities.get(i); xValue.add(new AxisValue(i).setLabel(dataEntity .getDate())); } Axis axisX = new Axis(); axisX.setHasTiltedLabels(true); axisX.setTextColor(Color.GRAY); axisX.setValues(xValue);
Y軸
List<AxisValue> yValue = new ArrayList<>(); for(int i = 0; i < 8000; i = i + 1000){ yValue.add(new AxisValue(i).setLabel(String.valueOf(i))); } Axis axisY = new Axis(); axisY.setTextColor(Color.GRAY); axisY.setValues(yValue); axisY.setHasLines(true);
LineChartData data = new LineChartData(); data.setLines(lines); data.setAxisXBottom(axisX); data.setAxisYLeft(axisY);
AxisValue
創(chuàng)建實例
AxisValue v = new AxisValue(i).setLabel(String.valueOf(i));
常用屬性方法
- setHasTiltedLabels(true) # 坐標軸字體是否顯示斜體
- setValues() # 填充數(shù)據(jù)列表
- setHasLines(true) # 是否顯示參考線
LineChartData
將以上包含了點數(shù)據(jù)列表的折線添加到 LineChartData 中并綁定給圖表控件
Chart
//設(shè)置是否允許平移以及平移的方向 bindingView.chartLine.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL); //填充數(shù)據(jù) bindingView.chartLine.setLineChartData(data);
實現(xiàn)平移必須設(shè)置視圖大小
//實例化一個新的ViewPort 構(gòu)造函數(shù)中傳入填充數(shù)據(jù)后的默認最大視圖 Viewport viewport = new Viewport(bindingView.chartLine.getMaximumViewport()); //設(shè)置y軸的顯示 viewport.top = MyApplication.getSharedPreferences().getInt("max", 8000); viewport.bottom = MyApplication.getSharedPreferences().getInt("min", 3000); //一定要先設(shè)置最大視圖范圍 bindingView.chartLine.setMaximumViewport(viewport); //圖表最大視圖 //設(shè)置x軸的顯示 viewport.right = MyApplication.getSharedPreferences().getInt("chart_right", 7); viewport.left = 0; //最后設(shè)置當前顯示的視圖范圍 bindingView.chartLine.setCurrentViewport(viewport); //圖表當前視圖
完整代碼
private void initChart(List<DataEntity> dataEntities) { List<PointValue> valuesR = new ArrayList<>(); List<PointValue> valuesS = new ArrayList<>(); List<AxisValue> xValue = new ArrayList<>(); List<AxisValue> yValue = new ArrayList<>(); for (int i = 0; i < dataEntities.size(); i++) { DataEntity dataEntity = dataEntities.get(i); if (dataEntity.getDataR() < MyApplication.getSharedPreferences().getInt("min", 3000) || dataEntity.getDataS() < MyApplication.getSharedPreferences().getInt("min", 3000)) continue; valuesR.add(new PointValue(i, dataEntity .getDataR()).setLabel(String.valueOf(dataEntity .getDataR()))); valuesS.add(new PointValue(i, dataEntity .getDataS()).setLabel(String.valueOf(dataEntity .getDataS()))); xValue.add(new AxisValue(i).setLabel(dataEntity .getDate())); } for (int i = 0; i < MyApplication.getSharedPreferences().getInt("salary_max", 8000); i = i + 500) { yValue.add(new AxisValue(i).setLabel(String.valueOf(i))); } List<Line> lines = new ArrayList<>(); lines.add(new Line(valuesR).setColor(Color.RED).setCubic(false).setHasLabelsOnlyForSelected(true)); lines.add(new Line(valuesS).setColor(Color.BLUE).setCubic(false).setHasLabelsOnlyForSelected(true)); Axis axisX = new Axis(); axisX.setHasTiltedLabels(true); axisX.setTextColor(Color.GRAY); axisX.setValues(xValue); Axis axisY = new Axis(); axisY.setTextColor(Color.GRAY); axisY.setValues(yValue); axisY.setHasLines(true); LineChartData data = new LineChartData(); data.setLines(lines); data.setAxisXBottom(axisX); data.setAxisYLeft(axisY); bindingView.chartLine.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL); bindingView.chartLine.setLineChartData(data); //設(shè)置圖表顯示的大小 Viewport viewport = new Viewport(bindingView.chartLine.getMaximumViewport()); //填充數(shù)據(jù)后的默認最大視圖 viewport.top = MyApplication.getSharedPreferences().getInt("max", 8000); viewport.bottom = MyApplication.getSharedPreferences().getInt("min", 3000); bindingView.chartLine.setMaximumViewport(viewport); //圖表最大視圖 viewport.right = MyApplication.getSharedPreferences().getInt("chart_right", 7); viewport.left = 0; bindingView.chartLine.setCurrentViewport(viewport); //圖表當前視圖 }
原文鏈接:https://blog.csdn.net/ymtianyu/article/details/122301682
相關(guān)推薦
- 2022-01-03 當前時間與新年倒計時
- 2022-09-26 你了解Redis事務(wù)嗎_Redis
- 2022-10-08 C#中LINQ的Select與SelectMany函數(shù)使用_C#教程
- 2023-06-13 C++?ncnn模型驗證精度實現(xiàn)代碼_C 語言
- 2022-05-03 Docker?Desktop啟動失敗的解決(Docker?failed?to?initialize?
- 2022-10-30 C語言struct結(jié)構(gòu)體介紹_C 語言
- 2022-07-21 HCIP---BGP ---邊界網(wǎng)關(guān)協(xié)議
- 2022-09-23 Pandas多列值合并成一列的實現(xiàn)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支