MPAndroidChart:创建闭合图表(圆形折线图)
我需要在我的Android应用程序中绘制一个“封闭图表”,如下所示: 数据的xIndex先增大后减小。如本例所示:MPAndroidChart:创建闭合图表(圆形折线图),android,mpandroidchart,Android,Mpandroidchart,我需要在我的Android应用程序中绘制一个“封闭图表”,如下所示: 数据的xIndex先增大后减小。如本例所示: [3,3],[4,4],[5,5],[6,4],[7,3],[6,2],[5,1],[4,2],[3,3] 当我尝试在MPAndroidChart中用该数据绘制一条线Сhart时,它只呈现数据的前半部分(在xIndex下降之前)。其他数据未显示 如何使用MPAndroidChart正确绘制这些数据?给出了库中可用的各种图表的示例。同样,您也可以检查并查看它是否具有所需的功能 此
[3,3],[4,4],[5,5],[6,4],[7,3],[6,2],[5,1],[4,2],[3,3]
当我尝试在MPAndroidChart中用该数据绘制一条线Сhart时,它只呈现数据的前半部分(在xIndex下降之前)。其他数据未显示
如何使用MPAndroidChart正确绘制这些数据?给出了库中可用的各种图表的示例。同样,您也可以检查并查看它是否具有所需的功能
此外,特别指出不支持无序的折线图条目
请注意,此库不正式支持条目列表中的图形折线图数据,该条目列表未按条目的x位置升序排序
这就是说,如果您愿意预处理您的数据,您应该能够实现您想要的。您必须获取数据并提取两个不同的数据集,然后应用相同的样式。使用此技术,我可以达到您想要的效果:
代码如下:
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 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 = new ArrayList<>();
ArrayList<Entry> bottomEntries = new ArrayList<>();
for (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;
}
}
导入android.graphics.Color;
导入android.os.Bundle;
导入android.support.annotation.NonNull;
导入android.view.Menu;
导入android.view.WindowManager;
导入android.widget.SeekBar;
导入android.widget.TextView;
导入com.github.mikephil.charting.charts.LineChart;
导入com.github.mikephil.charting.components.Legend;
导入com.github.mikephil.charting.components.Legend.LegendForm;
导入com.github.mikephil.charting.components.XAxis;
导入com.github.mikephil.charting.components.YAxis;
导入com.github.mikephil.charting.data.Entry;
导入com.github.mikephil.charting.data.LineData;
导入com.github.mikephil.charting.data.LineDataSet;
导入com.github.mikephil.charting.interfaces.dataset.ILineDataSet;
导入com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
导入java.util.ArrayList;
导入java.util.Random;
公共类LineChartActivity4扩展了DemoBase{
私人线形图麦克哈特;
私有随机兰德;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
rand=新随机数();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.activity\u线条图);
mChart=(线形图)findViewById(R.id.chart1);
mChart.setDrawGridBackground(假);
mChart.getDescription().setEnabled(false);
mChart.setTouchEnabled(真);
mChart.setScaleXEnabled(真);
mChart.setScaleYEnabled(真);
mChart.setPinchZoom(真);
mChart.getLegend().setEnabled(false);
YAxis leftAxis=mChart.getAxisLeft();
leftAxis.removeAllLimitLines();//重置所有限制线以避免重叠线
leftAxis.enableGridDashedLine(10f、10f、0f);
leftAxis.setDrawZeroLine(false);
leftAxis.setDrawLimitLinesBehindData(真);
mChart.getAxisRight().setEnabled(true);
mChart.setDragOffsetX(20);
设置数据(generateClosedData(90、180、15));
麦克哈特动画公司(2500);
}
专用线数据生成器闭合数据(浮动偏移、浮动范围、浮动增量){
ArrayList topEntries=新的ArrayList();
ArrayList bottomEntries=新的ArrayList();
对于(intx=0;x),我以同样的方式决定,但我认为有一个正确的解决方案。感谢您的帮助answer@gearquicker别担心,你的解决方案是正确的!