LineGraph.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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.Path.Direction;
  30. import android.graphics.Point;
  31. import android.graphics.PorterDuffXfermode;
  32. import android.graphics.Region;
  33. import android.util.AttributeSet;
  34. import android.util.TypedValue;
  35. import android.view.MotionEvent;
  36. import android.view.View;
  37. import java.util.ArrayList;
  38. public class LineGraph extends View {
  39. private ArrayList<Line> lines = new ArrayList<Line>();
  40. Paint paint = new Paint();
  41. private float minY = 0, minX = 0;
  42. private float maxY = 0, maxX = 0;
  43. private double rangeYRatio = 0;
  44. private double rangeXRatio = 0;
  45. private boolean isMaxYUserSet = false;
  46. private boolean isMaxXUserSet = false;
  47. private int lineToFill = -1;
  48. private int indexSelected = -1;
  49. private OnPointClickedListener listener;
  50. private Bitmap fullImage;
  51. private boolean shouldUpdate = false;
  52. public LineGraph(Context context){
  53. super(context);
  54. this.setWillNotDraw(false);
  55. }
  56. public LineGraph(Context context, AttributeSet attrs) {
  57. super(context, attrs);
  58. this.setWillNotDraw(false);
  59. }
  60. public void setMinY(float minY){
  61. }
  62. public void removeAllLines(){
  63. while (lines.size() > 0){
  64. lines.remove(0);
  65. }
  66. shouldUpdate = true;
  67. postInvalidate();
  68. }
  69. public void addLine(Line line) {
  70. lines.add(line);
  71. shouldUpdate = true;
  72. postInvalidate();
  73. }
  74. public void addPointToLine(int lineIndex, double x, double y){
  75. addPointToLine(lineIndex, (float) x, (float) y);
  76. }
  77. public void addPointToLine(int lineIndex, float x, float y){
  78. LinePoint p = new LinePoint(x, y);
  79. addPointToLine(lineIndex, p);
  80. }
  81. public double getRangeYRatio(){
  82. return rangeYRatio;
  83. }
  84. public void setRangeYRatio(double rr){
  85. this.rangeYRatio = rr;
  86. }
  87. public double getRangeXRatio(){
  88. return rangeXRatio;
  89. }
  90. public void setRangeXRatio(double rr){
  91. this.rangeXRatio = rr;
  92. }
  93. public void addPointToLine(int lineIndex, LinePoint point){
  94. Line line = getLine(lineIndex);
  95. line.addPoint(point);
  96. lines.set(lineIndex, line);
  97. resetLimits();
  98. shouldUpdate = true;
  99. postInvalidate();
  100. }
  101. public void addPointsToLine(int lineIndex, LinePoint[] points){
  102. Line line = getLine(lineIndex);
  103. for(LinePoint point : points){
  104. line.addPoint(point);
  105. }
  106. lines.set(lineIndex, line);
  107. resetLimits();
  108. shouldUpdate = true;
  109. postInvalidate();
  110. }
  111. public void removeAllPointsAfter(int lineIndex, double x){
  112. removeAllPointsBetween(lineIndex, x, getMaxX());
  113. }
  114. public void removeAllPointsBefore(int lineIndex, double x){
  115. removeAllPointsBetween(lineIndex, getMinX(), x);
  116. }
  117. public void removeAllPointsBetween(int lineIndex, double startX, double finishX){
  118. Line line = getLine(lineIndex);
  119. LinePoint[] pts = new LinePoint[line.getPoints().size()];
  120. pts = line.getPoints().toArray(pts);
  121. for(LinePoint point : pts){
  122. if(point.getX() >= startX && point.getX() <= finishX)
  123. line.removePoint(point);
  124. }
  125. lines.set(lineIndex, line);
  126. resetLimits();
  127. shouldUpdate = true;
  128. postInvalidate();
  129. }
  130. public void removePointsFromLine(int lineIndex, LinePoint[] points){
  131. Line line = getLine(lineIndex);
  132. for(LinePoint point : points){
  133. line.removePoint(point);
  134. }
  135. lines.set(lineIndex, line);
  136. resetLimits();
  137. shouldUpdate = true;
  138. postInvalidate();
  139. }
  140. public void removePointFromLine(int lineIndex, float x, float y){
  141. LinePoint p = null;
  142. Line line = getLine(lineIndex);
  143. p = line.getPoint(x, y);
  144. removePointFromLine(lineIndex, p);
  145. }
  146. public void removePointFromLine(int lineIndex, LinePoint point){
  147. Line line = getLine(lineIndex);
  148. line.removePoint(point);
  149. lines.set(lineIndex, line);
  150. resetLimits();
  151. shouldUpdate = true;
  152. postInvalidate();
  153. }
  154. public void resetYLimits(){
  155. float range = getMaxY() - getMinY();
  156. setRangeY(getMinY()-range*getRangeYRatio(), getMaxY()+range*getRangeYRatio());
  157. }
  158. public void resetXLimits(){
  159. float range = getMaxX() - getMinX();
  160. setRangeX(getMinX()-range*getRangeXRatio(), getMaxX()+range*getRangeXRatio());
  161. }
  162. public void resetLimits() {
  163. resetYLimits();
  164. resetXLimits();
  165. }
  166. public ArrayList<Line> getLines() {
  167. return lines;
  168. }
  169. public void setLineToFill(int indexOfLine) {
  170. this.lineToFill = indexOfLine;
  171. shouldUpdate = true;
  172. postInvalidate();
  173. }
  174. public int getLineToFill(){
  175. return lineToFill;
  176. }
  177. public void setLines(ArrayList<Line> lines) {
  178. this.lines = lines;
  179. }
  180. public Line getLine(int index) {
  181. return lines.get(index);
  182. }
  183. public int getSize(){
  184. return lines.size();
  185. }
  186. public void setRangeY(float min, float max) {
  187. minY = min;
  188. maxY = max;
  189. isMaxYUserSet = true;
  190. }
  191. private void setRangeY(double min, double max){
  192. minY = (float)min;
  193. maxY = (float)max;
  194. isMaxXUserSet = true;
  195. }
  196. public void setRangeX(float min, float max) {
  197. minX = min;
  198. maxX = max;
  199. }
  200. private void setRangeX(double min, double max){
  201. minX = (float)min;
  202. maxX = (float)max;
  203. }
  204. public float getMaxY(){
  205. float max = lines.get(0).getPoint(0).getY();
  206. for (Line line : lines){
  207. for (LinePoint point : line.getPoints()){
  208. max = point.getY() > max ? point.getY() : max;
  209. }
  210. }
  211. maxY = max;
  212. return maxY;
  213. }
  214. public float getMinY(){
  215. float min = lines.get(0).getPoint(0).getY();
  216. for (Line line : lines){
  217. for (LinePoint point : line.getPoints()){
  218. min = point.getY() < min ? point.getY() : min;
  219. }
  220. }
  221. minY = min;
  222. return minY;
  223. }
  224. public float getMinLimY(){
  225. return minY;
  226. }
  227. public float getMaxLimY(){
  228. return maxY;
  229. }
  230. public float getMinLimX(){
  231. return minX;
  232. }
  233. public float getMaxLimX(){
  234. if (isMaxXUserSet) {
  235. return maxX;
  236. }
  237. else {
  238. return getMaxX();
  239. }
  240. }
  241. public float getMaxX(){
  242. float max = lines.size() > 0 ? lines.get(0).getPoint(0).getX() : 0;
  243. for (Line line : lines){
  244. for (LinePoint point : line.getPoints()){
  245. max = point.getX() > max ? point.getX() : max;
  246. }
  247. }
  248. maxX = max;
  249. return maxX;
  250. }
  251. public float getMinX(){
  252. float min = lines.size() > 0 ? lines.get(0).getPoint(0).getX() : 0;
  253. for (Line line : lines){
  254. for (LinePoint point : line.getPoints()){
  255. min = point.getX() < min ? point.getX() : min;
  256. }
  257. }
  258. minX = min;
  259. return minX;
  260. }
  261. public void onDraw(Canvas ca) {
  262. super.onDraw(ca);
  263. if (fullImage == null || shouldUpdate) {
  264. fullImage = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
  265. Canvas canvas = new Canvas(fullImage);
  266. paint.reset();
  267. Path path = new Path();
  268. float bottomPadding = 10, topPadding = 10;
  269. float sidePadding = 10;
  270. float usableHeight = getHeight() - bottomPadding - topPadding;
  271. float usableWidth = getWidth() - 2*sidePadding;
  272. float maxY = getMaxLimY();
  273. float minY = getMinLimY();
  274. float maxX = getMaxLimX();
  275. float minX = getMinLimX();
  276. int lineCount = 0;
  277. for (Line line : lines){
  278. int count = 0;
  279. float firstXPixels = 0, lastXPixels = 0, newYPixels = 0;
  280. float lastYPixels = 0, newXPixels = 0;
  281. if (lineCount == lineToFill){
  282. paint.setColor(Color.BLACK);
  283. paint.setAlpha(30);
  284. paint.setStrokeWidth(2);
  285. for (int i = 10; i-getWidth() < getHeight(); i = i+20){
  286. canvas.drawLine(i, getHeight()-bottomPadding, 0, getHeight()-bottomPadding-i, paint);
  287. }
  288. paint.reset();
  289. paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
  290. for (LinePoint p : line.getPoints()){
  291. float yPercent = (p.getY()-minY)/(maxY - minY);
  292. float xPercent = (p.getX()-minX)/(maxX - minX);
  293. if (count == 0){
  294. lastXPixels = sidePadding + (xPercent*usableWidth);
  295. lastYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  296. firstXPixels = lastXPixels;
  297. path.moveTo(lastXPixels, lastYPixels);
  298. } else {
  299. newXPixels = sidePadding + (xPercent*usableWidth);
  300. newYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  301. path.lineTo(newXPixels, newYPixels);
  302. Path pa = new Path();
  303. pa.moveTo(lastXPixels, lastYPixels);
  304. pa.lineTo(newXPixels, newYPixels);
  305. pa.lineTo(newXPixels, 0);
  306. pa.lineTo(lastXPixels, 0);
  307. pa.close();
  308. canvas.drawPath(pa, paint);
  309. lastXPixels = newXPixels;
  310. lastYPixels = newYPixels;
  311. }
  312. count++;
  313. }
  314. path.reset();
  315. path.moveTo(0, getHeight()-bottomPadding);
  316. path.lineTo(sidePadding, getHeight()-bottomPadding);
  317. path.lineTo(sidePadding, 0);
  318. path.lineTo(0, 0);
  319. path.close();
  320. canvas.drawPath(path, paint);
  321. path.reset();
  322. path.moveTo(getWidth(), getHeight()-bottomPadding);
  323. path.lineTo(getWidth()-sidePadding, getHeight()-bottomPadding);
  324. path.lineTo(getWidth()-sidePadding, 0);
  325. path.lineTo(getWidth(), 0);
  326. path.close();
  327. canvas.drawPath(path, paint);
  328. }
  329. lineCount++;
  330. }
  331. paint.reset();
  332. paint.setColor(Color.BLACK);
  333. paint.setAlpha(50);
  334. paint.setAntiAlias(true);
  335. canvas.drawLine(sidePadding, getHeight() - bottomPadding, getWidth()-sidePadding, getHeight()-bottomPadding, paint);
  336. paint.setAlpha(255);
  337. for (Line line : lines){
  338. int count = 0;
  339. float lastXPixels = 0, newYPixels = 0;
  340. float lastYPixels = 0, newXPixels = 0;
  341. paint.setColor(line.getColor());
  342. paint.setStrokeWidth(getStrokeWidth(line));
  343. for (LinePoint p : line.getPoints()){
  344. float yPercent = (p.getY()-minY)/(maxY - minY);
  345. float xPercent = (p.getX()-minX)/(maxX - minX);
  346. if (count == 0){
  347. lastXPixels = sidePadding + (xPercent*usableWidth);
  348. lastYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  349. } else {
  350. newXPixels = sidePadding + (xPercent*usableWidth);
  351. newYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  352. canvas.drawLine(lastXPixels, lastYPixels, newXPixels, newYPixels, paint);
  353. lastXPixels = newXPixels;
  354. lastYPixels = newYPixels;
  355. }
  356. count++;
  357. }
  358. }
  359. int pointCount = 0;
  360. for (Line line : lines){
  361. paint.setColor(line.getColor());
  362. paint.setStrokeWidth(getStrokeWidth(line));
  363. paint.setStrokeCap(Paint.Cap.ROUND);
  364. if (line.isShowingPoints()){
  365. for (LinePoint p : line.getPoints()){
  366. float yPercent = (p.getY()-minY)/(maxY - minY);
  367. float xPercent = (p.getX()-minX)/(maxX - minX);
  368. float xPixels = sidePadding + (xPercent*usableWidth);
  369. float yPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
  370. int outerRadius;
  371. if (line.isUsingDips()) {
  372. outerRadius = getPixelForDip(line.getStrokeWidth() + 4);
  373. }
  374. else {
  375. outerRadius = line.getStrokeWidth() + 4;
  376. }
  377. int innerRadius = outerRadius / 2;
  378. paint.setColor(Color.parseColor(p.getColor()));
  379. canvas.drawCircle(xPixels, yPixels, outerRadius, paint);
  380. paint.setColor(Color.WHITE);
  381. canvas.drawCircle(xPixels, yPixels, innerRadius, paint);
  382. Path path2 = new Path();
  383. path2.addCircle(xPixels, yPixels, 30, Direction.CW);
  384. p.setPath(path2);
  385. p.setRegion(new Region((int)(xPixels-30), (int)(yPixels-30), (int)(xPixels+30), (int)(yPixels+30)));
  386. if (indexSelected == pointCount && listener != null){
  387. paint.setColor(Color.parseColor(p.getColor()));
  388. paint.setAlpha(100);
  389. canvas.drawPath(p.getPath(), paint);
  390. paint.setAlpha(255);
  391. }
  392. pointCount++;
  393. }
  394. }
  395. }
  396. shouldUpdate = false;
  397. }
  398. ca.drawBitmap(fullImage, 0, 0, null);
  399. }
  400. private int getStrokeWidth(Line line) {
  401. int strokeWidth;
  402. if (line.isUsingDips()) {
  403. strokeWidth = getPixelForDip(line.getStrokeWidth());
  404. }
  405. else {
  406. strokeWidth = line.getStrokeWidth();
  407. }
  408. return strokeWidth;
  409. }
  410. private int getPixelForDip(int dipValue) {
  411. return (int) TypedValue.applyDimension(
  412. TypedValue.COMPLEX_UNIT_DIP,
  413. dipValue,
  414. getResources().getDisplayMetrics());
  415. }
  416. @Override
  417. public boolean onTouchEvent(MotionEvent event) {
  418. Point point = new Point();
  419. point.x = (int) event.getX();
  420. point.y = (int) event.getY();
  421. int count = 0;
  422. int lineCount = 0;
  423. int pointCount = 0;
  424. Region r = new Region();
  425. for (Line line : lines){
  426. pointCount = 0;
  427. for (LinePoint p : line.getPoints()){
  428. if (p.getPath() != null && p.getRegion() != null){
  429. r.setPath(p.getPath(), p.getRegion());
  430. if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
  431. indexSelected = count;
  432. } else if (event.getAction() == MotionEvent.ACTION_UP){
  433. if (r.contains((int)point.x,(int) point.y) && listener != null){
  434. listener.onClick(lineCount, pointCount);
  435. }
  436. indexSelected = -1;
  437. }
  438. }
  439. pointCount++;
  440. count++;
  441. }
  442. lineCount++;
  443. }
  444. if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP){
  445. shouldUpdate = true;
  446. postInvalidate();
  447. }
  448. return true;
  449. }
  450. public void setOnPointClickedListener(OnPointClickedListener listener) {
  451. this.listener = listener;
  452. }
  453. public interface OnPointClickedListener {
  454. abstract void onClick(int lineIndex, int pointIndex);
  455. }
  456. }