|
9 | 9 | import android.view.Menu; |
10 | 10 | import android.view.MenuItem; |
11 | 11 | import android.view.View; |
12 | | - |
13 | 12 | import com.github.mikephil.charting.charts.BarChart; |
| 13 | +import com.github.mikephil.charting.components.Description; |
14 | 14 | import com.github.mikephil.charting.data.BarData; |
15 | 15 | import com.github.mikephil.charting.data.BarDataSet; |
16 | 16 | import com.github.mikephil.charting.data.BarEntry; |
17 | 17 | import com.github.mikephil.charting.utils.ColorTemplate; |
18 | | - |
19 | 18 | import java.util.ArrayList; |
20 | 19 |
|
21 | 20 | public class MainActivity extends AppCompatActivity { |
22 | 21 |
|
23 | | - // Library at : https://github.com/PhilJay/MPAndroidChart/ |
24 | | - |
25 | | - @Override |
26 | | - protected void onCreate(Bundle savedInstanceState) { |
27 | | - super.onCreate(savedInstanceState); |
28 | | - setContentView(R.layout.activity_main); |
29 | | - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
30 | | - setSupportActionBar(toolbar); |
31 | | - |
32 | | - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
33 | | - fab.setOnClickListener(new View.OnClickListener() { |
34 | | - @Override |
35 | | - public void onClick(View view) { |
36 | | - Snackbar.make(view, "Using MPAndroidCharts 2.2.3", Snackbar.LENGTH_LONG).show(); |
37 | | - } |
38 | | - }); |
39 | | - |
40 | | - |
41 | | - // Data Sets |
42 | | - ArrayList<BarEntry> entries = new ArrayList<>(); |
43 | | - entries.add(new BarEntry(4f, 0)); |
44 | | - entries.add(new BarEntry(8f, 1)); |
45 | | - entries.add(new BarEntry(6f, 2)); |
46 | | - entries.add(new BarEntry(12f, 3)); |
47 | | - entries.add(new BarEntry(18f, 4)); |
48 | | - entries.add(new BarEntry(9f, 5)); |
49 | | - BarDataSet dataset = new BarDataSet(entries, "# of Calls"); |
50 | | - |
51 | | - |
52 | | - // Defining X Axis |
53 | | - ArrayList<String> labels = new ArrayList<String>(); |
54 | | - labels.add("January"); |
55 | | - labels.add("February"); |
56 | | - labels.add("March"); |
57 | | - labels.add("April"); |
58 | | - labels.add("May"); |
59 | | - labels.add("June"); |
60 | | - |
61 | | - // Declare the bar chart and set it as the view for this activity |
62 | | - final BarChart chart = (BarChart) findViewById(R.id.bar_chart); |
63 | | - |
64 | | - // Set data for the bar chart |
65 | | - BarData data = new BarData(labels, dataset); |
66 | | - chart.setData(data); |
67 | | - |
68 | | - // Set description |
69 | | - chart.setDescription("# of times Alice called Bob"); |
70 | | - |
71 | | - // Set themes |
72 | | - dataset.setColors(ColorTemplate.COLORFUL_COLORS); |
73 | | - |
74 | | - // Animate |
75 | | - chart.animateY(5000); |
76 | | - |
77 | | - // Save the chart aftre its drawn |
78 | | - new Handler().postDelayed(new Runnable() { |
79 | | - @Override |
80 | | - public void run() { |
81 | | - // Save the chart to gallery/path |
82 | | - chart.saveToGallery("mychart.jpg", 85); // 85 is the quality of the image |
83 | | - } |
84 | | - }, 2000); |
85 | | - |
86 | | - } |
87 | | - |
88 | | - @Override |
89 | | - public boolean onCreateOptionsMenu(Menu menu) { |
90 | | - // Inflate the menu; this adds items to the action bar if it is present. |
91 | | - getMenuInflater().inflate(R.menu.menu_main, menu); |
92 | | - return true; |
| 22 | + // Library at : https://github.com/PhilJay/MPAndroidChart/ |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void onCreate(Bundle savedInstanceState) { |
| 26 | + super.onCreate(savedInstanceState); |
| 27 | + setContentView(R.layout.activity_main); |
| 28 | + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 29 | + setSupportActionBar(toolbar); |
| 30 | + |
| 31 | + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
| 32 | + fab.setOnClickListener(new View.OnClickListener() { |
| 33 | + @Override |
| 34 | + public void onClick(View view) { |
| 35 | + Snackbar.make(view, "Using MPAndroidCharts 3.0.1", Snackbar.LENGTH_LONG).show(); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + // Data Sets |
| 40 | + ArrayList<BarEntry> entries = new ArrayList<>(); |
| 41 | + entries.add(new BarEntry(4f, 0)); |
| 42 | + entries.add(new BarEntry(8f, 1)); |
| 43 | + entries.add(new BarEntry(6f, 2)); |
| 44 | + entries.add(new BarEntry(12f, 3)); |
| 45 | + entries.add(new BarEntry(18f, 4)); |
| 46 | + entries.add(new BarEntry(9f, 5)); |
| 47 | + BarDataSet dataset = new BarDataSet(entries, "# of Calls"); |
| 48 | + |
| 49 | + // Declare the bar chart and set it as the view for this activity |
| 50 | + final BarChart chart = (BarChart) findViewById(R.id.bar_chart); |
| 51 | + |
| 52 | + // Set data for the bar chart |
| 53 | + BarData data = new BarData(dataset); |
| 54 | + chart.setData(data); |
| 55 | + chart.setFitBars(true); // make the x-axis fit exactly all bars |
| 56 | + |
| 57 | + // Set description |
| 58 | + Description description = new Description(); |
| 59 | + description.setText("# of times Alice called Bob"); |
| 60 | + chart.setDescription(description); |
| 61 | + |
| 62 | + // Set themes |
| 63 | + dataset.setColors(ColorTemplate.COLORFUL_COLORS); |
| 64 | + |
| 65 | + // Animate |
| 66 | + chart.animateY(5000); |
| 67 | + |
| 68 | + // Save the chart aftre its drawn |
| 69 | + new Handler().postDelayed(new Runnable() { |
| 70 | + @Override |
| 71 | + public void run() { |
| 72 | + // Save the chart to gallery/path |
| 73 | + chart.saveToGallery("mychart.jpg", 85); // 85 is the quality of the image |
| 74 | + } |
| 75 | + }, 2000); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 80 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 81 | + getMenuInflater().inflate(R.menu.menu_main, menu); |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 87 | + // Handle action bar item clicks here. The action bar will |
| 88 | + // automatically handle clicks on the Home/Up button, so long |
| 89 | + // as you specify a parent activity in AndroidManifest.xml. |
| 90 | + int id = item.getItemId(); |
| 91 | + |
| 92 | + //noinspection SimplifiableIfStatement |
| 93 | + if (id == R.id.action_settings) { |
| 94 | + return true; |
93 | 95 | } |
94 | 96 |
|
95 | | - @Override |
96 | | - public boolean onOptionsItemSelected(MenuItem item) { |
97 | | - // Handle action bar item clicks here. The action bar will |
98 | | - // automatically handle clicks on the Home/Up button, so long |
99 | | - // as you specify a parent activity in AndroidManifest.xml. |
100 | | - int id = item.getItemId(); |
101 | | - |
102 | | - //noinspection SimplifiableIfStatement |
103 | | - if (id == R.id.action_settings) { |
104 | | - return true; |
105 | | - } |
106 | | - |
107 | | - return super.onOptionsItemSelected(item); |
108 | | - } |
| 97 | + return super.onOptionsItemSelected(item); |
| 98 | + } |
109 | 99 | } |