MPAndroidChart中的可滚动线条图
我正在创建一个应用程序,并使用MPAndroidChart库绘制折线图。我把日期放在x轴上,数字放在y轴上。当有更多数据时,我无法水平滚动折线图。我的代码片段如下:MPAndroidChart中的可滚动线条图,android,mpandroidchart,Android,Mpandroidchart,我正在创建一个应用程序,并使用MPAndroidChart库绘制折线图。我把日期放在x轴上,数字放在y轴上。当有更多数据时,我无法水平滚动折线图。我的代码片段如下: XAxis xAxis = chart.getXAxis(); xAxis.setTextSize(8f); xAxis.setTextColor(Color.BLACK); xAxis.setDrawGridLines(false); xAxis.setPosition(xAxis.getP
XAxis xAxis = chart.getXAxis();
xAxis.setTextSize(8f);
xAxis.setTextColor(Color.BLACK);
xAxis.setDrawGridLines(false);
xAxis.setPosition(xAxis.getPosition());
xAxis.setDrawAxisLine(true);
// xAxis.setSpaceBetweenLabels(1);
xAxis.setLabelRotationAngle(-90.0f);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTextColor(Color.BLACK);
leftAxis.setDrawGridLines(true);
leftAxis.setValueFormatter(new DefaultYAxisValueFormatter(0));
YAxis rightAxis = chart.getAxisRight();
rightAxis.setTextColor(Color.BLACK);
rightAxis.setDrawGridLines(true);
rightAxis.setValueFormatter(new DefaultYAxisValueFormatter(0));
LineDataSet barDataSet1 = null;
if (valueSet1 != null && valueSet1.size() > 0) {
barDataSet1 = new LineDataSet(valueSet1, "Sended");
barDataSet1.setColor(Color.BLACK);
barDataSet1.setCircleColor(Color.BLACK);
barDataSet1.setCircleSize(2.0f);
barDataSet1.setLineWidth(1.0f);
barDataSet1.setValueTextColor(Color.BLACK);
barDataSet1.setValueTextSize(10.0f);
barDataSet1.setDrawCubic(true);
}
ArrayList dataSets = new ArrayList<>();
if (barDataSet1 != null)
dataSets.add(barDataSet1);
LineData data = new LineData(xDatelist, dataSets);
if (data != null)
chart.setData(data);
chart.setPinchZoom(true);
chart.setScrollContainer(true);
chart.setHorizontalScrollBarEnabled(true);
chart.setScaleXEnabled(true);
chart.setDescription("Send/Received files");
chart.invalidate();
XAxis XAxis=chart.getXAxis();
xAxis.setTextSize(8f);
xAxis.setTextColor(Color.BLACK);
xAxis.setDrawGridLines(false);
xAxis.setPosition(xAxis.getPosition());
xAxis.setDrawAxisLine(真);
//xAxis.setSpaceBetweenLabels(1);
X轴设定旋转角(-90.0f);
YAxis leftAxis=chart.getAxisLeft();
leftAxis.setTextColor(Color.BLACK);
leftAxis.setDrawGridLines(true);
leftAxis.setValueFormatter(新的DefaultYAxisValueFormatter(0));
YAxis rightAxis=chart.getAxisRight();
右轴。setTextColor(颜色。黑色);
rightAxis.setDrawGridLines(真);
rightAxis.setValueFormatter(新的DefaultYAxisValueFormatter(0));
LineDataSet barDataSet1=null;
如果(valueSet1!=null&&valueSet1.size()>0){
barDataSet1=新的LineDataSet(valueSet1,“已发送”);
barDataSet1.setColor(Color.BLACK);
barDataSet1.setCircleColor(颜色:黑色);
barDataSet1.setCircleSize(2.0f);
barDataSet1.设置线宽(1.0f);
barDataSet1.setValueTextColor(Color.BLACK);
barDataSet1.setValueTextSize(10.0f);
barDataSet1.setDrawCubic(真);
}
ArrayList数据集=新的ArrayList();
如果(barDataSet1!=null)
数据集.add(barDataSet1);
LineData数据=新的LineData(xDatelist,数据集);
如果(数据!=null)
图表.设置数据(数据);
chart.setPinchZoom(真);
图表.setScrollContainer(真);
chart.setHorizontalScrollBarEnabled(真);
图表.setScaleXEnabled(真);
chart.setDescription(“发送/接收文件”);
chart.invalidate();
当有更多的数据时,我仍然无法滚动图形,然后数据在x轴上被压缩。如何解决此问题?您可以将图表添加到
水平滚动视图中,使其滚动。但要做到这一点,首先需要计算图表视图所需的高度和宽度,并通过编程将其设置为chartview
在下面的代码中,我临时将其设置为一些默认值以检查滚动行为
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_above="@+id/seekBar1"
android:fillViewport="true"
android:scrollbars="horizontal"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical" >
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="1000dp"
android:layout_height="300dp"
/>
</LinearLayout>
</HorizontalScrollView>
更好的方法是对图表使用以下方法:
chart.moveViewToX()
setVisibleXRangeMaximum()这对我很有效
// we can modify viewport, Scrolling the data from right to left
// allow 30 values to be displayed at once on the x-axis not allow more value
chart.setVisibleXRangeMaximum(30);
// set the left edge of the chart to x-index 20
// moveViewToX(...) also calls invalidate()
chart.moveViewToX(20);
查看此项了解更多信息,请阅读
我希望它能帮助你 如果yAxis left和yAxis right中都有值呢?如果希望saveToGallery(filename)函数将整个图表(不仅仅是视口中的内容或可见内容)作为图像保存,则需要这样做。