Android(MP图表)

时间:2019-04-08 07:06:58

标签: android

`

package m.com.mygraph;

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

import com.github.mikephil.charting.charts.LineChart;
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 com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GridLabelRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.util.ArrayList;

public class SecondaryAxisActivity extends AppCompatActivity {


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



        lineChart = findViewById(R.id.lineChart);



       //Disabling the drags from both axis
        lineChart.setDragXEnabled(false);
        lineChart.setDragYEnabled(false);



        //setting the X-axis position
        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lineChart.getXAxis().setDrawLabels(true);
        lineChart.getXAxis().setAxisMaximum(10);
        lineChart.getXAxis().setAxisMinimum(0);

        //setting the draw grid lines on Xaxis to False
        lineChart.getXAxis().setDrawGridLines(false);

        //removing the Y axis from the LHS
        lineChart.getAxis(YAxis.AxisDependency.LEFT).setEnabled(false);

        //Aligning the Y-axis bottom with X-axis
        lineChart.getAxis(YAxis.AxisDependency.RIGHT).setAxisMinimum(0);






        //setting data and design for the line to be drawn on graph
        LineDataSet lineDataSet1 = new LineDataSet(dataValues1(),"Data set 1");
        lineDataSet1.setColor(Color.RED);
        lineDataSet1.setDrawCircleHole(false);
        lineDataSet1.setDrawCircles(true);
        lineDataSet1.setCircleColor(Color.RED);
        lineDataSet1.setDrawValues(true);
        lineDataSet1.setLineWidth(4);


        //setting data and design for the line to be drawn on graph
        LineDataSet lineDataSet2 = new LineDataSet(dataValues2(),"Data set 2");
        lineDataSet2.setColor(Color.GREEN);
        lineDataSet2.setDrawCircleHole(false);
        lineDataSet2.setDrawCircles(true);
        lineDataSet2.setCircleColor(Color.GREEN);
        lineDataSet2.setDrawValues(true);
        lineDataSet2.setLineWidth(4);

        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(lineDataSet1);
        dataSets.add(lineDataSet2);

        LineData data = new LineData(dataSets);
        lineChart.setData(data);
        lineChart.invalidate();

       // lineChart.setDrawGridBackground(false);

    }

    private ArrayList<Entry> dataValues1()
    {
      ArrayList<Entry> dataVals = new ArrayList<Entry>();
      dataVals.add(new Entry(1,20));
      dataVals.add(new Entry(2,24));
      dataVals.add(new Entry(3,2));
      dataVals.add(new Entry(4,10));
      dataVals.add(new Entry(5,28));

      return  dataVals;
    }

    private ArrayList<Entry> dataValues2()
    {
      ArrayList<Entry> dataVals = new ArrayList<Entry>();
      dataVals.add(new Entry(1,15));
      dataVals.add(new Entry(2,18));
      dataVals.add(new Entry(3,5));
      dataVals.add(new Entry(4,6));
      dataVals.add(new Entry(5,35));

      return  dataVals;
    }
}

`

enter image description here我想在android应用程序中创建一个图表,该图表的右侧有Y轴,并且X轴标签也从右到左对齐,但我没有卡在X轴上将标签从右到左排列,还有一个问题,如果您仔细观察,也会发现自己 Y轴零与X轴不对齐,实际上,它位于上方。

我要附上一些图片。

任何帮助或建议将不胜感激。[在此处输入链接描述] [2

2 个答案:

答案 0 :(得分:0)

我邀请您阅读base axis documentation。 如您所见,您可以使用以下功能:

  

setInverted(启用了布尔值):如果设置为true,则此轴为   倒置意味着最高值将在底部,   最低价位于顶部。

答案 1 :(得分:0)

我通过在顶部创建X轴并在左侧创建Y轴解决了这个问题。然后 使Y轴反转然后绘制图形...完成后..将整个图形旋转180度..可以使您感觉到图形的右侧是Y刻度,底部是x轴从右到左完成(如我的问题所述)。