java - MP Chart Showing One Extra Value at X_axis in Line Chart? -
i have implemented mp linechart. every thing working except one.it adds first month @ end of x axis. , values starts second point not first one. tried several solutions provided on google couldn't succeeded. e.g in following screen shot months list contains [apr,may,jun,jul,aug] adding in apr @ end , line starting may not apr. following code.
string[] mmonths; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_multi_line_chart); context = this; mpref = new mysharedpreferences(this); charts_list = new arraylist<charts_data_model>(); mchart = (linechart) findviewbyid(r.id.chart1); mchart.setonchartvalueselectedlistener(this); mchart.setdrawgridbackground(false); mchart.setdescription("vsc chart"); mchart.setdrawborders(false); mchart.getaxisleft().setenabled(true); mchart.getaxisright().setdrawaxisline(false); mchart.getaxisright().setenabled(false); mchart.getaxisright().setdrawgridlines(false); mchart.getxaxis().setdrawaxisline(true); mchart.getxaxis().setdrawgridlines(true); // enable touch gestures mchart.settouchenabled(true); // enable scaling , dragging mchart.setdragenabled(true); mchart.setscaleenabled(true); // if disabled, scaling can done on x- , y-axis separately mchart.setpinchzoom(true); legend l = mchart.getlegend(); l.setposition(legend.legendposition.above_chart_left); l.setxentryspace(7f); l.setyentryspace(0f); l.setyoffset(0f); l.setdirection(legend.legenddirection.left_to_right); l.setwordwrapenabled(true); new task4().execute(); } class task4 extends asynctask<void, void, string> { @override protected void onpreexecute() { pdialog = new progressdialog(multilinechartactivity.this); pdialog.setmessage("please wait"); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); super.onpreexecute(); } @override protected string doinbackground(void... arg0) { getchartdatahadsm(); //record method return null; } @override protected void onpostexecute(string result) { super.onpostexecute(result); xaxis xaxis = mchart.getxaxis(); xaxis.setposition(xaxis.xaxisposition.bottom); xaxis.setaxisminvalue(0f); xaxis.setavoidfirstlastclipping(true); // xaxis.setgranularityenabled(false); xaxis.setgranularity(1f); xaxis.setvalueformatter(new axisvalueformatter() { @override public string getformattedvalue(float value, axisbase axis) { return mmonths[(int) value % mmonths.length]; } @override public int getdecimaldigits() { return 0; } }); linedata data = new linedata(sets); mchart.setdata(data); mchart.invalidate(); pdialog.dismiss(); } } public void getchartdatahadsm() { mmonths = new string[charts_list_hadsm.size()]; arraylist<entry> e1 = new arraylist<entry>(); arraylist<entry> e2 = new arraylist<entry>(); int size = charts_list_hadsm.size(); (int = 0; < size; i++) { string month = charts_list_hadsm.get(i).month; mmonths[i] = month; string anx = charts_list_hadsm.get(i).anx; string dep = charts_list_hadsm.get(i).dep; e1.add(new entry(i + 1, integer.parseint(anx))); e2.add(new entry(i + 1, integer.parseint(dep))); } sets = new arraylist<ilinedataset>(); if (!e1.isempty()) { linedataset d1 = new linedataset(e1, "anxiety"); d1.setlinewidth(2.5f); d1.setcircleradius(4.5f); d1.sethighlightcolor(color.rgb(244, 117, 117)); d1.setdrawvalues(false); sets.add(d1); } if (!e2.isempty()) { linedataset d2 = new linedataset(e2, "depression"); d2.setlinewidth(2.5f); d2.setcircleradius(4.5f); d2.sethighlightcolor(color.rgb(244, 117, 117)); d2.setcolor(colortemplate.vordiplom_colors[0]); d2.setcirclecolor(colortemplate.vordiplom_colors[0]); d2.setdrawvalues(false); sets.add(d2); } }
please hint or appreciated.
i have not been working chartset use. error describing , image messing indexes.
i have reviewed source , found following thing find suspicious , may want check:
for (int = 0; < size; i++) { // month list 0-based string month = charts_list_hadsm.get(i).month; mmonths[i] = month; string anx = charts_list_hadsm.get(i).anx; string dep = charts_list_hadsm.get(i).dep; // data list 1-based // add data @ (i+1) skipping position 0 e1.add(new entry(i + 1, integer.parseint(anx))); e2.add(new entry(i + 1, integer.parseint(dep))); }
essentially, month have 1 legend few, , seems legend rotates when reaches end of entries.
it matches image:
in apr column, there no value, starts may. values added there 1 more value there month in legend. thus, legend restarts apr show value.
i suggest fix:
for (int = 0; < size; i++) { // month list 0-based string month = charts_list_hadsm.get(i).month; mmonths[i] = month; string anx = charts_list_hadsm.get(i).anx; string dep = charts_list_hadsm.get(i).dep; // make entries index 0-based, e1.add(new entry(i, integer.parseint(anx))); e2.add(new entry(i, integer.parseint(dep))); }
Comments
Post a Comment