Mpandroidchart: Creating A Closed Chart (circular Line Chart)
Solution 1:
The sample app on the Google Play Store gives an example of every kind of chart available in the library. Likewise, the source code is available for you to inspect and see if it has the feature you want.
Additionally, the wiki specifically says that unordered line chart entries are not supported.
Please be aware that this library does not officially support drawing LineChart data from an Entry list not sorted by the x-position of the entries in ascending manner.
That being said, if you are willing to pre-process your data you should be able to achieve what you want. You would have to take your data and extract two distinct DataSets to which you would apply the same styling. I was able to achieve something like the effect you want using this technique:
Here is the 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;
publicclassLineChartActivity4extendsDemoBase {
private LineChart mChart;
private Random rand;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rand = newRandom();
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);
YAxisleftAxis= mChart.getAxisLeft();
leftAxis.removeAllLimitLines(); // reset all limit lines to 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 = newArrayList<>();
ArrayList<Entry> bottomEntries = newArrayList<>();
for (intx=0; x <= 180; x++) {
floatval1= offset + generateValue(x, range, delta);
floatval2= offset - generateValue(x, range, delta);
topEntries.add(newEntry(x, val1));
bottomEntries.add(newEntry(x, val2));
}
LineDataSetset1= generateLineDataSet(topEntries);
LineDataSetset2= generateLineDataSet(bottomEntries);
ArrayList<ILineDataSet> dataSets = newArrayList<>();
dataSets.add(set1);
dataSets.add(set2);
LineDatadata=newLineData(dataSets);
return data;
}
privatefloatgenerateValue(int x, float range, float delta) {
floatsine= (float) Math.sin(Math.toRadians(x));
floatscaledSine= sine * range;
if (x == 0 || x == 180) {
return scaledSine;
}
else {
return scaledSine + rand.nextFloat() * delta;
}
}
@NonNullprivate LineDataSet generateLineDataSet(ArrayList<Entry> topEntries) {
LineDataSet set;
set = newLineDataSet(topEntries, "");
set.setColor(Color.BLUE);
set.setDrawCircles(false);
set.setLineWidth(4f);
set.setValueTextSize(9f);
set.setFormSize(15.f);
return set;
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
returntrue;
}
}
Post a Comment for "Mpandroidchart: Creating A Closed Chart (circular Line Chart)"