如果我只想显示要在Android折线图中mpAndroidchart的X轴上显示的值怎么办

时间:2018-08-08 08:14:50

标签: java android graph mpandroidchart

这是我项目的当前状态

enter image description here

我想更改此 enter image description here

这是我的代码

  

Blockquote

public class MainActivity extends AppCompatActivity {

LineChart mChart;

private static final float MAX_BP_VALUE = 220f;
private static final float MAX_BT_VALUE = 42f;
private static final float UPPER_LIMIT = 130.0f;
private static final float LOWER_LIMIT = 50.0f;

private static SimpleDateFormat mFormat = new SimpleDateFormat("yyyy:mm:dd");

IAxisValueFormatter xAxisFormatter;

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

    mFormat.setTimeZone(TimeZone.getDefault());

    mChart = (LineChart) findViewById(R.id.chart);

    xAxisFormatter = new IAxisValueFormatter() {

        public int getDecimalDigits() {
            return 0;
        }

        @Override
        public String getFormattedValue(float value, AxisBase axis) {

            return mFormat.format(new Date((long) value));
        }
    };


    setupChart();
    setupAxes();
    setupData();
    setLegend();

    Button drawButton = (Button) findViewById(R.id.drawButton);
    drawButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            LineData data = mChart.getData();
            LineDataSet dataset = BPcreateSet();

            try {

                dataset.addEntry(new Entry(new Long(mFormat.parse("2018:05:11").getTime()).floatValue(), 110));
                dataset.addEntry(new Entry(new Long(mFormat.parse("2018:06:13").getTime()).floatValue(), 150));
                dataset.addEntry(new Entry(new Long(mFormat.parse("2018:07:15").getTime()).floatValue(), 120));
                dataset.addEntry(new Entry(new Long(mFormat.parse("2018:08:17").getTime()).floatValue(), 80));
                dataset.addEntry(new Entry(new Long(mFormat.parse("2018:09:18").getTime()).floatValue(), 40));

            } catch(Exception e) {
                e.printStackTrace();
            }


            ArrayList<Integer> colorList = new ArrayList<Integer>();
            colorList.add(Color.WHITE);
            colorList.add(Color.RED);
            colorList.add(Color.WHITE);
            colorList.add(Color.WHITE);
            colorList.add(Color.BLUE);
            dataset.setCircleColors(colorList);

            data.addDataSet(dataset);

            mChart.setData(data);

            data.notifyDataChanged();
            mChart.notifyDataSetChanged();

            mChart.animateX(1000);

        }
    });

}

private void setupChart() {
    mChart.getDescription().setEnabled(false);
    mChart.setTouchEnabled(true);
    mChart.setPinchZoom(true);
    mChart.setScaleEnabled(true);
    mChart.setDrawGridBackground(false);
    mChart.setBackgroundColor(Color.BLACK);
    mChart.setMarker(new XYMarkerView(this, xAxisFormatter));
    mChart.setDrawMarkers(true);

}

private void setupAxes() {

    XAxis xl = mChart.getXAxis();
    xl.setPosition(XAxis.XAxisPosition.BOTTOM);
    xl.setTextColor(Color.WHITE);
    xl.setDrawGridLines(false);
    xl.setAvoidFirstLastClipping(true);
    xl.setEnabled(true);
    xl.setLabelRotationAngle(-45.0f);
    xl.setGranularityEnabled(true);
    xl.setGranularity(1.0f/6.0f); // per 10 minutes
    xl.setDrawGridLines(true);
    xl.setAxisLineWidth(2f);
    xl.setAxisLineColor(Color.WHITE);
    xl.setValueFormatter(xAxisFormatter);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTextColor(Color.WHITE);
    leftAxis.setAxisMaximum(MAX_BP_VALUE);
    leftAxis.setAxisMinimum(20f);
    leftAxis.setGranularity(20f);
    leftAxis.setAxisLineWidth(2f);
    leftAxis.setAxisLineColor(Color.WHITE);
    leftAxis.setLabelCount(10);
    leftAxis.setDrawGridLines(true);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setTextColor(Color.WHITE);
    rightAxis.setAxisMaximum(MAX_BT_VALUE);
    rightAxis.setAxisMinimum(32f);
    rightAxis.setGranularity(2f);
    rightAxis.setAxisLineWidth(2f);
    rightAxis.setAxisLineColor(Color.WHITE);
    rightAxis.setDrawGridLines(false);
    xl.setSpaceMin(50);

    LimitLine ul = new LimitLine(UPPER_LIMIT, "Upper Limit");
    ul.setLineWidth(2f);
    ul.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
    ul.setTextSize(10f);
    ul.setTextColor(Color.WHITE);

    LimitLine ll = new LimitLine(LOWER_LIMIT, "Lower Limit");
    ll.setLineColor(Color.BLUE);
    ll.setLineWidth(2f);

    ll.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);
    ll.setTextSize(10f);
    ll.setTextColor(Color.WHITE);

    LimitLine llI = new LimitLine(400f, "체온");
    llI.setLineColor(Color.BLUE);
    llI.setLineWidth(2f);
    llI.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);

    leftAxis.removeAllLimitLines();
    leftAxis.addLimitLine(ul);
    leftAxis.addLimitLine(ll);
    xl.addLimitLine(llI);


    leftAxis.setDrawLimitLinesBehindData(true);

}

private void setupData() {
    LineData data = new LineData();
    data.setValueTextColor(Color.WHITE);

    // add empty data
    mChart.setData(data);
}

private void setLegend() {
    Legend l = mChart.getLegend();

    l.setForm(Legend.LegendForm.CIRCLE);
    l.setTextColor(Color.WHITE);
}

private LineDataSet BPcreateSet() {
    LineDataSet set = new LineDataSet(null, "pulse");
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    set.setColors(Color.argb(255, 0, 255, 0));

    set.setLineWidth(3f);
    set.setCircleRadius(6f);
    set.setValueTextColor(Color.WHITE);
    set.setValueTextSize(10f);

    set.setDrawValues(false);


    return set;
}

}

我要像上图那样标记X轴。

请原谅我,第一个和最后一个值是空的,因为我在Photoshop中很差。

有解决方案吗?

您还可以推荐另一个代表时间值并指示X轴所需值的库

0 个答案:

没有答案