Engineering Format of y-axis values in AChartEngine for Android -
i working on project need plotting data. use android studio 5.2.0 , achartengine 1.2.0 on windows 7 64 bits pc. interested in engineering formatting of y-labels, e.g. 2.3e6.. have studied without success. wish 1, 3, 0, 1 , 1 min no. of integer digits, max no. of integer digits, min no. of fraction digits, max no. of fraction digits , no. of digits in exponent part, respectively. in addition exponent must muliple of 3.
here linegraph class:
public class linegraph { public intent getintent(context context) { int size = ( int ) math.pow(2, 10); double start = 0; double end = 2 * math.pi; double inc = (end - start) / (size - 1); double[] x = new double[size]; double[] y = new double[size]; xymultipleseriesdataset dataset = new xymultipleseriesdataset(); timeseries series = new timeseries("sin * cos"); ( int = 0; < size; i++ ) { x[i] = start + * inc; y[i] = 10000 * math.sin(x[i]) * math.cos(x[i]); series.add(x[i], y[i]); } dataset.addseries(series); xyseriesrenderer ren = new xyseriesrenderer(); // xyseriesrenderer properties ren.setcolor(color.red); ren.setfillpoints(true); ren.setdisplayboundingpoints(false); ren.setfillpoints(true); ren.setlinewidth(2f); // xymultipleseriesrenderer properties xymultipleseriesrenderer mren = new xymultipleseriesrenderer(); mren.setantialiasing(true); mren.setmargins(new int[] { 10, 80, 10, 10 }); mren.setshowgrid(true); mren.setapplybackgroundcolor(true); mren.setbackgroundcolor(color.ltgray); mren.setgridcolor(color.white); mren.setlabelstextsize(20f); mren.setylabelsverticalpadding(-5f); mren.setylabelspadding(40); mren.setshowlegend(false); mren.setxlabels(5); mren.setylabels(10); // number format decimalformat noformat = new decimalformat("##0.##e0"); noformat.setminimumintegerdigits(1); noformat.setmaximumintegerdigits(3); noformat.setminimumfractiondigits(0); noformat.setmaximumfractiondigits(2); mren.setylabelformat(noformat, 0); mren.addseriesrenderer(renderer); // construct , return intent. intent intent = chartfactory.getlinechartintent(context, dataset, mren, "line graph"); return intent; } }
and mainactivity:
public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if ( id == r.id.action_settings ) { return true; } return super.onoptionsitemselected(item); } public void linegraphhandler(view view) { linegraph linegraph = new linegraph(); intent lineintent = linegraph.getintent(this); startactivity(lineintent); } }
here activity_main.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="ramin.graphtutorial.graph.mainactivity"> <button android:id="@+id/button_linegraph" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:onclick="linegraphhandler" android:text="@string/button_line_graph_text"/> </relativelayout>
and finally, manifest.xml file:
<?xml version="1.0" encoding="utf-8"?> <manifest package="ramin.graphtutorial.graph" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowbackup="true" android:allowclearuserdata="true" android:allowtaskreparenting="true" android:hardwareaccelerated="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme"> <activity android:name="ramin.graphtutorial.graph.mainactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <activity android:name="org.achartengine.graphicalactivity"/> </application> </manifest>
it works fine when apply format method of noformat single double value, not fomatting y-labels. latter following;
xymultipleseriesrenderer mren = new xymultipleseriesrenderer(); xyseriesrenderer ren = new xyseriesrenderer (); mren.addseriesrenderer(ren); int noofylabels = 5; mren.setylabels( noofylabels ); mren.setylabelformat( noformat, 0 );
but 3 zeros, i.e., 000 values along y-axis larger 1000. have tried manually setting y-value text labels, following code snippet:
double[] ylabels = mren.getytextlabellocations(0); mren.setylabels( 0 ); ( int = 0; < noofylabels; i++ ) { mren.addytextlabel( ylabels[ ], noformat.format( ylabels[ ] ) ); }
but index out of bound in array ylabels. please me on issue? ps: asked question previously, didn't answers. maybe question not clear enough. thank in advance. ramin.
try this
private linearlayout chartlayout; private graphicalview mchart; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); showlinegraph( ); } public void showlinegraph() { chartlayout= (linearlayout) findviewbyid(r.id.graphholder); // linegraph linegraph = new linegraph(); int size = (int) math.pow(2, 10); double start = 0; double end = 2 * math.pi; double inc = (end - start) / (size - 1); double[] x = new double[size]; double[] y = new double[size]; xymultipleseriesdataset dataset = new xymultipleseriesdataset(); timeseries series = new timeseries("sin * cos"); for(int = 0; < size; i++) { x[i] = start + * inc; y[i] = 1000 * math.sin(x[i]) * math.cos(x[i]); series.add(x[i], y[i]); } dataset.addseries(series); xyseriesrenderer ren = new xyseriesrenderer(); // xyseriesrenderer properties ren.setcolor(color.red); ren.setfillpoints(true); ren.setdisplayboundingpoints(false); ren.setfillpoints(true); ren.setlinewidth(2f); // xymultipleseriesrenderer properties xymultipleseriesrenderer mren = new xymultipleseriesrenderer(); mren.setantialiasing(true); mren.setmargins(new int[] { 10, 80, 10, 10 }); mren.setshowgrid(true); mren.setapplybackgroundcolor(true); mren.setbackgroundcolor(color.ltgray); mren.setgridcolor(color.white); mren.setlabelstextsize(20f); mren.setylabelsverticalpadding(-5f); mren.setylabelspadding(40); mren.setshowlegend(false); mren.setxlabels(5); mren.setylabels(10); // number format decimalformat noformat = new decimalformat("##0.##e0"); noformat.setminimumintegerdigits(1); noformat.setmaximumintegerdigits(3); noformat.setminimumfractiondigits(0); noformat.setmaximumfractiondigits(2); mren.setylabelformat(noformat, 0); mren.addseriesrenderer(ren); // construct , return intent. view = chartfactory.getlinechartview(this, dataset, mren); chartlayout.addview(view); }
Comments
Post a Comment