Farshid Civil
Farshid Civil

Reputation: 13

draw a line chart with mpandroid

can anyone draw these data using mpandroid?

 double[] y2={4151.07,  3375.21,    3354.56,    3333.87,    3313.15, 3146.99,   123.67, 84.89,  43.9,   0.34,   0.0};
        double[] x2={ 0.0,  1606.64,    1643.04,    1679.05,    1534.98,    1458.7, 1377.66,    1291.13,    1260.91, 1200.2, 547.3};

I wrote a code and in my android device just the first five elements of arrays are drawn! [I mean from y2[0] to y2[5] and from x2[0] to x2[5]) can anyone say me why? this is my code:

for(int i=0;i<5;i++){
            float y=(float)y2[i] ;
            float x=(float)x2[i] ;
            yVals.add(new Entry( x, y));
        }
        LineDataSet sety=new LineDataSet(yVals,"yData");
        LineData data = new LineData(sety);
        lineChart.setData(data);

           XAxis xAxis = lineChart.getXAxis();
           lineChart.getAxisRight().setAxisMaxValue(4500);
           lineChart.getAxisLeft().setAxisMaxValue(4500);
           lineChart.getAxisRight().setAxisMinValue(-20);
           lineChart.getAxisLeft().setAxisMinValue(-20);

           xAxis.setAxisMaxValue(3500);
           xAxis.setAxisMinValue(-20);
           lineChart.animateX(3000);
           lineChart.setDragEnabled(true);
           lineChart.setScaleEnabled(true);

you think I should zoom it to see other data?

Upvotes: 1

Views: 1507

Answers (1)

Madhu
Madhu

Reputation: 583

You are adding only 5 Entry objects in the for loop. Obviously your chart will show only 5 entries.

EDIT :

    double[] y2 = {4151.07, 3375.21, 3354.56, 3333.87, 3313.15, 3146.99, 123.67, 84.89, 43.9, 0.34, 0.0};
    double[] x2 = {0.0, 1606.64, 1643.04, 1679.05, 1534.98, 1458.7, 1377.66, 1291.13, 1260.91, 1200.2, 547.3};

    ArrayList<Entry> yVals1 = new ArrayList<>();
    for (int i = 0; i < y2.length; i++) {
        yVals1.add(new Entry(i, (float) y2[i]));
    }

    LineDataSet sety = new LineDataSet(yVals1, "Dataset 1");
    sety.setValueFormatter(new LineChartYValueFormatter());
    sety.setColor(ColorTemplate.MATERIAL_COLORS[2]);
    ArrayList<ILineDataSet> dataSets = new ArrayList<>();
    dataSets.add(sety);

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

    XAxis xAxis = lineChart.getXAxis();
    xAxis.setAxisMinValue(0);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setValueFormatter(new LineChartXAxisValueFormatter(x2));
    xAxis.setLabelCount(10);
    xAxis.setLabelRotationAngle(45);

    lineChart.animateX(3000);
    lineChart.setDragEnabled(true);
    lineChart.setScaleEnabled(true);

    lineChart.highlightValue(null);
    lineChart.invalidate();

LineChartXAxisValueFormatter class

public class LineChartXAxisValueFormatter implements AxisValueFormatter {

    double[] mXLabels;

    public LineChartXAxisValueFormatter(double[] xLabels) {
        mXLabels = xLabels;
    }

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

        int i=(int)value;
        return new DecimalFormat("#0.00").format(mXLabels[i]);
    }

    @Override
    public int getDecimalDigits() {
        return 0;
    }
}

LineChartYValueFormatter class

public class LineChartYValueFormatter implements ValueFormatter {
    public LineChartYValueFormatter() {
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return new DecimalFormat("#0.00").format(value);
    }
}

See the result

Upvotes: 2

Related Questions