-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScatterPlotActivity.java
More file actions
176 lines (115 loc) · 4.82 KB
/
ScatterPlotActivity.java
File metadata and controls
176 lines (115 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.example.sudarshanseshadri.bloombergjson;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.ScatterChart;
import com.github.mikephil.charting.components.IMarker;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.ScatterData;
import com.github.mikephil.charting.data.ScatterDataSet;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class ScatterPlotActivity extends AppCompatActivity {
private static final double PERCENT_LIMIT = 3;
ScatterChart graph;
String xAxis;
String yAxis;
String filter;
double[] xAxisVals;
double[] yAxisVals;
TextView xAxisView, titleView;
VerticalLabelView yAxisView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scatter_plot);
Intent i = getIntent();
xAxis = i.getStringExtra("x-axis");
yAxis = i.getStringExtra("y-axis");
xAxisVals = i.getDoubleArrayExtra("x-axis values");
yAxisVals = i.getDoubleArrayExtra("y-axis values");
xAxisView = findViewById(R.id.id_textView_x_axis);
yAxisView = findViewById(R.id.id_textView_y_axis);
titleView = findViewById(R.id.id_textView_title);
xAxis = i.getStringExtra("x-axis");
yAxis = i.getStringExtra("y-axis");
xAxisView.setText(xAxis);
yAxisView.setText(yAxis);
yAxisView.setTextColor(xAxisView.getCurrentTextColor());
yAxis = i.getStringExtra("y-axis");
filter = i.getStringExtra("filter");
if (filter!=null) {
titleView.setText(yAxis + " vs " + xAxis + "\n Filtered by " + filter);
}
else
{
titleView.setText(yAxis + " vs " + xAxis);
}
graph = findViewById(R.id.id_scatter_plot);
setUpChart();
}
public void setUpChart() {
graph.setTouchEnabled(true);
graph.setDragEnabled(true);
graph.setScaleEnabled(true);
graph.setBackgroundColor(Color.WHITE);
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < yAxisVals.length; i++) {
values.add(new Entry((float) yAxisVals[i], (float) xAxisVals[i]));
}
ScatterDataSet dataSet = new ScatterDataSet(values, "Randoms");
dataSet.setDrawValues(false);
dataSet.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
dataSet.setScatterShapeSize(8f);
dataSet.setColor(Color.BLACK);
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
ScatterData data = new ScatterData(dataSet);
CustomMarkerView mv = new CustomMarkerView(this, R.layout.custom_marker_view_layout);
graph.getDescription().setEnabled(false);
graph.setMarker(mv);
graph.getLegend().setEnabled(false);
graph.setData(data);
graph.invalidate();
graph.animateY(100);
}
public class CustomMarkerView extends MarkerView {
private TextView tvContent;
public CustomMarkerView (Context context, int layoutResource) {
super(context, layoutResource);
// this markerview only displays a textview
tvContent = (TextView) findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText(e.getX()+ ", " + e.getY());
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}
}
}