android - MPAndroidChart: Creating a closed chart (circular line chart) -
i need draw "closed chart" in android app, this:
the xindex of data first increases decreases. in example:
[3,3],[4,4],[5,5],[6,4],[7,3],[6,2],[5,1],[4,2],[3,3]
when try drawing lineŠ”hart data in mpandroidchart, renders first half of data (prior fall of xindex). other data not shown.
how can draw data correctly mpandroidchart ?
the sample app on google play store gives example of every kind of chart available in library. likewise, source code available inspect , see if has feature want.
additionally, the wiki says unordered line chart entries not supported.
please aware library not officially support drawing linechart data entry list not sorted x-position of entries in ascending manner.
that being said, if willing pre-process data should able achieve want. have take data , extract 2 distinct datasets apply same styling. able achieve effect want using technique:
here code:
import android.graphics.color; import android.os.bundle; import android.support.annotation.nonnull; import android.view.menu; import android.view.windowmanager; import android.widget.seekbar; import android.widget.textview; import com.github.mikephil.charting.charts.linechart; import com.github.mikephil.charting.components.legend; import com.github.mikephil.charting.components.legend.legendform; import com.github.mikephil.charting.components.xaxis; import com.github.mikephil.charting.components.yaxis; import com.github.mikephil.charting.data.entry; import com.github.mikephil.charting.data.linedata; import com.github.mikephil.charting.data.linedataset; import com.github.mikephil.charting.interfaces.datasets.ilinedataset; import com.xxmassdeveloper.mpchartexample.notimportant.demobase; import java.util.arraylist; import java.util.random; public class linechartactivity4 extends demobase { private linechart mchart; private random rand; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); rand = new random(); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_linechart); mchart = (linechart) findviewbyid(r.id.chart1); mchart.setdrawgridbackground(false); mchart.getdescription().setenabled(false); mchart.settouchenabled(true); mchart.setscalexenabled(true); mchart.setscaleyenabled(true); mchart.setpinchzoom(true); mchart.getlegend().setenabled(false); yaxis leftaxis = mchart.getaxisleft(); leftaxis.removealllimitlines(); // reset limit lines avoid overlapping lines leftaxis.enablegriddashedline(10f, 10f, 0f); leftaxis.setdrawzeroline(false); leftaxis.setdrawlimitlinesbehinddata(true); mchart.getaxisright().setenabled(true); mchart.setdragoffsetx(20); mchart.setdata(generatecloseddata(90, 180, 15)); mchart.animatex(2500); } private linedata generatecloseddata(float offset, float range, float delta) { arraylist<entry> topentries = new arraylist<>(); arraylist<entry> bottomentries = new arraylist<>(); (int x = 0; x <= 180; x++) { float val1 = offset + generatevalue(x, range, delta); float val2 = offset - generatevalue(x, range, delta); topentries.add(new entry(x, val1)); bottomentries.add(new entry(x, val2)); } linedataset set1 = generatelinedataset(topentries); linedataset set2 = generatelinedataset(bottomentries); arraylist<ilinedataset> datasets = new arraylist<>(); datasets.add(set1); datasets.add(set2); linedata data = new linedata(datasets); return data; } private float generatevalue(int x, float range, float delta) { float sine = (float) math.sin(math.toradians(x)); float scaledsine = sine * range; if (x == 0 || x == 180) { return scaledsine; } else { return scaledsine + rand.nextfloat() * delta; } } @nonnull private linedataset generatelinedataset(arraylist<entry> topentries) { linedataset set; set = new linedataset(topentries, ""); set.setcolor(color.blue); set.setdrawcircles(false); set.setlinewidth(4f); set.setvaluetextsize(9f); set.setformsize(15.f); return set; } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.line, menu); return true; } }
Comments
Post a Comment