forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFragment.java
More file actions
186 lines (136 loc) · 7.19 KB
/
SimpleFragment.java
File metadata and controls
186 lines (136 loc) · 7.19 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
175
176
177
178
179
180
181
182
183
184
185
186
package com.xxmassdeveloper.mpchartexample.fragments;
import android.support.v4.app.Fragment;
import com.github.mikephil.charting.charts.ScatterChart;
import com.github.mikephil.charting.charts.ScatterChart.ScatterShape;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.ScatterData;
import com.github.mikephil.charting.data.ScatterDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.FileUtils;
import com.xxmassdeveloper.mpchartexample.R;
import java.util.ArrayList;
public abstract class SimpleFragment extends Fragment {
protected BarData generateBarData(int dataSets, float range, int count) {
ArrayList<BarDataSet> sets = new ArrayList<BarDataSet>();
for(int i = 0; i < dataSets; i++) {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
// entries = FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "stacked_bars.txt");
for(int j = 0; j < count; j++) {
entries.add(new BarEntry((float) (Math.random() * range) + range / 4, j));
}
BarDataSet ds = new BarDataSet(entries, getLabel(i));
ds.setColors(ColorTemplate.VORDIPLOM_COLORS);
sets.add(ds);
}
BarData d = new BarData(ChartData.generateXVals(0, count), sets);
return d;
}
protected ScatterData generateScatterData(int dataSets, float range, int count) {
ArrayList<ScatterDataSet> sets = new ArrayList<ScatterDataSet>();
ScatterShape[] shapes = ScatterChart.getAllPossibleShapes();
for(int i = 0; i < dataSets; i++) {
ArrayList<Entry> entries = new ArrayList<Entry>();
for(int j = 0; j < count; j++) {
entries.add(new Entry((float) (Math.random() * range) + range / 4, j));
}
ScatterDataSet ds = new ScatterDataSet(entries, getLabel(i));
ds.setScatterShapeSize(12f);
ds.setScatterShape(shapes[i % shapes.length]);
ds.setColors(ColorTemplate.COLORFUL_COLORS);
ds.setScatterShapeSize(9f);
sets.add(ds);
}
ScatterData d = new ScatterData(ChartData.generateXVals(0, count), sets);
return d;
}
/**
* generates less data (1 DataSet, 4 values)
* @return
*/
protected PieData generatePieData() {
int count = 4;
ArrayList<Entry> entries1 = new ArrayList<Entry>();
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("Quarter 1");
xVals.add("Quarter 2");
xVals.add("Quarter 3");
xVals.add("Quarter 4");
for(int i = 0; i < count; i++) {
xVals.add("entry" + (i+1));
entries1.add(new Entry((float) (Math.random() * 60) + 40, i));
}
PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2014");
ds1.setColors(ColorTemplate.VORDIPLOM_COLORS);
ds1.setSliceSpace(2f);
PieData d = new PieData(xVals, ds1);
return d;
}
protected LineData generateLineData() {
// DataSet ds1 = new DataSet(n, "O(n)");
// DataSet ds2 = new DataSet(nlogn, "O(nlogn)");
// DataSet ds3 = new DataSet(nsquare, "O(n\u00B2)");
// DataSet ds4 = new DataSet(nthree, "O(n\u00B3)");
ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>();
LineDataSet ds1 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "sine.txt"), "Sine function");
LineDataSet ds2 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "cosine.txt"), "Cosine function");
ds1.setLineWidth(2f);
ds2.setLineWidth(2f);
ds1.setDrawCircles(false);
ds2.setDrawCircles(false);
ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]);
ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]);
// load DataSets from textfiles in assets folders
sets.add(ds1);
sets.add(ds2);
// sets.add(FileUtils.dataSetFromAssets(getActivity().getAssets(), "n.txt"));
// sets.add(FileUtils.dataSetFromAssets(getActivity().getAssets(), "nlogn.txt"));
// sets.add(FileUtils.dataSetFromAssets(getActivity().getAssets(), "square.txt"));
// sets.add(FileUtils.dataSetFromAssets(getActivity().getAssets(), "three.txt"));
int max = Math.max(sets.get(0).getEntryCount(), sets.get(1).getEntryCount());
LineData d = new LineData(ChartData.generateXVals(0, max), sets);
return d;
}
protected LineData getComplexity() {
ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>();
LineDataSet ds1 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "n.txt"), "O(n)");
LineDataSet ds2 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "nlogn.txt"), "O(nlogn)");
LineDataSet ds3 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "square.txt"), "O(n\u00B2)");
LineDataSet ds4 = new LineDataSet(FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "three.txt"), "O(n\u00B3)");
ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]);
ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]);
ds3.setColor(ColorTemplate.VORDIPLOM_COLORS[2]);
ds4.setColor(ColorTemplate.VORDIPLOM_COLORS[3]);
ds1.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]);
ds2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[1]);
ds3.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[2]);
ds4.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[3]);
ds1.setLineWidth(2.5f);
ds1.setCircleSize(3f);
ds2.setLineWidth(2.5f);
ds2.setCircleSize(3f);
ds3.setLineWidth(2.5f);
ds3.setCircleSize(3f);
ds4.setLineWidth(2.5f);
ds4.setCircleSize(3f);
// load DataSets from textfiles in assets folders
sets.add(ds1);
sets.add(ds2);
sets.add(ds3);
sets.add(ds4);
LineData d = new LineData(ChartData.generateXVals(0, ds1.getEntryCount()), sets);
return d;
}
private String[] mLabels = new String[] { "Company A", "Company B", "Company C", "Company D", "Company E", "Company F" };
// private String[] mXVals = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec" };
private String getLabel(int i) {
return mLabels[i];
}
}