java - How to get value from CandleStick and add to LineChart -> MPANDROIDCHART -
i face problem getting data candlestickchart , adding linechart.
ok, @ method. got data candlestick , configure data linechart. work perfectly. have generated 2 chars guys see on image:
but problem when trying add dynamicdata chars. method add data candlestickchart.
here method setdata on linechart, here candleentry lastentry = set1.getentryforindex(i);
know value on bars.
private void setdata() { int prog = 50; (int = 0; < prog; i++) { candleentry lastentry = set1.getentryforindex(i); float lastopenclosemax = math.max(lastentry.getopen(), lastentry.getclose()); entries.add(new entry(i, lastopenclosemax)); } xaxis xaxis = linechart.getxaxis(); xaxis.setposition(xaxis.xaxisposition.bottom); xaxis.settextcolor(color.transparent); xaxis.setdrawgridlines(false); yaxis leftaxis = linechart.getaxisleft(); leftaxis.setlabelcount(12, false); leftaxis.setdrawgridlines(false); leftaxis.setaxismaximum(1.6f); leftaxis.setaxisminimum(0.4f); leftaxis.setdrawaxisline(true); leftaxis.settextcolor(color.transparent); yaxis rightaxis = linechart.getaxisright(); rightaxis.setenabled(false); collections.sort(entries, new entryxcomparator()); linedataset = new linedataset(entries, "# of calls"); linedata = new linedata(linedataset); linedataset.setcolor(color.gray); linedataset.setdrawcircles(false); // dataset.setdrawfilled(true); linedata.setvaluetextcolor(color.transparent); linechart.getlegend().setenabled(false); linechart.getdescription().setenabled(false); linechart.setbackgroundcolor(color.transparent); linechart.setdata(linedata); linechart.animatex(4000); }
here addlineentry, not work , dont know why, guys have idea?
private void addlineentry() { linedata = linechart.getdata(); if (linedataset == null) { linedataset = createlineset(); linedata.adddataset(linedataset); } int prog = 1; (int = 0; < prog; i++) { candleentry lastentry = set1.getentryforindex(yvals1.size() - 1); float lastopenclosemax = math.max(lastentry.getopen(), lastentry.getclose()); entries.add(new entry(linedataset.getxmax() +1, lastopenclosemax)); } linedataset.notifydatasetchanged(); linechart.notifydatasetchanged(); linechart.invalidate(); mchart.moveviewto(mchart.getxchartmax(), 2f, yaxis.axisdependency.right); }
and last similar method candlestickchart addentry.
private void addentry(boolean start) { data = mchart.getdata(); if (set1 == null) { set1 = createset(); data.adddataset(set1); } float highmax = 1.0700f; float highlow = 1.1700f; float lowmax = 0.5700f; float lowlow = 0.63000f; int prog = 1; int xmax = (int) set1.getxmax(); candleentry lastentry = set1.getentryforindex(yvals1.size() - 1); (int = 0; < prog; i++) { float open = highlow + new random().nextfloat() * (highmax - highlow); float close = lowlow + new random().nextfloat() * (lowmax - lowlow); float lastopenclosemax = math.max(lastentry.getopen(), lastentry.getclose()); float currentopenclosemax = math.max(open, close); float currentopenclosemin = math.min(open, close); float high = open + 0.3f; float low = close - 0.3f; if (currentopenclosemax < lastopenclosemax) { yvals1.add(new candleentry(xmax + 1, high, low, currentopenclosemax, currentopenclosemin)); } else { yvals1.add(new candleentry(xmax + 1, high, low, currentopenclosemin, currentopenclosemax)); } mchart.notifydatasetchanged(); mchart.invalidate(); mchart.moveviewto(mchart.getxchartmax(), 2f, yaxis.axisdependency.right); } }
and here method remove entry candlestickchart , linechart, , work good.
private void removelastentry() { candledata data = mchart.getdata(); if (data != null) { icandledataset set = data.getdatasetbyindex(0); if (set != null) { set.removefirst(); data.notifydatachanged(); mchart.notifydatasetchanged(); mchart.invalidate(); } } linedata linedata = linechart.getdata(); if (linedata != null) { ilinedataset set = linedata.getdatasetbyindex(0); if (set != null) { set.removefirst(); data.notifydatachanged(); linechart.notifydatasetchanged(); linechart.invalidate(); } }
do guys have idea whats wrong? started bounty: here isfull class:
public class maingamefragment extends fragment { @bindview(r.id.spinner_money) spinner spinnerdata; @bindview(r.id.text_profit_ill) textview text_profit; @bindview(r.id.button_cash) button btncashcurrency; @bindview(r.id.restart_game) button restartgame; @bindview(r.id.butoncurrency) button buttoncurrency; @bindview(r.id.chart) candlestickchart mchart; @bindview(r.id.progress_bar) progressbar progress; @bindview(r.id.btn_buy) button btnbuy; @bindview(r.id.btn_sell) button btnsell; @bindview(r.id.drawer_settings) imagebutton opendrawersettings; @bindview(r.id.chartline) linechart linechart; public static arraylist<string> historytransaction = new arraylist<>(); public static arraylist<string> leaderboarduser = new arraylist<>(); public static string usernameandscore; private handler handler; private handler handlerlast; private string buttonposition; int pos = 0; lostdialogfragment lostfragment = lostdialogfragment.newinstance(1); windialogfragment winfragment = windialogfragment.newinstance(1); settingsfragment settingsfragment = settingsfragment.newinstance(1); string dialog_win = "windialogfragment"; string dialog_lost = "lostdialogfragment"; string dialog_settings = "settingsfragment"; private candledata data; private linedata linedata; private linedataset linedataset; private candledataset set1; private drawer result; public static storagereference storagereference; private runnable r; private runnable rlast; arraylist<candleentry> yvals1 = new arraylist<>(); arraylist<entry> entries = new arraylist<>(); public maingamefragment() { } @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); bundle bundle = getarguments(); if (bundle != null) { buttonposition = getarguments().getstring("button_position", "value"); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_main_game, container, false); butterknife.bind(this, view); settext(); configurespinnerdataandlogic(); configurechart(); configurecolorprogresbar(); opendrawer(); configuredrawer(); welcomemessage(); configurehandler(5000); storagereference = firebasestorage.getinstance().getreference(); setdata(); return view; } private void setdata() { int prog = 50; (int = 0; < prog; i++) { candleentry lastentry = set1.getentryforindex(i); float lastopenclosemax = math.max(lastentry.getopen(), lastentry.getclose()); entries.add(new entry(i, lastopenclosemax)); } xaxis xaxis = linechart.getxaxis(); xaxis.setposition(xaxis.xaxisposition.bottom); xaxis.settextcolor(color.transparent); xaxis.setdrawgridlines(false); yaxis leftaxis = linechart.getaxisleft(); leftaxis.setlabelcount(12, false); leftaxis.setdrawgridlines(false); leftaxis.setaxismaximum(1.6f); leftaxis.setaxisminimum(0.4f); leftaxis.setdrawaxisline(true); leftaxis.settextcolor(color.transparent); yaxis rightaxis = linechart.getaxisright(); rightaxis.setenabled(false); collections.sort(entries, new entryxcomparator()); linedataset = new linedataset(entries, "# of calls"); linedata = new linedata(linedataset); linedataset.setcolor(color.gray); linedataset.setdrawcircles(false); // dataset.setdrawfilled(true); linedata.setvaluetextcolor(color.transparent); linechart.getlegend().setenabled(false); linechart.getdescription().setenabled(false); linechart.setbackgroundcolor(color.transparent); linechart.setdata(linedata); linechart.animatex(4000); } private void welcomemessage() { toast.maketext(getcontext(), "welcome " + mainactivity.getusername(getcontext()).trim() + "!" + " getting data last hour..", toast.length_long).show(); } private void configuredrawer() { accountheader headerresult = new accountheaderbuilder() .withactivity(getactivity()) .withprofileimagesclickable(false) .withheaderbackground(r.drawable.logo_white) .withonaccountheaderlistener(new accountheader.onaccountheaderlistener() { @override public boolean onprofilechanged(view view, iprofile profile, boolean currentprofile) { return false; } }) .build(); result = new drawerbuilder() .withsliderbackgroundcolor(color.gray) .withaccountheader(headerresult) .withactivity(getactivity()) .withdisplaybelowstatusbar(false) .withdrawergravity(gravity.left) .withheaderpadding(true) .adddraweritems( new sectiondraweritem().withname("options"), new primarydraweritem().withname("trading history").withicon(r.drawable.trading_history).withidentifier(2), new primarydraweritem().withname("leader board").withicon(r.drawable.leade_board).withidentifier(3), new primarydraweritem().withname("special offer").withicon(r.drawable.special_icon).withidentifier(4), new primarydraweritem().withname("video tutorials").withicon(r.drawable.video_tutorials).withidentifier(5), new primarydraweritem().withname("faq").withicon(r.drawable.faq_icon).withidentifier(6), new primarydraweritem().withname("contact").withicon(r.drawable.contact_icon).withidentifier(7) ) .buildforfragment(); result.setondraweritemclicklistener(new drawer.ondraweritemclicklistener() { @override public boolean onitemclick(view view, int position, idraweritem draweritem) { fragmenttransaction ft = getchildfragmentmanager().begintransaction(); ft.addtobackstack(null); settingsfragment.position = position; result.closedrawer(); if (settingsfragment != null) { settingsfragment.show(ft, dialog_settings); } return true; } }); result.getdrawerlayout().setfitssystemwindows(false); result.getslider().setfitssystemwindows(false); } private candledataset createset() { set1 = new candledataset(null, "dataset 1"); set1.setcolor(color.rgb(240, 99, 99)); set1.sethighlightcolor(color.rgb(190, 190, 190)); set1.setaxisdependency(yaxis.axisdependency.left); set1.setvaluetextsize(10f); return set1; } private linedataset createlineset() { linedataset = new linedataset(null, "dataset 1"); linedataset.setcolor(color.rgb(240, 99, 99)); linedataset.sethighlightcolor(color.rgb(190, 190, 190)); linedataset.setaxisdependency(yaxis.axisdependency.left); linedataset.setvaluetextsize(10f); return linedataset; } public void opendrawer() { opendrawersettings.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { result.opendrawer(); } }); } private void addlineentry() { linedata = linechart.getdata(); if (linedataset == null) { linedataset = createlineset(); linedata.adddataset(linedataset); } int prog = 1; int xmax = (int) set1.getxmax(); (int = 0; < prog; i++) { candleentry lastentry = set1.getentryforindex(xmax -1); float lastopenclosemax = math.max(lastentry.getopen(), lastentry.getclose()); entries.add(new entry(linedata.getdatasetcount() +1, lastopenclosemax)); } linedataset.notifydatasetchanged(); linechart.notifydatasetchanged(); linechart.invalidate(); linechart.moveviewto(linechart.getxchartmax(), 2f, yaxis.axisdependency.right); } private void addentry(boolean start) { data = mchart.getdata(); if (set1 == null) { set1 = createset(); data.adddataset(set1); } float highmax = 1.0700f; float highlow = 1.1700f; float lowmax = 0.5700f; float lowlow = 0.63000f; int prog = 1; int xmax = (int) set1.getxmax(); candleentry lastentry = set1.getentryforindex(yvals1.size() - 1); (int = 0; < prog; i++) { float open = highlow + new random().nextfloat() * (highmax - highlow); float close = lowlow + new random().nextfloat() * (lowmax - lowlow); float lastopenclosemax = math.max(lastentry.getopen(), lastentry.getclose()); float currentopenclosemax = math.max(open, close); float currentopenclosemin = math.min(open, close); float high = open + 0.3f; float low = close - 0.3f; if (currentopenclosemax < lastopenclosemax) { yvals1.add(new candleentry(xmax + 1, high, low, currentopenclosemax, currentopenclosemin)); } else { yvals1.add(new candleentry(xmax + 1, high, low, currentopenclosemin, currentopenclosemax)); } mchart.notifydatasetchanged(); mchart.invalidate(); mchart.moveviewto(mchart.getxchartmax(), 2f, yaxis.axisdependency.right); } } private void removelastentry() { candledata data = mchart.getdata(); if (data != null) { icandledataset set = data.getdatasetbyindex(0); if (set != null) { set.removefirst(); data.notifydatachanged(); mchart.notifydatasetchanged(); mchart.invalidate(); } } linedata linedata = linechart.getdata(); if (linedata != null) { ilinedataset set = linedata.getdatasetbyindex(0); if (set != null) { set.removefirst(); data.notifydatachanged(); linechart.notifydatasetchanged(); linechart.invalidate(); } } } public string getuserinfoandsave() { usernameandscore = mainactivity.getusername(getcontext()).trim() + ": " + btncashcurrency.gettext().tostring().trim(); return usernameandscore; } private void configurehandlerwithoutremovelastentry(final int time) { handlerlast = new handler(); rlast = new runnable() { public void run() { // removelastentry(); addentry(true); handler.postdelayed(this, time); } }; handlerlast.postdelayed(rlast, time); } private void configurehandler(final int time) { handler = new handler(); r = new runnable() { public void run() { removelastentry(); addentry(true); addlineentry(); handler.postdelayed(this, time); } }; handler.postdelayed(r, time); } public void stoplast() { handlerlast.removecallbacks(rlast); } public void stop() { handler.removecallbacks(r); } private void configurechart() { mchart.getdescription().setenabled(false); mchart.getlegend().settextcolor(color.white); mchart.setmaxvisiblevaluecount(50); xaxis xaxis = mchart.getxaxis(); xaxis.setposition(xaxis.xaxisposition.bottom); xaxis.settextcolor(color.white); xaxis.setdrawgridlines(false); yaxis leftaxis = mchart.getaxisleft(); leftaxis.setlabelcount(12, false); leftaxis.setdrawgridlines(false); leftaxis.setdrawaxisline(true); leftaxis.settextcolor(color.white); yaxis rightaxis = mchart.getaxisright(); rightaxis.setenabled(false); float highmax = 1.0700f; float highlow = 1.1700f; float lowmax = 0.5700f; float lowlow = 0.63000f; int prog = 50; float last = float.negative_infinity; string date = string.valueof(android.text.format.dateformat.format("yyyy-mm-dd", new java.util.date())); (int = 0; < prog; i++) { float open = highlow + new random().nextfloat() * (highmax - highlow); float close = lowlow + new random().nextfloat() * (lowmax - lowlow); float max = math.max(open, close); if (last < max) { float tmp = open; open = close; close = tmp; } last = max; float high = open + 0.3f; float low = close - 0.3f; yvals1.add(new candleentry(i, high, low, open, close)); } set1 = new candledataset(yvals1, date); data = new candledata(set1); mchart.setdata(data); set1.setaxisdependency(yaxis.axisdependency.left); set1.setcolor(color.rgb(80, 80, 80)); set1.setincreasingcolor(color.green); set1.setincreasingpaintstyle(paint.style.fill); set1.setdecreasingcolor(color.red); set1.setdecreasingpaintstyle(paint.style.fill); set1.setneutralcolor(color.blue); set1.setbarspace(0.2f); set1.setvaluetextcolor(color.transparent); mchart.notifydatasetchanged(); mchart.animatex(4000); } private void settext() { buttoncurrency.settext("assets: \n" + buttonposition); } private void configurecolorprogresbar() { progress.getindeterminatedrawable().setcolorfilter( getresources().getcolor(r.color.white), android.graphics.porterduff.mode.src_in); } @onclick(r.id.invest_text) public void invest() { float highmax = 1.0700f; float highlow = 1.1700f; float lowmax = 0.5700f; float lowlow = 0.63000f; float open = highlow + new random().nextfloat() * (highmax - highlow); float close = lowlow + new random().nextfloat() * (lowmax - lowlow); float high = open + 0.3f; float low = close - 0.3f; if (pos == 0) { yvals1.add(new candleentry(50, high, low, open, close)); pos++; } else if (pos == 1) { yvals1.add(new candleentry(51, high, low, open, close)); pos++; } else if (pos == 2) { yvals1.add(new candleentry(52, high, low, open, close)); pos++; } else if (pos == 3) { yvals1.add(new candleentry(53, high, low, open, close)); pos++; } else if (pos == 4) { yvals1.add(new candleentry(54, high, low, open, close)); pos++; } mchart.invalidate(); } @onclick({r.id.btn_buy, r.id.btn_sell}) public void ongamebuttonsclicked() { final fragmenttransaction ft = getchildfragmentmanager().begintransaction(); ft.addtobackstack(null); string cashtext = btncashcurrency.gettext().tostring(); final int[] cash = {integer.valueof(cashtext)}; if (cash[0] <= 0) { restartgame.setvisibility(view.visible); toast.maketext(getcontext(), "your cash on -, u cant play. please restart game.", toast.length_short).show(); } else if (integer.valueof(spinnerdata.getselecteditem().tostring()) >= integer.valueof(btncashcurrency.gettext().tostring())) { toast.maketext(getcontext(), "you not have available cash, change money in invest section.", toast.length_short).show(); } else { int min = 0; int max = 2; random r = new random(); int i1 = r.nextint(max - min + 1) + min; string text = spinnerdata.getselecteditem().tostring(); final int temp = integer.parseint(text); final int temp2 = temp * 2; disableandenablebuttons(false); if (i1 == 0 || i1 == 1) { progress.setvisibility(view.visible); stop(); configurehandler(900); new handler().postdelayed(new runnable() { @override public void run() { lostfragment.show(ft, dialog_lost); cash[0] -= temp2; btncashcurrency.settext(cash[0] + ""); progress.setvisibility(view.gone); disableandenablebuttons(true); string score = "lose " + mainactivity.getusername(getcontext()).trim() + ": " + btncashcurrency.gettext().tostring().trim() + " -" + temp2; historytransaction.add(score); getuserinfoandsave(); stop(); configurehandler(5000); } }, 5000); } else { progress.setvisibility(view.visible); stop(); configurehandler(900); new handler().postdelayed(new runnable() { @override public void run() { winfragment.show(ft, dialog_win); cash[0] += temp2; btncashcurrency.settext(cash[0] + ""); progress.setvisibility(view.gone); disableandenablebuttons(true); string score = "win " + mainactivity.getusername(getcontext()).trim() + ": " + btncashcurrency.gettext().tostring().trim() + " +" + temp2; historytransaction.add(score); getuserinfoandsave(); stop(); configurehandler(5000); } }, 5000); } } } private void disableandenablebuttons(boolean on) { btnbuy.setenabled(on); btnsell.setenabled(on); } @onclick(r.id.restart_game) public void restartgame() { intent intent = new intent(getcontext(), mainactivity.class); intent.addflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); startactivity(intent); maingamefragment.leaderboarduser.add(maingamefragment.usernameandscore); ((currencyselectactivity) getcontext()).hidegamefragment(); ((currencyselectactivity) getcontext()).closecurrencyactivity(); sharedpreferences prefs = getcontext().getsharedpreferences("app.forex", context.mode_private); sharedpreferences.editor edit = prefs.edit(); set<string> set = new hashset<>(); set.add(string.valueof(maingamefragment.leaderboarduser)); edit.putstringset("user_and_score", set); historytransaction.clear(); edit.apply(); } private void configurespinnerdataandlogic() { string[] arrayspinner = new string[]{ "50", "100", "150", "200", "250", "300", "400", "500"}; arrayadapter<string> adapter = new arrayadapter<>(getcontext(), android.r.layout.simple_list_item_1, arrayspinner); spinnerdata.setadapter(adapter); spinnerdata.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { ((textview) parent.getchildat(0)).settextcolor(color.white); string text = spinnerdata.getselecteditem().tostring(); int temp = integer.parseint(text); text_profit.settext((temp * 2) + " $ " + "100%"); } @override public void onnothingselected(adapterview<?> parent) { } }); } }
aand here xml files
<com.github.mikephil.charting.charts.candlestickchart android:id="@+id/chart" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/butoncurrency" android:layout_margin="30dp" /> <com.github.mikephil.charting.charts.linechart android:id="@+id/chartline" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/butoncurrency" android:layout_margin="30dp" />
highly needed
instead of adding new entry entries array.. try using method linedata.addentry().
something :
private void addentry() { linedata data = mchart.getdata(); ilinedataset set = data.getdatasetbyindex(0); if (set == null) { set = createset(); data.adddataset(set); } data.addentry(new entry(data.getdatasetbyindex(datasetindex).getentrycount(), yvalue), datasetindex); data.notifydatachanged(); mchart.notifydatasetchanged(); }
your "datasetindex" 0 if have single dataset in linechart.
for more information, can check class
Comments
Post a Comment