我已经实现了折线图,并且运行良好。但对于某些值,该线不可见或被切断。我附上了截图。
对于诸如 [Entry, x: 0.0 y: 0.0, Entry, x: 1.0 y: 0.0, Entry, x: 2.0 y: 0.0, Entry, x: 3.0 y: 0.0, Entry, x: 4.0 y : 1.0, Entry, x: 5.0 y: 2.0, Entry, x: 6.0 y: 3.0]
这是库的问题吗?正如我所观察到的,当值从 0 变为某个更大的值或从更大的值变为 0 时,线被切断。
LineChart lineChart = (LineChart) findViewById(R.id.chart);
lineChart.setDrawBorders(true);
lineChart.getDescription().setEnabled(false);
lineChart.fitScreen();
lineChart.setPadding(0,0,0,0);
lineChart.getLegend().setEnabled(false);
lineChart.setDoubleTapToZoomEnabled(false);
lineChart.getAxisLeft().setEnabled(false);
lineChart.getAxisRight().setEnabled(true);
lineChart.getAxisLeft().setStartAtZero(true);
lineChart.getAxisRight().setDrawAxisLine(true);
lineChart.getAxisRight().setDrawLabels(true);
lineChart.getAxisRight().setDrawGridLines(false);
lineChart.getXAxis().setEnabled(true);
lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
lineChart.getXAxis().setDrawAxisLine(true);
lineChart.getXAxis().setDrawGridLines(true);
lineChart.setScaleMinima(3f, 0f);
lineChart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer
lineChart.setDrawGridBackground(false);
lineChart.setTouchEnabled(true);
lineChart.setDragEnabled(true);
lineChart.setScaleEnabled(true);
lineChart.setPinchZoom(false);
Legend l = lineChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(true);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
ArrayList<Entry> values = new ArrayList<Entry>();
values.add(new Entry(Float.parseFloat("1"), 5));
values.add(new Entry(Float.parseFloat("2"), 2));
values.add(new Entry(Float.parseFloat("3"), 6));
values.add(new Entry(Float.parseFloat("4"), 8));
values.add(new Entry(Float.parseFloat("5"), 2));
LineDataSet d = new LineDataSet(values, "Actual kWh");
d.setMode(LineDataSet.Mode.CUBIC_BEZIER);
d.setLineWidth(1.5f);
d.setCircleRadius(3f);
d.setCircleColorHole(Color.BLACK);
d.setValueTextSize(0f);
d.setCircleColor(ContextCompat.getColor(mContext,R.color.blue_line));
d.setColor(ContextCompat.getColor(mContext,R.color.blue_line));
d.setDrawFilled(true);
d.setFillDrawable(ContextCompat.getDrawable(mContext, R.drawable.graph_fill));
dataSets.add(d);
LineData data = new LineData(dataSets);
lineChart.setData(data);
lineChart.invalidate();
最佳答案
我认为您的问题与 d.setMode(LineDataSet.Mode.CUBIC_BEZIER);
有关。
负值被您的图形截断,贝塞尔插值创建一条曲线,在 T 和 W 之间几乎没有负值。
请尝试使用不同的模式(如线性模式 d.setMode(LineDataSet.Mode.LINEAR);
)或接受负值(删除 lineChart.getAxisLeft().setStartAtZero(真);
).
更新
使用 LineDataSet.Mode.HORIZONTAL_BEZIER
模式,您可以获得所需的行为。
关于android - MPAndroidChart 线图,线在中间被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48060395/