Android學習筆記-解決hellocharts折線圖由於特殊資料不能展示的問題
前兩天修改一個專案的BUG:
使用hellocharts展示了一個餅圖,點選餅圖的一項再用折線圖展示餅圖中每項具體資料。
發現問題:餅圖中現有資料,點選之後折線圖不能展示資料。
通過檢視資料來源發現:不能展示圖片的情況有兩種:
1. 折線圖所有資料一樣:例:{7,7,7}
2. 折線圖只有一個數據,例:{10}
通過debug除錯發現:在計算RawX,RawY會出現pixelOffset為NaN的問題。
/**
* Translates chart value into raw pixel value. Returned value is absolute pixel X coordinate. If this method
* return
* 0 that means left most pixel of the screen.
*/
public float computeRawX(float valueX) {
// TODO: (contentRectMinusAllMargins.width() / currentViewport.width()) can be recalculated only when viewport
// change.
final float pixelOffset = (valueX - currentViewport.left) * (contentRectMinusAllMargins.width() /
currentViewport.width());
return contentRectMinusAllMargins.left + pixelOffset;
}
/**
* Translates chart value into raw pixel value. Returned value is absolute pixel Y coordinate. If this method
* return
* 0 that means top most pixel of the screen.
*/
public float computeRawY(float valueY) {
final float pixelOffset = (valueY - currentViewport.bottom) * (contentRectMinusAllMargins.height() /
currentViewport.height());
return contentRectMinusAllMargins.bottom - pixelOffset;
}
先看如何讓解決問題2的:
當所有點都一樣的時候:computeRawY會為NaN,原因是:
private void calculateMaxViewport() {
tempMaximumViewport.set(Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MAX_VALUE);
LineChartData data = dataProvider.getLineChartData();
for (Line line : data.getLines()) {
// Calculate max and min for viewport.
for (PointValue pointValue : line.getValues()) {
if (pointValue.getX() < tempMaximumViewport.left) {
tempMaximumViewport.left = pointValue.getX();
}
if (pointValue.getX() > tempMaximumViewport.right) {
tempMaximumViewport.right = pointValue.getX();
}
if (pointValue.getY() < tempMaximumViewport.bottom) {
tempMaximumViewport.bottom = pointValue.getY();
}
if (pointValue.getY() > tempMaximumViewport.top) {
tempMaximumViewport.top = pointValue.getY();
}
}
}
}
這個方法計算Y軸最大值最小值,當所有資料一樣,計算出的bottom和top相等,導致
computeRawY中currentViewport.height()獲取的值為0;所以修改如下:
private void calculateMaxViewport() {
tempMaximumViewport.set(Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MAX_VALUE);
LineChartData data = dataProvider.getLineChartData();
for (Line line : data.getLines()) {
// Calculate max and min for viewport.
for (PointValue pointValue : line.getValues()) {
if (pointValue.getX() < tempMaximumViewport.left) {
tempMaximumViewport.left = pointValue.getX();
}
if (pointValue.getX() > tempMaximumViewport.right) {
tempMaximumViewport.right = pointValue.getX();
}
if (pointValue.getY() < tempMaximumViewport.bottom) {
tempMaximumViewport.bottom = pointValue.getY();
}
if (pointValue.getY() > tempMaximumViewport.top) {
tempMaximumViewport.top = pointValue.getY();
}
}
}
if (tempMaximumViewport.top == tempMaximumViewport.bottom) {//解決最大值最小值相等時,圖不能展示問題
tempMaximumViewport.top = tempMaximumViewport.top * 2;
tempMaximumViewport.bottom = 0;
}
}
這樣所有值都相等,可以在圖中間畫出一條直線。
再看問題一:
由於只有一個點,computeRawX中的currentViewport.width()為初始值,即min,這樣computeRawX中的currentViewport算出來同樣為NaN,修改程式碼如下:
public float computeRawX(float valueX) {
// TODO: (contentRectMinusAllMargins.width() / currentViewport.width()) can be recalculated only when viewport
// change.
final float pixelOffset = (valueX - currentViewport.left) * (contentRectMinusAllMargins.width() /
currentViewport.width());
if (Float.isNaN(pixelOffset)||Float.isInfinite(pixelOffset)) {
return contentRectMinusAllMargins.left + 0;
}
return contentRectMinusAllMargins.left + pixelOffset;
}
判斷當算出的pixelOffset為NaN的時候直接返回contentRectMinusAllMargins.left。
好了以上兩處修改就能解決折線圖由於特殊資料(一條資料/資料相同)時不能展示的問題。
看上去修改的程式碼很簡單,其實這裡主要是原始碼的閱讀和程式碼的跟蹤。如何找到這兩處還是花費了不少時間和精力的。
所以記下來,以供同樣出現此問題的朋友參考。
相關推薦
Android學習筆記-解決hellocharts折線圖由於特殊資料不能展示的問題
前兩天修改一個專案的BUG: 使用hellocharts展示了一個餅圖,點選餅圖的一項再用折線圖展示餅圖中每項具體資料。 發現問題:餅圖中現有資料,點選之後折線圖不能展示資料。 通過檢視資料來源發現:不能展示圖片的情況有兩種: 1. 折線圖所有資料一樣:
Android學習筆記之Bitmap點陣圖雖觸控點移動
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Sigmaplot學習筆記2——製作折線圖
製作折線圖 繼續使用之前的資料,基本步驟一致 完成 這時候就有一個疑問了,如何修改X軸的刻度呢 在預設情況下,x軸的刻度間隔是2,雙擊x軸標籤,進入Graph Properties,點選Major Ticks 可以看到最下面的Tick inte
Android學習筆記-ImageView(圖像視圖)
尺寸 map 顯示 htm 通過 加載 內容 github bit 原文來自:http://www.runoob.com/w3cnote/android-tutorial-imageview.html 本節引言: 本節介紹的UI基礎控件是:ImageView(圖像視圖)
Pro Android學習筆記 ActionBar(1):Home圖標區
ces tom 新的 方便 find rac vertica lba manifest ?? Pro Android學習筆記(四八):ActionBar(1):Home圖標區 2013年03月10日 ? 綜合 ? 共 3256字 ? 字號 小 中 大 ? 評論關閉
Android學習筆記之解決螢幕旋轉後Activity重建問題
0.只需在activity類下重寫onSaveInstanceState方法,下面是一個重寫的例項,目的是儲存oncreate()方法中的臨時變數 @Override protected void onSaveInstanceState(Bundle outState)
Android學習筆記--GMS認證中常見的fail項及解決方法
############################################################# cts測試的一些命令: sudo chmod a+x copy_media.sh ./copy_media.sh all ad
Android學習筆記 ----啟動 Theme.Dialog 主題的Activity時程式崩潰的解決辦法
新建了一個Android Studio工程,在MainActivity的主介面中添加了兩個按鈕,點選其中一個按鈕用來啟動 NormalActivity,點選另一按鈕用來啟動DialogActivity. 其中,NormalActivity和DialogActivity都是很簡單的介面,只是在AndroidMa
Pro Android學習筆記(一三七):Home Screen Widgets(3):配置Activity
map onclick widgets info xtra ces extends height appwidget 文章轉載僅僅能用於非商業性質,且不能帶有虛擬貨幣、積分、註冊等附加條件。轉載須註明出處http://blog.csdn.net/flowingfly
Android學習筆記-TextView(文本框)(二)
com ddc tel spanned extra pac 全部 con 平時 文章參考自:http://www.runoob.com/w3cnote/android-tutorial-textview.html 2.4 使用autoLink屬性識別鏈接類型 當文字中出
Android學習筆記-繪制圓形ImageView實例
eight font private cte class get wid actor oid 現在很多的APP都很喜歡圓形的頭像,這裏就簡單的寫個圓形的ImageView~ 第三方圓形ImageView控件: RoundedImageView CircleImageView
學習筆記TF015:加載圖像、圖像格式、圖像操作、顏色
修改 感知 存在 邊界 apt 預處理 所有 images 其中 TensorFlow支持JPG、PNG圖像格式,RGB、RGBA顏色空間。圖像用與圖像尺寸相同(height*width*chnanel)張量表示。通道表示為包含每個通道顏色數量標量秩1張量。圖像所有像素存在
udacity android 學習筆記: lesson 4 part a
odi todo col 數據庫版本 pretty define all 大致 lec udacity android 學習筆記: lesson 4 part a 作者:幹貨店打雜的 /titer1 /Archimedes 出處:https://
Android學習筆記:超能RecyclerView組件使用總結
popu bin view設置 and col cas mda rac data 個人認為 RecyclerView組件確實值得學習並用到我們的項目中去,前面學了相關的內容。今天再補充一些相關的東東。 1,實現對RecyclerView中的數據進行加入和刪除操作。
Android 開發筆記___滾動視圖__scroll view
protected apk sch view col extend linear scroll set 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="h
Android 學習筆記--9妹圖片+代碼混淆+打包
文件名 打包 andro span apk 使用 背景透明 學習筆記 tro 一、Android Studio 創建.9文件 註:找一張背景透明的png圖片,將文件名改成 " xxxx.9.png " 然後放到drawable文件中,雙擊 註:有一種
Android學習筆記(36):Android的兩種事件處理方式
post gravity cal log 基於 處理方法 hang mil 重寫 Android提供了兩種事件處理的方式:基於回調的事件處理 和 基於監聽的事件處理。 我們來說的easy理解一點: (1)基於回調的事件處理就是繼承GUI組件,並重寫該組件的
android學習筆記之ImageView的scaleType屬性
有關 sni mage nds 目標 big ins 分辨率 處理 我們知道,ImageView有一個屬性叫做scaleType,它的取值一共同擁有八種,各自是:matrix,fitXY。fitStart,fitCenter。fitEnd,center,centerCr
Android學習筆記---使用adb進行root時提示devices offline
ces lin sdn adb blog 筆記 重復 root net 今天下午的時候莫名其妙的adb root就連接不上了,提示devices offline這個錯誤,重啟了幾次設備,還是不行 最後利用百度在 http://blog.csdn.net/BruceHurri
Android學習筆記三:用Intent串聯activity
conda data activity setresult result 意圖 prot 其他 cte 一:Intent Intent可以理解為 意圖。 我們可以通過創建intent實例來定義一個跳轉意圖,意圖包括:要跳轉到哪個頁面、需要傳遞什麽