· 7 years ago · Feb 09, 2019, 12:20 PM
1package com.exae.url;
2 import java.util.ArrayList;
3 import java.util.List;
4 import org.achartengine.ChartFactory;
5 import org.achartengine.model.CategorySeries;
6 import org.achartengine.renderer.DefaultRenderer;
7 import org.achartengine.renderer.SimpleSeriesRenderer;
8 import android.app.Activity;
9 import android.content.Context;
10 import android.content.Intent;
11 import android.database.Cursor;
12 import android.database.sqlite.SQLiteDatabase;
13 import android.database.sqlite.SQLiteException;
14 import android.graphics.Color;
15 import android.os.Bundle;
16 import android.util.Log;
17 import android.view.View;
18 import android.widget.Button;
19 import android.widget.ImageView;
20 import android.widget.LinearLayout;
21 import android.widget.TextView;
22
23 public class NewData extends Activity {
24 List<PieDetailsItem> piedata = new ArrayList<PieDetailsItem>(0);
25
26private final String SAMPLE_DB_NAME = "chartdb";
27private final String SAMPLE_TABLE_NAME = "piechart1";
28SQLiteDatabase sampleDB = null;
29LinearLayout ll;
30protected static final Context context = null;
31int year;
32double value;
33TextView tv;
34Button bt;
35ImageView img_view;
36int count=0;
37String value_arr[]={};
38static int[] my_year={};
39static double[] my_values={};
40 int my_values_length=0;
41/** Called when the activity is first created. */
42@Override
43public void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.dbchart);
46
47 //ArrayList<String> results = new ArrayList<String>();
48 //SQLiteDatabase sampleDB = null;
49 ll=(LinearLayout)findViewById(R.id.my_lay);
50tv=(TextView)findViewById(R.id.mu_text);
51 bt=(Button)findViewById(R.id.generate);
52 img_view=(ImageView)findViewById(R.id.pie);
53
54
55 try {
56 sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
57
58 sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
59 SAMPLE_TABLE_NAME +
60 " (Year INT(5),Value DOUBLE(5));");
61 sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (2001,'15');");
62 sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (2002,'20');");
63 sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (2003,'10');");
64 sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (2004,'39');");
65 sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (2005,'16');");
66
67 /*sampleDB.execSQL("INSERT INTO " +
68 SAMPLE_TABLE_NAME +
69 " Values ('Makam','Sai Geetha','India',25);");
70 sampleDB.execSQL("INSERT INTO " +
71 SAMPLE_TABLE_NAME +
72 " Values ('Chittur','Raman','India',25);");
73 sampleDB.execSQL("INSERT INTO " +
74 SAMPLE_TABLE_NAME +
75 " Values ('Solutions','Collabera','India',20);");*/
76
77 Cursor c = sampleDB.rawQuery("SELECT Year,Value FROM " + SAMPLE_TABLE_NAME , null);
78
79
80 if (c != null ) {
81 if (c.moveToFirst()) {
82 do {
83 //String firstName = c.getString(c.getColumnIndex("FirstName"));
84 year = c.getInt(c.getColumnIndex("Year"));
85 value = c.getDouble(c.getColumnIndex("Value"));
86
87
88 System.out.println(year);
89 System.out.println(value);
90 tv.setText("Year" + year + ",Value: " + value);
91 }while (c.moveToNext());
92
93 }
94 }
95
96 int columncount=c.getCount();
97 int columnindex=0;
98 int columnindex1=1;
99 System.out.println("count------>"+columncount);
100
101
102 my_year = new int[columncount];
103
104 if (c.moveToFirst())
105 {
106 for (int i = 0; i < columncount; i++)
107 {
108 my_year[i] = c.getInt(columnindex);
109 System.out.println("database value-----"+i);
110 System.out.println(my_year[i]);
111 c.moveToNext();
112 }
113 }
114
115
116
117 my_values = new double[columncount];
118
119 if (c.moveToFirst())
120 {
121 for (int j = 0; j < columncount; j++)
122 {
123 my_values[j] = c.getDouble(columnindex1);
124 System.out.println("database value-----"+j);
125 System.out.println(my_values[j]);
126 c.moveToNext();
127 }
128 }
129
130
131 int my_year_length=my_year.length;
132 System.out.println("string length----->"+my_year_length);
133
134 for(int k=0;k<my_year_length;k++)
135 {
136 System.out.println("array stored values----------->"+my_year[k]);
137 }
138
139
140 my_values_length=my_values.length;
141 System.out.println("string length----->"+my_values_length);
142
143 for(int k1=0;k1<my_values_length;k1++)
144 {
145 System.out.println("array stored values----------->"+my_values[k1]);
146 }
147
148
149
150 bt.setOnClickListener(new View.OnClickListener()
151 {
152 public void onClick(View v)
153 {
154
155 Intent achartIntent = new AChartExample().execute(context);
156 startActivity(achartIntent);
157
158 }
159
160 });
161 //this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
162
163
164
165
166 } catch (SQLiteException se ) {
167 Log.e(getClass().getSimpleName(), "Could not create or Open the database");
168 } finally {
169 if (sampleDB != null)
170 sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
171 sampleDB.close();
172 }
173
174}
175
176public Intent execute(Context context) {
177
178 Log.i("", "enter into achartexample class...........");
179 int[] colors = new int[] { Color.RED, Color.YELLOW, Color.BLUE, Color.GRAY, Color.WHITE};
180 DefaultRenderer renderer = buildCategoryRenderer(colors);
181
182
183 CategorySeries categorySeries = new CategorySeries("Vehicles Chart");
184 for(int p=0;p<my_values_length;p++)
185 {
186 System.out.println("values of pie---->"+my_values[p]);
187 categorySeries.add("bikes ", my_values[p]);
188 System.out.println("values ahs been added!!!!!");
189 }
190
191 Log.i("","vaules going to out.....");
192 return ChartFactory.getPieChartIntent(context, categorySeries, renderer, null);
193
194 }
195
196 protected DefaultRenderer buildCategoryRenderer(int[] colors)
197 {
198 DefaultRenderer renderer = new DefaultRenderer();
199
200 for (int color : colors) {
201 SimpleSeriesRenderer r = new SimpleSeriesRenderer();
202 r.setColor(color);
203 Log.i("", "color added....");
204 renderer.addSeriesRenderer(r);
205 }
206 return renderer;
207 }
208
209 }
210
21109-20 16:31:22.491: E/AndroidRuntime(8118): FATAL EXCEPTION: main
212 09-20 16:31:22.491: E/AndroidRuntime(8118): java.lang.NullPointerException
213 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.content.ComponentName.<init>(ComponentName.java:75)
214 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.content.Intent.<init> (Intent.java:2702)
215 09-20 16:31:22.491: E/AndroidRuntime(8118): at org.achartengine.ChartFactory.getPieChartIntent(ChartFactory.java:594)
216 09-20 16:31:22.491: E/AndroidRuntime(8118): at com.exae.url.NewData$AChartExample.execute(NewData.java:213)
217 09-20 16:31:22.491: E/AndroidRuntime(8118): at com.exae.url.NewData$1.onClick(NewData.java:174)
218 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.view.View.performClick(View.java:2485)
219 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.view.View$PerformClick.run(View.java:9080)
220 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.os.Handler.handleCallback(Handler.java:587)
221 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.os.Handler.dispatchMessage(Handler.java:92)
222 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.os.Looper.loop(Looper.java:123)
223 09-20 16:31:22.491: E/AndroidRuntime(8118): at android.app.ActivityThread.main(ActivityThread.java:3683)
224 09-20 16:31:22.491: E/AndroidRuntime(8118): at java.lang.reflect.Method.invokeNative(Native Method)
225 09-20 16:31:22.491: E/AndroidRuntime(8118): at java.lang.reflect.Method.invoke(Method.java:507)
226 09-20 16:31:22.491: E/AndroidRuntime(8118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
227 09-20 16:31:22.491: E/AndroidRuntime(8118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
228 09-20 16:31:22.491: E/AndroidRuntime(8118): at dalvik.system.NativeStart.main(Native Method)
229
230protected static final Context context = null;
231
232ChartFactory.getPieChartIntent(context, categorySeries, renderer, null);