MPAndroidChart定义x轴上的值之间的精确/固定间隔(时间)

时间:2018-06-05 18:32:26

标签: android mpandroidchart linegraph

我在应用程序MPAndroidChart库中使用,我现在有这样的折线图:

enter image description here

在X轴上,我有几秒钟,我希望在X轴上的值之间具有相同的间隔。现在我有19:03和19:20,但我想要19:05,19:10,19:15(间隔300秒)......

我该怎么做?

现在我正在使用此功能:

 lineChart.setScaleMinima(115f, 1f);

但它并不太好。此外,我希望它从5分钟间隔变为1小时或1天。

这可能吗?

1 个答案:

答案 0 :(得分:1)

tl; dr 将X轴的最小值和最大值设置为所需间隔的倍数(例如5的倍数),并将轴粒度设置为该间隔的适当倍数(例如5)。

完整示例:以下是一个示例测试程序,该程序演示了如何设置轴范围和粒度以强制标记处于您选择的某个倍数。粒度选项设置轴的最小分辨率,并将xMin和xMax设置为该值的倍数,这意味着您的轴间距将是您选择的倍数。请注意,例如,如果您选择5分钟间隔,则可能会以10分钟间隔(或15或20)获得轴刻度,以保持适当数量的轴线。

编辑:OP询问了一个实时图形,因此我更新了该示例以实时绘制正弦波。

ggplot(data = data, aes(x=Month + pmin(Day/30, 1), y=Oxygen)) + 
  geom_point() + 
  geom_smooth(method = "loess", se=FALSE) + 
  facet_wrap( ~ Year, ncol=2) + 
  scale_x_continuous(name = "Date", breaks = 1:12, labels = month.name) + 
  # scale_x_date(date_breaks = "1 month", date_labels = "%B")  + 
  theme(
    axis.text.x=element_text(angle = 90, hjust = 1),
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.line = element_line(colour = "black")
  )

还有XML文件

package com.project.testchart;

import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
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 java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private final Handler handler = new Handler();
    private ArrayList<Entry> data = new ArrayList<>();

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                addPointToChart();
                drawChart();
                handler.postDelayed(this, 250);
            }
        }, 250);
    }

    private void addPointToChart() {

        while( data.size() > 100 ) {
            data.remove(0);
        }

        lastX += 1f;
        float y = (float)Math.sin(lastX/5f);
        data.add(new Entry(lastX,y));
    }

    private void drawChart() {

        float textSize = 20f;

        LineDataSet line = new LineDataSet(data, "Sin");
        line.setLineWidth(5);
        line.setColor(Color.BLACK);
        line.setDrawValues(false);
        line.setDrawCircles(false);
        line.setHighLightColor(Color.TRANSPARENT);

        LineData lines = new LineData();
        lines.addDataSet(line);

        LineChart chart = findViewById(R.id.test_chart);
        chart.setData(lines);

        Description desc = new Description();
        desc.setText("");
        chart.setDescription(desc);
        chart.setDrawBorders(true);

        YAxis yAxisR = chart.getAxisRight();
        yAxisR.setDrawGridLines(false);
        yAxisR.setEnabled(false);

        YAxis yAxisL = chart.getAxisLeft();
        yAxisL.setDrawGridLines(true);
        yAxisL.setDrawTopYLabelEntry(true);
        yAxisL.setTextSize(textSize);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(textSize);

        float xMin = (float)Math.floor((line.getXMin()-xInterval)/xInterval)*xInterval;
        float xMax = (float)Math.ceil((line.getXMax()+xInterval)/xInterval)*xInterval;
        float xSpan = xMax - xMin;
        float xi = xSpan / 6; // approximately 6 labels
        float xGran = Math.max(xInterval,(float)Math.round(xi/xInterval)*xInterval);

        xAxis.setGranularity(xGran);
        xAxis.setAxisMinimum(xMin);
        xAxis.setAxisMaximum(xMax);

        Legend l = chart.getLegend();
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setTextSize(textSize);

        chart.setExtraBottomOffset(10f);
        chart.setExtraRightOffset(20f);

        chart.invalidate();
    }

    private float xInterval = 5f;
    private float lastX = 0f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        float xStart = 3f;
        float xEnd = 7f;
        for(int i = 0; i < 4; ++i) {
            float x = xStart + (i/3f)*(xEnd-xStart);
            float y = (float)Math.sin(x/5f);
            data.add(new Entry(x,y));
        }
        lastX = xEnd;

        drawChart();
    }
}

一些截取不同数据范围的屏幕截图

Example screenshots