简体   繁体   English

MPAndroidChart 中的可滚动折线图

[英]Scrollable LineChart in MPAndroidChart

I am creating an application and use the MPAndroidChart library to draw line chart.我正在创建一个应用程序并使用 MPAndroidChart 库来绘制折线图。 I am putting Dates on x-axis and number on y-axis.我将日期放在 x 轴上,将数字放在 y 轴上。 I am not able to scroll line chart horizontally when there are more data.当有更多数据时,我无法水平滚动折线图。 My snippet is here:我的片段在这里:

    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();

Still I am not able to scroll graph when there is more data then date gets compressed on x-axis.当有更多数据然后日期在 x 轴上被压缩时,我仍然无法滚动图形。 How can I solve this issue?我该如何解决这个问题?

You can add your chart inside HorizontalScrollView to make it scroll.您可以在HorizontalScrollView添加图表以使其滚动。 But to do so, you first need to calculate your chart view's desired height and width and programatically set it to chartview.但要这样做,您首先需要计算图表视图所需的高度和宽度,并以编程方式将其设置为图表视图。

In the below code, I have temporary set it to some default value to check scrolling behaviour.在下面的代码中,我临时将其设置为某个默认值以检查滚动行为。

<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>

A better approach would be to use the following methods with your chart:更好的方法是对图表使用以下方法:

chart.moveViewToX() chart.moveViewToX()

chart.setVisibleXRangeMaximum() chart.setVisibleXRangeMaximum()

This worked for me.这对我有用。

// 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); 

Check this for more information read this检查此了解更多信息阅读

I hope it'll help you....!我希望它会帮助你......!

chart.setData(...); chart.setData(...); // first set the data. // 首先设置数据。

// now modify viewport chart.setVisibleXRangeMaximum(7); // 现在修改视口 chart.setVisibleXRangeMaximum(7); // allow 7 values to be displayed at once on the x-axis, not more. // 允许在 x 轴上一次显示 7 个值,而不是更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM