|
| 1 | +# MaterialUnixGraphLibrary |
| 2 | +Graphview for Android that plots points automatically based on Unix time input. |
| 3 | + |
| 4 | +Features: |
| 5 | +- Support for user touch input and dialogs |
| 6 | +- Automatic y- and x-axis lebel generation |
| 7 | +- Draw multiple lines simultioinesly |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### Adding depency to your project |
| 15 | + |
| 16 | +Add ```com.github.Velli20:MaterialUnixGraphLibrary``` to your dependencies in build.gradle |
| 17 | + |
| 18 | +```gradle |
| 19 | +repositories { |
| 20 | + jcenter() |
| 21 | + maven { url "https://jitpack.io" } |
| 22 | +} |
| 23 | + |
| 24 | +dependencies { |
| 25 | + compile 'com.github.Velli20:MaterialUnixGraphLibrary:v1.5' |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Add LineGraph to your layout |
| 30 | + |
| 31 | +```xml |
| 32 | +<com.velli20.materialunixgraph.LineGraph |
| 33 | + android:id="@+id/graph" |
| 34 | + android:layout_width="match_parent" |
| 35 | + android:layout_height="200dp"/> |
| 36 | +``` |
| 37 | + |
| 38 | +### Drawing line |
| 39 | + |
| 40 | +You can find full sample code under /sample/ directrory. |
| 41 | +Basic line drawing: |
| 42 | + |
| 43 | +```java |
| 44 | +static final int GRAPH_MAX_VERTICAL_VALUE = 120; |
| 45 | + @Override |
| 46 | + protected void onCreate(Bundle savedInstanceState) { |
| 47 | + super.onCreate(savedInstanceState); |
| 48 | + LineGraph graph = (LineGraph) findViewById(R.id.graph); |
| 49 | + |
| 50 | + long unixTimeNow = System.currentTimeMillis(); |
| 51 | + long oneDayInMillis = 1000 * 60 * 60 * 24; |
| 52 | + boolean showLinePoints = true; |
| 53 | + |
| 54 | + Line line = getDummyLine(unixTimeNow, unixTimeNow+oneDayInMillis, showLinePoints); |
| 55 | + line.setLineColor(Color.parseColor("#00b0ff")); |
| 56 | + line.setFillLine(true); |
| 57 | + line.setFillAlpha(60); /* Set alpha of the fill color 0-255 */ |
| 58 | + line.setLineStrokeWidth(2f); |
| 59 | + |
| 60 | + graph.setMaxVerticalAxisValue(GRAPH_MAX_VERTICAL_VALUE); |
| 61 | + graph.addLine(line); |
| 62 | + } |
| 63 | + |
| 64 | + public Line getDummyLine(long startDateInMillis, long endDateInMillis, boolean showPoints) { |
| 65 | + Line line = new Line(); |
| 66 | + |
| 67 | + /* Create y-axis points for the line */ |
| 68 | + LinePoint point; |
| 69 | + for (int i = 0; i < 10; i++) { |
| 70 | + long x = startDateInMillis + (((endDateInMillis - startDateInMillis) / 10) * i); |
| 71 | + |
| 72 | + point = new LinePoint(x, mRandom.nextInt(GRAPH_MAX_VERTICAL_VALUE)); |
| 73 | + point.setDrawPoint(showPoints); |
| 74 | + |
| 75 | + line.addPoint(point); |
| 76 | + } |
| 77 | + |
| 78 | + return line; |
| 79 | + } |
| 80 | +``` |
| 81 | + |
| 82 | +### Callbacks |
| 83 | + |
| 84 | +To know when the user taps the graph point or line you set |
| 85 | + |
| 86 | +```java |
| 87 | + |
| 88 | +final LineGraph graph = (LineGraph) findViewById(R.id.graph); |
| 89 | +graph.setOnLinePointTouchListener(new OnLinePointTouchListener() { |
| 90 | + @Override |
| 91 | + public void onLinePointClicked(Line line, LinePoint point) { |
| 92 | + /* User has clicked a point on the graph. Create a dialog to show above the touched point */ |
| 93 | + |
| 94 | + LinePointDialog dialog = new LinePointDialog(point.getX(), point.getY()); |
| 95 | + dialog.setTitle("Pseudo-Random value"); |
| 96 | + dialog.setTitleColor(Color.parseColor("#00b0ff")); |
| 97 | + dialog.setContentText(String.format(Locale.getDefault(), "%s\n%.2f €", getTimeLabel(point.getX()), point.getY())); |
| 98 | + dialog.setContentColor(Color.parseColor("#9e9e9e")); |
| 99 | + graph.drawDialog(dialog); |
| 100 | + } |
| 101 | + }); |
| 102 | +``` |
| 103 | + |
| 104 | +To disable the touch input |
| 105 | + |
| 106 | +```java |
| 107 | +graph.setDrawUserTouchPointEnabled(boolean enabled); |
0 commit comments