Page 1 of 1

XY chart in Android

Posted: Thu Dec 22, 2011 9:22 pm
by 16961123
Hi,

I want to draw an XY chart.

I used the following code but I haven't get the awaited result.

Code: Select all

public class XY_ChartActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout layout = (LinearLayout) findViewById(R.id.chartPanel);
        TChart chart = new TChart(this);
        chart.getGraphics3D().getAspect().setView3D(false);
        Line series = new Line();
        series.add(12.0,1.1);
        series.add(14.0,1.3);
        series.add(16.0,1.4);
        series.add(20.0,1.6);
        series.add(24.0,2.0);
        series.add(26.0,2.3);
        series.add(25.0,2.4);
        series.add(23.0,2.6);
        series.add(19.0,2.1);
        chart.addSeries(series);
        layout.addView(chart);
    }
}
Below there is the image of the chart that I got:
Image

Below there is instead the image of the chart that I want:
Image

Did I use a wrong series? Can you help me?

Thanks you!

Re: XY chart in Android

Posted: Fri Dec 23, 2011 8:04 am
by narcis
Hi Simone,

No, you didn't use the wrong series but by default their X values are sorted ascendingly. To get the plot you want you should set XValues.Order to none before populating series:

Code: Select all

        Line series = new Line(tChart1.getChart());

        series.getXValues().setOrder(ValueListOrder.NONE);

        series.add(12.0,1.1);
        series.add(14.0,1.3);
        series.add(16.0,1.4);
        series.add(20.0,1.6);
        series.add(24.0,2.0);
        series.add(26.0,2.3);
        series.add(25.0,2.4);
        series.add(23.0,2.6);