how to display graph in android using MySQL database -
i can't data mysql database graphics. found example code create graph want put x axis data , y axis data database
my example codes are:
public class graph extends activity { private xyplot xyplot; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.graph); // initialize our xyplot reference: xyplot = (xyplot) findviewbyid(r.id.xyplot); number[] income = {2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 }; number[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 }; final string[] mmonths = new string[] { "jan","feb", "mar","apr", "may","jun", "jul", "aug","sep","oct", "nov","dec" }; // converting above income array xyseries xyseries incomeseries = new simplexyseries( arrays.aslist(income), // array => list simplexyseries.arrayformat.y_vals_only , // y_vals_only means use element index x value "income"); // title of series // converting above expense array xyseries xyseries expenseseries = new simplexyseries( arrays.aslist(expense), // array => list simplexyseries.arrayformat.y_vals_only, // y_vals_only means use element index x value "expense"); // title of series // create formatter format line , point of income series lineandpointformatter incomeformat = new lineandpointformatter( color.rgb(0, 0, 255), // line color color.rgb(200, 200, 200), // point color null ); // fill color (none) // create formatter format line , point of expense series lineandpointformatter expenseformat = new lineandpointformatter( color.rgb(255, 0, 0), // line color color.rgb(200, 200, 200), // point color null); // fill color (none) // add expense series xyplot: xyplot.addseries(expenseseries,expenseformat); // add income series xyplot: xyplot.addseries(incomeseries, incomeformat); // formatting domain values ( x-axis ) xyplot.setdomainvalueformat(new format() { @override public stringbuffer format(object obj, stringbuffer toappendto, fieldposition pos) { return new stringbuffer( mmonths[ ( (number)obj).intvalue() ] ); } @override public object parseobject(string source, parseposition pos) { return null; } }); xyplot.setdomainlabel(""); xyplot.setrangelabel("amount in dollars"); // increment x-axis 1 value xyplot.setdomainstep(xystepmode.increment_by_val, 1); xyplot.getgraphwidget().setrangelabelwidth(50); // reduce number of range labels xyplot.setticksperrangelabel(2); // reduce number of domain labels xyplot.setticksperdomainlabel(2); // remove developer guides chart xyplot.disableallmarkup(); }
get database codes:
public class allproductsactivity extends listactivity { // progress dialog private progressdialog pdialog; // creating json parser object jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> productslist; // url products list private static string url_all_products = "http://10.0.2.2/android_connect/get_all_products.php"; // json node names private static final string tag_success = "success"; private static final string tag_degerler = "degerler"; private static final string tag_sicaklik = "sicaklik"; private static final string tag_gerilim = "gerilim"; private static final string tag_akim = "akim"; private static final string tag_reel = "reel guc"; private static final string tag_reaktif = "reaktif guc"; // products jsonarray jsonarray products = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.all_products); // hashmap listview productslist = new arraylist<hashmap<string, string>>(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); // getting json string url jsonobject json = jparser.makehttprequest(url_all_products, "get", params); // check log cat json response log.d("all products: ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // products found // getting array of products products = json.getjsonarray(tag_degerler); // looping through products (int = 0; < products.length(); i++) { jsonobject c = products.getjsonobject(i); // storing each json item in variable string sicaklik = c.getstring(tag_sicaklik); string gerilim = c.getstring(tag_gerilim); string akim = c.getstring(tag_akim); string reel = c.getstring(tag_reel); string reaktif = c.getstring(tag_reaktif); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_sicaklik, sicaklik); map.put(tag_gerilim, gerilim); map.put(tag_akim, akim); map.put(tag_reel, reel); map.put(tag_reaktif, reaktif); // adding hashlist arraylist productslist.add(map); } } else { } } catch (jsonexception e) { e.printstacktrace(); } listadapter adapter = new simpleadapter( allproductsactivity.this, productslist, r.layout.list_item, new string[] { tag_sicaklik, tag_gerilim,tag_akim,tag_reel,tag_reaktif}, new int[] { r.id.sicaklik, r.id.gerilim, r.id.akim, r.id.reel, r.id.reaktif}); // updating listview setlistadapter(adapter); } }
json parser codes:
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or method public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); }else if(method == "get"){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
yeah, isn't going work. activity.oncreate not main(). can't put application there.
i'm sure isn't want hear, need go way drawing board. going want put code requests json object service.
the code actual graphing looks pretty good. need set when service succeeds in retrieving json, , parsing it, notifies activity there graph.
step code , brush on android architecture couple days. isn't hard, different.
Comments
Post a Comment