BarGraph.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 = 30, AXIS_LABEL_FONT_SIZE = 15;
  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. NinePatchDrawable popup = (NinePatchDrawable)this.getResources().getDrawable(popupImageID);
  82. float maxValue = 0;
  83. float padding = 7 * mContext.getResources().getDisplayMetrics().density;
  84. int selectPadding = (int) (4 * mContext.getResources().getDisplayMetrics().density);
  85. float bottomPadding = 30 * mContext.getResources().getDisplayMetrics().density;
  86. float usableHeight;
  87. if (mShowBarText) {
  88. this.mPaint.setTextSize(VALUE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  89. Rect r3 = new Rect();
  90. this.mPaint.getTextBounds("$", 0, 1, r3);
  91. usableHeight = getHeight()-bottomPadding-Math.abs(r3.top-r3.bottom)-24 * mContext.getResources().getDisplayMetrics().density;
  92. } else {
  93. usableHeight = getHeight()-bottomPadding;
  94. }
  95. // Draw x-axis line
  96. if (mShowAxis){
  97. mPaint.setColor(Color.BLACK);
  98. mPaint.setStrokeWidth(2 * mContext.getResources().getDisplayMetrics().density);
  99. mPaint.setAlpha(50);
  100. mPaint.setAntiAlias(true);
  101. canvas.drawLine(0, getHeight()-bottomPadding+10* mContext.getResources().getDisplayMetrics().density, getWidth(), getHeight()-bottomPadding+10* mContext.getResources().getDisplayMetrics().density, mPaint);
  102. }
  103. float barWidth = (getWidth() - (padding*2)*mBars.size())/mBars.size();
  104. // Maximum y value = sum of all values.
  105. for (final Bar bar : mBars) {
  106. if (bar.getValue() > maxValue) {
  107. maxValue = bar.getValue();
  108. }
  109. }
  110. mRectangle = new Rect();
  111. int count = 0;
  112. for (final Bar bar : mBars) {
  113. // Set bar bounds
  114. int left = (int)((padding*2)*count + padding + barWidth*count);
  115. int top = (int)(getHeight()-bottomPadding-(usableHeight*(bar.getValue()/maxValue)));
  116. int right = (int)((padding*2)*count + padding + barWidth*(count+1));
  117. int bottom = (int)(getHeight()-bottomPadding);
  118. mRectangle.set(left, top, right, bottom);
  119. // Draw bar
  120. this.mPaint.setColor(bar.getColor());
  121. this.mPaint.setAlpha(255);
  122. canvas.drawRect(mRectangle, this.mPaint);
  123. // Create selection region
  124. Path path = new Path();
  125. path.addRect(new RectF(mRectangle.left-selectPadding, mRectangle.top-selectPadding, mRectangle.right+selectPadding, mRectangle.bottom+selectPadding), Path.Direction.CW);
  126. bar.setPath(path);
  127. bar.setRegion(new Region(mRectangle.left-selectPadding, mRectangle.top-selectPadding, mRectangle.right+selectPadding, mRectangle.bottom+selectPadding));
  128. // Draw x-axis label text
  129. if (mShowAxis){
  130. this.mPaint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  131. int x = (int)(((mRectangle.left+mRectangle.right)/2)-(this.mPaint.measureText(bar.getName())/2));
  132. int y = (int) (getHeight()-3 * mContext.getResources().getDisplayMetrics().scaledDensity);
  133. canvas.drawText(bar.getName(), x, y, this.mPaint);
  134. }
  135. // Draw value text
  136. if (mShowBarText){
  137. this.mPaint.setTextSize(VALUE_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
  138. this.mPaint.setColor(Color.WHITE);
  139. Rect r2 = new Rect();
  140. this.mPaint.getTextBounds(bar.getValueString(), 0, 1, r2);
  141. int boundLeft = (int) (((mRectangle.left+mRectangle.right)/2)-(this.mPaint.measureText(bar.getValueString())/2)-10 * mContext.getResources().getDisplayMetrics().density);
  142. int boundTop = (int) (mRectangle.top+(r2.top-r2.bottom)-18 * mContext.getResources().getDisplayMetrics().density);
  143. int boundRight = (int)(((mRectangle.left+mRectangle.right)/2)+(this.mPaint.measureText(bar.getValueString())/2)+10 * mContext.getResources().getDisplayMetrics().density);
  144. popup.setBounds(boundLeft, boundTop, boundRight, mRectangle.top);
  145. popup.draw(canvas);
  146. 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);
  147. }
  148. if (mIndexSelected == count && mListener != null) {
  149. this.mPaint.setColor(Color.parseColor("#33B5E5"));
  150. this.mPaint.setAlpha(100);
  151. canvas.drawPath(bar.getPath(), this.mPaint);
  152. this.mPaint.setAlpha(255);
  153. }
  154. count++;
  155. }
  156. mShouldUpdate = false;
  157. }
  158. ca.drawBitmap(mFullImage, 0, 0, null);
  159. }
  160. public void setPopupImageID(int id){
  161. this.popupImageID = id;
  162. }
  163. @Override
  164. public boolean onTouchEvent(MotionEvent event) {
  165. Point point = new Point();
  166. point.x = (int) event.getX();
  167. point.y = (int) event.getY();
  168. int count = 0;
  169. for (Bar bar : mBars){
  170. Region r = new Region();
  171. r.setPath(bar.getPath(), bar.getRegion());
  172. if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
  173. mIndexSelected = count;
  174. } else if (event.getAction() == MotionEvent.ACTION_UP){
  175. if (r.contains((int)point.x,(int) point.y) && mListener != null){
  176. if (mIndexSelected > -1) mListener.onClick(mIndexSelected);
  177. mIndexSelected = -1;
  178. }
  179. }
  180. else if(event.getAction() == MotionEvent.ACTION_CANCEL)
  181. mIndexSelected = -1;
  182. count++;
  183. }
  184. if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
  185. mShouldUpdate = true;
  186. postInvalidate();
  187. }
  188. return true;
  189. }
  190. @Override
  191. protected void onDetachedFromWindow()
  192. {
  193. if(mFullImage != null)
  194. mFullImage.recycle();
  195. super.onDetachedFromWindow();
  196. }
  197. public void setOnBarClickedListener(OnBarClickedListener listener) {
  198. this.mListener = listener;
  199. }
  200. public interface OnBarClickedListener {
  201. abstract void onClick(int index);
  202. }
  203. }