0

I am using Android Plot chart Library bar rendering chart in my application. I want to apply the transition animation when charts rendered.

E.g: Bar plots are raising smoothly from 0 to the current value in the chart.

Help me to achieve this.

Thanks in advance.

Prakash M
  • 651
  • 3
  • 13

1 Answers1

1

Keshaw is correct - there are no "built in" animation effects but because the library is designed for realtime display, it's not difficult to create your own effects. Here's functioning example thrown together on top of the SimpleXYPlot example in the DemoApp: (The AnimatedSeries implementation is the piece you'll be interested in)

/**
 * A straightforward example of using AndroidPlot to plot some data.
 */
public class SimpleXYPlotActivity extends Activity
{

    private XYPlot plot;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.simple_xy_plot_example);

        // initialize our XYPlot reference:
        plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

        // Create a couple arrays of y-values to plot:
        Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
        Number[] series2Numbers = {4, 6, 3, 8, 2, 10};

        // Turn the above arrays into XYSeries':
        XYSeries series1 = new SimpleXYSeries(
                Arrays.asList(series1Numbers),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                "Series1");                             // Set the display title of the series

        // same as above
        XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");

        // Create a formatter to use for drawing a series using LineAndPointRenderer
        // and configure it from xml:
        LineAndPointFormatter series1Format = new LineAndPointFormatter();
        series1Format.setPointLabelFormatter(new PointLabelFormatter());
        series1Format.configure(getApplicationContext(),
                R.xml.line_point_formatter_with_plf1);

        AnimatedSeries as1 = new AnimatedSeries(plot, series1);

        // add a new series' to the xyplot:
        plot.addSeries(as1, series1Format);

        // same as above:
        LineAndPointFormatter series2Format = new LineAndPointFormatter();
        series2Format.setPointLabelFormatter(new PointLabelFormatter());
        series2Format.configure(getApplicationContext(),
                R.xml.line_point_formatter_with_plf2);
        plot.addSeries(series2, series2Format);

        // reduce the number of range labels
        plot.setTicksPerRangeLabel(3);
        plot.getGraphWidget().setDomainLabelOrientation(-45);

        new Thread(as1).start();
    }

    /**
     * A primitive example of applying an animation to a series
     */
    class AnimatedSeries implements XYSeries, Runnable {

        private final XYPlot plot;
        private final XYSeries series;
        private int step = 0;
        private int stepCount = 25;
        private float factor = 0;

        public AnimatedSeries(XYPlot plot, XYSeries series) {
            this.plot = plot;
            this.series = series;
        }

        @Override
        public void run() {
            try {
                while (step < stepCount) {
                    factor = step / (float) stepCount;
                    Thread.sleep(50);
                    plot.redraw();
                    step++;
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public int size() {
            return series.size();
        }

        @Override
        public Number getX(int index) {
            return index;
        }

        @Override
        public Number getY(int index) {
            return series.getY(index).doubleValue() * factor;
        }

        @Override
        public String getTitle() {
            return series.getTitle();
        }
    }
}
Nick
  • 8,181
  • 4
  • 38
  • 63