我试图使用MP安卓图表使X轴的值显示在图形的底部而不是顶部

时间:2016-12-06 16:03:07

标签: android mpandroidchart

我使用MP安卓图创建了一个图形应用程序,我用它来绘制两个不同的行数据集。但我的问题是xaxis的值标签出现在图表的顶部。我需要一种方法让它到底。下面是我的代码

final LineChart linechart = (LineChart)findViewById(R.id.idlinechart);

        //created list enties to hold the values
        List<Entry> valuesCompany1 = new ArrayList<>();  List<Entry> valuesCompany2 = new ArrayList<>();
//created entry values to be entered into the lsit entry b
        Entry c1e1 = new Entry(0f, 100000f); valuesCompany1.add(c1e1);

        Entry c1e2 = new Entry (1f, 140000f);valuesCompany1.add(c1e2);

        Entry c1e3 = new Entry(2f,90000f);valuesCompany1.add(c1e3);

        Entry c1e4 = new Entry (3f, 150000f); valuesCompany1.add(c1e4);

        Entry c2e1 = new Entry(0f, 50000f); valuesCompany2.add(c2e1);

        Entry c2e2 = new Entry (1f, 50000f);valuesCompany2.add(c2e2);

        Entry c2e3 = new Entry(2f,150000f);valuesCompany2.add(c2e3);

        Entry c2e4 = new Entry (3f, 110000f); valuesCompany2.add(c2e4);

//so now we create line data to hold the list
        LineDataSet linedatasetComp1 = new LineDataSet(valuesCompany1, "company 1");
        linedatasetComp1.setAxisDependency(YAxis.AxisDependency.LEFT);
            linedatasetComp1.setColor(Color.BLUE);
        LineDataSet linedatasetComp2 = new LineDataSet(valuesCompany2, "company 2");
        linedatasetComp2.setAxisDependency(YAxis.AxisDependency.LEFT);
            linedatasetComp2.setColor(Color.RED);

        List<ILineDataSet> iLinedata = new ArrayList<ILineDataSet>();
        iLinedata.add(linedatasetComp1); //adding the line data sets into the line data list
        iLinedata.add(linedatasetComp2);
//setting the Ilinedata list into line data
        LineData linedata = new LineData(iLinedata);

            //setting the line data into line chart
        linechart.setData(linedata);
        linechart.invalidate(); //refreshing the line chart

            //Saving the picture to galery
Button savebutton = (Button)findViewById(R.id.button);
            savebutton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            linechart.saveToGallery("new Line chart", 150);
                            Toast.makeText(getApplicationContext(), "graph saved ", Toast.LENGTH_LONG).show();
                    }
            });

            final String[] Quaters = {"Q1", "Q2", "Q3", "Q4"};

            IAxisValueFormatter valueFormater = new IAxisValueFormatter() {
                    @Override
                    public String getFormattedValue(float value, AxisBase axis) {
                            return Quaters[(int) value];

                    }

                    @Override
                    public int getDecimalDigits() {
                            return 0;
                    }
            };
            XAxis xAxis = linechart.getXAxis();
            xAxis.setGranularity(1f);
            xAxis.setValueFormatter(valueFormater);
        Description desc = new Description();
        desc.setText("A graph comparing revenuews of two companies ");
        desc.setEnabled(true);
        desc.setPosition(0,0);
        desc.setTextColor(Color.BLACK);
       linechart.setDescription(desc);

2 个答案:

答案 0 :(得分:4)

我不知道LineChart是否有这个选项,但是如果它确实你需要让XAxis像这样调整位置:

linechart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

希望它有所帮助!

答案 1 :(得分:0)

使用MPAndroid图表版本2.2.4或更高版本并使用代码

 mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

如Ponchotg所说