X和Y轴打印的Android MPChart问题

时间:2015-09-18 06:42:10

标签: android charts mpandroidchart

我正在使用MPChart绘制折线图。

下面是我用来绘制图表的代码。图表工作正常。我唯一的问题是我需要在图表的左侧和底部打印(即)L形状的x和y轴。我希望X值(传递给LineData的那些)打印在图形的底部(x轴),我想设置y轴的最小值和最大值,并且应该根据这个min调整图形。最大值。

如果我取消注释第1行部分并传递true /删除第1行部分。图表变得格格不入。有人请帮帮我。

由于

heartXVals是包含heartYVals条目数据中相应心率值的日期的ArrayList。

heartDataSet = LineDataSet(heartYVals,"")

        heartDataSet!!.setLineWidth(1.75f)
        heartDataSet!!.setCircleSize(3f);
        heartDataSet!!.setColor(Color.WHITE);
        heartDataSet!!.setCircleColor(Color.WHITE);
        heartDataSet!!.setHighLightColor(Color.WHITE);
        heartDataSet!!.setDrawValues(false);

        val dataSets:ArrayList<LineDataSet> = ArrayList();
        dataSets.add(heartDataSet!!);

        val data:LineData = LineData(heartXVals,dataSets)

        val lineChart:LineChart = view.findViewById(R.id.heartChart) as LineChart

        lineChart.setDescription("")
        lineChart.setNoDataTextDescription("You need to provide data for the chart.")
        lineChart.setDrawGridBackground(false)
        lineChart.setTouchEnabled(false)
        lineChart.setDragEnabled(false)
        lineChart.setScaleEnabled(true)

        // if disabled, scaling can be done on x- and y-axis separately
        lineChart.setPinchZoom(false)

        //lineChart.setBackgroundColor(color)

        // set custom chart offsets (automatic offset calculation is hereby disabled)
        lineChart.setViewPortOffsets(10f, 0f, 10f, 0f)


        // add data
        lineChart.setData(data)

        // get the legend (only possible after setting data)
        val l = lineChart.getLegend()
        l.setEnabled(false)

        lineChart.getAxisLeft().setEnabled(false) -- Line 2

      /*  val leftAxis:YAxis = lineChart.getAxisLeft(); ---Line 1
        leftAxis.removeAllLimitLines()
        leftAxis.setAxisMaxValue(220f);
        leftAxis.setAxisMinValue(40f);
        leftAxis.setStartAtZero(false);
        leftAxis.enableGridDashedLine(0f, 0f, 0f);*/


        lineChart.getAxisRight().setEnabled(false)

        lineChart.getXAxis().setEnabled(false)
        //lineChart.getY.setEnabled(true)

        // animate calls invalidate()...
        lineChart.animateX(2500)

        lineChart.invalidate()

3 个答案:

答案 0 :(得分:3)

// - X Axis
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(tf);
xAxis.setTextSize(12f);
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTextColor(ColorTemplate.getHoloBlue());
xAxis.setEnabled(true);
xAxis.disableGridDashedLine();
xAxis.setSpaceBetweenLabels(5);
xAxis.setDrawGridLines(false);
xAxis.setAvoidFirstLastClipping(true);

// - Y Axis
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines();
leftAxis.setTypeface(tf);
leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setAxisMaxValue(1000f);
leftAxis.setAxisMinValue(0f); // to set minimum yAxis
leftAxis.setStartAtZero(false);
leftAxis.enableGridDashedLine(10f, 10f, 0f);
leftAxis.setDrawLimitLinesBehindData(true);
leftAxis.setDrawGridLines(true);
mChart.getAxisRight().setEnabled(false);


//-----------------
xAxis.setPosition(XAxisPosition.BOTTOM); --- x Axis 
leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART); --- x Axis

答案 1 :(得分:0)

添加以下行

    XAxis xAxis = lineChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // to set xAxis in Bottom

    YAxis leftAxis = lineChart.getAxisLeft();
    leftAxis.removeAllLimitLines();
    leftAxis.setAxisMaxValue(220f); // to set maximum yAxis 
    leftAxis.setAxisMinValue(0f); // to set minimum yAxis
    leftAxis.setStartAtZero(false);
    leftAxis.enableGridDashedLine(10f, 10f, 0f);

    leftAxis.setDrawLimitLinesBehindData(true);

    lineChart.getAxisRight().setEnabled(false);

    lineChart.animateX(2500, Easing.EasingOption.EaseInOutQuart);

enter image description here

答案 2 :(得分:0)

在您的代码中,x和y轴均设置为false。将这两行更改为lineChart.getAxisRight().setEnabled(true); lineChart.getXAxis().setEnabled(true);

与图例相同,将行更改为: val l = lineChart.getLegend(); l.setEnabled(true);