Skip to main content

How to create an animated Line Chart in Sketchware

 There are times when you might want to show data in visual format. This tutorial will show you how to create an animated line chart using Sketchware. The image below shows how the final result will look.


animated linechart image


Step 1.

Create a more block with a view variable named "layout", a listmap variable named "list", a number variable named "radius" and another named "margin"

Add an add source directly block in the moreblock. Inside the block place the following code:


final LinearLayout viewGroup = (LinearLayout) _layout;

        viewGroup.removeAllViews();

        viewGroup.setPadding(0,0,0,0);

        viewGroup.setOrientation(LinearLayout.HORIZONTAL);

        viewGroup.setGravity(Gravity.BOTTOM);


        viewGroup.post(new Runnable() {

            @Override

            public void run() {

                final float length = _list.size();

                final float group_height = viewGroup.getHeight();

                final float child_width = (viewGroup.getWidth())/length;


                //getProsent(child_width,100-_margin)

                for (int i = 0; i < length; i++) {

                    float getValue = Integer.parseInt(_list.get(i).get("value").toString());

                    float value = drawValueDetector(getValue,group_height);


                    LinearLayout child = new LinearLayout(_layout.getContext());

                    setLayoutParams(child,child_width,group_height,(int)_margin,1);

                    child.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);


                    LinearLayout back_view = new LinearLayout(_layout.getContext());

                    setLayoutParams(back_view,getProsent((float)child_width,(float)(100-_margin)),group_height,0,1);

                    back_view.setGravity(Gravity.BOTTOM);

                    background((View)back_view,(int)_radius,"#339e9e9e");


                    final LinearLayout fore_view = new LinearLayout(_layout.getContext());


                    //========================================================================


                    setLayoutParams(fore_view,0,0,0,1);


                    Animation ani = new ShowAnim(fore_view, (int) value/* target layout height */);

                    ani.setDuration(500/* animation time */);

                    ani.setInterpolator(new android.view.animation.DecelerateInterpolator());

                    fore_view.startAnimation(ani);


                    //========================================================================


                    if (_list.get(i).containsKey("color")){

                        background((View)fore_view,(int)_radius,_list.get(i).get("color").toString());

                    }

                    else {

                        background((View)fore_view,(int)_radius,"#9e9e9e");

                    }


                    back_view.addView(fore_view);

                    child.addView(back_view);

                    viewGroup.addView(child);


                }

            }


            private int getProsent(float value, float i) {

                return (int) (value/100*i);

            }




            int getDip(int value){

                return (int) (value*0.75);

            }


            int getPix(int value){

                return (int) (value/0.75);

            }


            private float drawValueDetector(float value, float max_value) {

                float min_value = max_value/maxValue(_list);

                return min_value*value;

            }


            private float maxValue(ArrayList<HashMap<String, Object>> list) {

                float save_max = 0;

                for (int i = 0; i < list.size(); i++) {

                    float draw = (float) Integer.parseInt(_list.get(i).get("value").toString());

                    if (save_max<draw){

                        save_max = draw;

                    }

                }

                return save_max;

            }


            private void setLayoutParams(LinearLayout view, float width,float height,int margin,int weight){

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) width,(int) height,(float) weight);

                params.setMargins(margin,0,margin,0);

                view.setLayoutParams(params);

            }


            private void background(View view,int radius,String color){

                android.graphics.drawable.GradientDrawable drawable = new android.graphics.drawable.GradientDrawable();

                drawable.setColor(android.graphics.Color.parseColor(color));

                drawable.setCornerRadius(radius);

                view.setBackground(drawable);

            }


            class ShowAnim extends android.view.animation.Animation {

                int targetHeight;

                View view;


                public ShowAnim(View view, int targetHeight) {

                    this.view = view;

                    this.targetHeight = targetHeight;

                }


                @Override

                protected void applyTransformation(float interpolatedTime, android.view.animation.Transformation t) {

                    view.getLayoutParams().height = (int) (targetHeight * interpolatedTime);

                    view.requestLayout();

                }


                @Override

                public void initialize(int width, int height, int parentWidth,

                                       int parentHeight) {

                    super.initialize(width, height, parentWidth, parentHeight);

                }


                @Override

                public boolean willChangeBounds() {

                    return true;

                }

            }


        });


SETTING UP

Once we have created our moreblock we can then use this logic where we want it to work. You can place this onButtonClick or onCreate. We can get the data from shared preferences or a preset list.... but for the sake of this example I will use the logic below:

Animated line chart logic


Once we have placed our blocks and configured our list we can now run the app.



Comments

Popular posts from this blog

How to easily create background notifications in Sketchware(original)

How to easily create background notifications in Sketchware(original) One of the keys to building a successful app is to find mechanisms that will keep your users engaged. You can do this by using background notifications. This tutorial will show you how to do that in Sketchware. We will cover: 1. How to create notifications in Sketchware 2. How to show these notifications even when the app is closed.

Happy Birthday Memes: Laugh Your Way to Someone's Heart

Happy Birthday Memes: Laugh Your Way to Someone's Heart Introduction Birthdays are a special occasion to celebrate another year of life, and what better way to do it than with a good laugh? That's where happy birthday memes come in! These hilarious images are the perfect way to put a smile on someone's face and let them know you're thinking of them on their special day. Why Memes Are the Best Birthday Greetings * Relatable:  Memes often tap into universal experiences and emotions, making them instantly relatable to a wide audience. * Quick and Easy:  Sharing a meme is a hassle-free way to send birthday wishes, perfect for those last-minute greetings. * Memorable:  A funny meme is more likely to stick in someone's mind, making your birthday wishes unforgettable. * Versatile:  There's a meme for every type of person and occasion, from silly and goofy to heartfelt and sentimental. How to Use Happy Birthday Memes * Share on Social Media:  Spread ...

Cybersecurity Essentials: How To Stay Safe On The Internet

  Hi there, my name's Likhwa and welcome to Building Africa's Next Tech Unicorn. on this platform I give a behind the scenes look at what it takes to build a startup within the African context. In today's video, I'll be doing something different- I'll be talking about how you can stay safe online. This isn't meant to scare you or get you all paranoid, but to help you be more vigilant in protecting yourself and your loved ones from cyber threats.  Most people underestimate the dangers of the internet, but the more I am exposed to it, the more I wish I didn't need it. At times I wish I could just disconnect, get rid of all my devices and not have to worry about the constant threats that come with being online. I believe some of us are fortunate by default- especially those who live in countries like my Zimbabwe- because we hardly use credit cards to transact online and even when we do, its not like we have a lot of money- otherwise we would be a hacker's g...