BarGraph.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Created by Daniel Nadeau
  3. * daniel.nadeau01@gmail.com
  4. * danielnadeau.blogspot.com
  5. *
  6. * Licensed to the Apache Software Foundation (ASF) under one
  7. or more contributor license agreements. See the NOTICE file
  8. distributed with this work for additional information
  9. regarding copyright ownership. The ASF licenses this file
  10. to you under the Apache License, Version 2.0 (the
  11. "License"); you may not use this file except in compliance
  12. with the License. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing,
  15. software distributed under the License is distributed on an
  16. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. KIND, either express or implied. See the License for the
  18. specific language governing permissions and limitations
  19. under the License.
  20. */
  21. package com.echo.holographlibrary;
  22. import android.content.Context;
  23. import android.graphics.Bitmap;
  24. import android.graphics.Bitmap.Config;
  25. import android.graphics.Canvas;
  26. import android.graphics.Color;
  27. import android.graphics.Paint;
  28. import android.graphics.Path;
  29. import android.graphics.Point;
  30. import android.graphics.Rect;
  31. import android.graphics.RectF;
  32. import android.graphics.Region;
  33. import android.graphics.drawable.NinePatchDrawable;
  34. import android.util.AttributeSet;
  35. import android.view.MotionEvent;
  36. import android.view.View;
  37. import java.util.ArrayList;
  38. public class BarGraph extends View {
  39. private final static int VALUE_FONT_SIZE = 23, AXIS_LABEL_FONT_SIZE = 12;
  40. private ArrayList<Bar> mBars = new ArrayList<Bar>();
  41. private Paint mPaint = new Paint();
  42. private Rect mRectangle = null;
  43. private boolean mShowBarText = true;
  44. private boolean mShowAxis = true;
  45. private int mIndexSelected = -1;
  46. private OnBarClickedListener mListener;
  47. private Bitmap mFullImage;
  48. private boolean mShouldUpdate = false;
  49. private int popupImageID;
  50. private Context mContext = null;
  51. public BarGraph(Context context) {
  52. super(context);
  53. mContext = context;
  54. this.setWillNotDraw(false);
  55. }
  56. public BarGraph(Context context, AttributeSet attrs) {
  57. super(context, attrs);
  58. mContext = context;
  59. this.setWillNotDraw(false);
  60. }
  61. public void setShowBarText(boolean show){
  62. mShowBarText = show;
  63. }
  64. public void setShowAxis(boolean show){
  65. mShowAxis = show;
  66. }
  67. public void setBars(ArrayList<Bar> points){
  68. this.mBars = points;
  69. mShouldUpdate = true;
  70. //postInvalidate();
  71. }
  72. public ArrayList<Bar> getBars(){
  73. return this.mBars;
  74. }
  75. public void onDraw(Canvas ca) {
  76. super.onDraw(ca);
  77. if (mFullImage == null || mShouldUpdate) {
  78. mFullImage = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
  79. Canvas canvas = new Canvas(mFullImage);
  80. canvas.drawColor(Color.TRANSPARENT);
  81. /*
  82. Bitmap backMap = BitmapFactory.decodeResource(getResources(), this.popupImageID);
  83. backMap = backMap.copy(Bitmap.Config.ARGB_8888, true);
  84. byte[] chunk = backMap.getNinePatchChunk();
  85. NinePatchDrawable popup = new NinePatchDrawable(getResources(), backMap, chunk, new Rect(), null);
  86. popup.setBounds(0, 0, backMap.getWidth(), backMap.getHeight());
  87. */
  88. NinePatchDrawable popup = (NinePatchDrawable)this.getResources().getDrawable(this.popupImageID);
  89. float maxValue = 0;
  90. float padding = 7 * mContext.getResources().getDisplayMetrics().density;
  91. int selectPadding = (int) (4 * mContext.getResources().getDisplayMetrics().density);
  92. float bottomPadding = 30 * mContext.getResources().getDisplayMetrics().density;
  93. float usableHeight;
  94. if (mShowBarText) {
  95. this.mPaint.setTextSize(VALUE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  96. Rect r3 = new Rect();
  97. this.mPaint.getTextBounds("$", 0, 1, r3);
  98. usableHeight = getHeight()-bottomPadding-Math.abs(r3.top-r3.bottom)-24 * mContext.getResources().getDisplayMetrics().density;
  99. } else {
  100. usableHeight = getHeight()-bottomPadding;
  101. }
  102. // Draw x-axis line
  103. if (mShowAxis){
  104. mPaint.setColor(Color.BLACK);
  105. mPaint.setStrokeWidth(2 * mContext.getResources().getDisplayMetrics().density);
  106. mPaint.setAlpha(50);
  107. mPaint.setAntiAlias(true);
  108. canvas.drawLine(0, getHeight()-bottomPadding+10* mContext.getResources().getDisplayMetrics().density, getWidth(), getHeight()-bottomPadding+10* mContext.getResources().getDisplayMetrics().density, mPaint);
  109. }
  110. float barWidth = (getWidth() - (padding*2)*mBars.size())/mBars.size();
  111. // Maximum y value = sum of all values.
  112. for (final Bar bar : mBars) {
  113. if (bar.getValue() > maxValue) {
  114. maxValue = bar.getValue();
  115. }
  116. }
  117. mRectangle = new Rect();
  118. int count = 0;
  119. for (final Bar bar : mBars) {
  120. // Set bar bounds
  121. int left = (int)((padding*2)*count + padding + barWidth*count);
  122. int top = (int)(getHeight()-bottomPadding-(usableHeight*(bar.getValue()/maxValue)));
  123. int right = (int)((padding*2)*count + padding + barWidth*(count+1));
  124. int bottom = (int)(getHeight()-bottomPadding);
  125. mRectangle.set(left, top, right, bottom);
  126. // Draw bar
  127. this.mPaint.setColor(bar.getColor());
  128. this.mPaint.setAlpha(255);
  129. canvas.drawRect(mRectangle, this.mPaint);
  130. // Create selection region
  131. Path path = new Path();
  132. path.addRect(new RectF(mRectangle.left-selectPadding, mRectangle.top-selectPadding, mRectangle.right+selectPadding, mRectangle.bottom+selectPadding), Path.Direction.CW);
  133. bar.setPath(path);
  134. bar.setRegion(new Region(mRectangle.left-selectPadding, mRectangle.top-selectPadding, mRectangle.right+selectPadding, mRectangle.bottom+selectPadding));
  135. // Draw x-axis label text
  136. if (mShowAxis){
  137. this.mPaint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  138. int x = (int)(((mRectangle.left+mRectangle.right)/2)-(this.mPaint.measureText(bar.getName())/2));
  139. int y = (int) (getHeight()-3 * mContext.getResources().getDisplayMetrics().scaledDensity);
  140. canvas.drawText(bar.getName(), x, y, this.mPaint);
  141. }
  142. // Draw value text
  143. if (mShowBarText){
  144. this.mPaint.setTextSize(VALUE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  145. this.mPaint.setColor(Color.WHITE);
  146. Rect r2 = new Rect();
  147. this.mPaint.getTextBounds(bar.getValueString(), 0, 1, r2);
  148. int boundLeft = (int) (((mRectangle.left+mRectangle.right)/2)-(this.mPaint.measureText(bar.getValueString())/2)-5 * mContext.getResources().getDisplayMetrics().density);
  149. int boundTop = (int) (mRectangle.top+(r2.top-r2.bottom)-18 * mContext.getResources().getDisplayMetrics().density);
  150. int boundRight = (int)(((mRectangle.left+mRectangle.right)/2)+(this.mPaint.measureText(bar.getValueString())/2)+5 * mContext.getResources().getDisplayMetrics().density);
  151. popup.setBounds(boundLeft, boundTop, boundRight, mRectangle.top);
  152. popup.draw(canvas);
  153. canvas.drawText(bar.getValueString(), (int)(((mRectangle.left+mRectangle.right)/2)-(this.mPaint.measureText(bar.getValueString()))/2), mRectangle.top-(mRectangle.top - boundTop)/2f+(float)Math.abs(r2.top-r2.bottom)/2f*0.7f, this.mPaint);
  154. }
  155. if (mIndexSelected == count && mListener != null) {
  156. this.mPaint.setColor(Color.parseColor("#33B5E5"));
  157. this.mPaint.setAlpha(100);
  158. canvas.drawPath(bar.getPath(), this.mPaint);
  159. this.mPaint.setAlpha(255);
  160. }
  161. count++;
  162. }
  163. mShouldUpdate = false;
  164. }
  165. ca.drawBitmap(mFullImage, 0, 0, null);
  166. }
  167. public void setPopupImageID(int id){
  168. this.popupImageID = id;
  169. }
  170. @Override
  171. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  172. super.onLayout(changed, l, t, r, b);
  173. if(mFullImage != null)
  174. mFullImage = null;
  175. postInvalidate();
  176. }
  177. @Override
  178. public boolean onTouchEvent(MotionEvent event) {
  179. Point point = new Point();
  180. point.x = (int) event.getX();
  181. point.y = (int) event.getY();
  182. int count = 0;
  183. for (Bar bar : mBars){
  184. Region r = new Region();
  185. r.setPath(bar.getPath(), bar.getRegion());
  186. if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
  187. mIndexSelected = count;
  188. } else if (event.getAction() == MotionEvent.ACTION_UP){
  189. if (r.contains((int)point.x,(int) point.y) && mListener != null){
  190. if (mIndexSelected > -1) mListener.onClick(mIndexSelected);
  191. mIndexSelected = -1;
  192. }
  193. }
  194. else if(event.getAction() == MotionEvent.ACTION_CANCEL)
  195. mIndexSelected = -1;
  196. count++;
  197. }
  198. if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
  199. mShouldUpdate = true;
  200. postInvalidate();
  201. }
  202. return true;
  203. }
  204. @Override
  205. protected void onDetachedFromWindow()
  206. {
  207. if(mFullImage != null)
  208. mFullImage.recycle();
  209. super.onDetachedFromWindow();
  210. postInvalidate();
  211. }
  212. public void setOnBarClickedListener(OnBarClickedListener listener) {
  213. this.mListener = listener;
  214. }
  215. public interface OnBarClickedListener {
  216. abstract void onClick(int index);
  217. }
  218. }