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

Hip hop and RnB songs to apologize to your partner

Love is a beautiful thing, but it often goes wrong. This often leads to pain, suffering and sorrow.  Being imperfect beings, hearts tend to get broken all the time regardless how hard we may try to avoid it.  The heartbreak is often inadvertent but at times we find ourselves in the wrong. An oversight, a word unsaid or even a lapse in our judgement can cause our loved ones harm. This doesn't always have to be the end though. Oftentimes, relationships can be mended by simply uttering three simple words: "I AM SORRY". This article is a collection of some of my favourite 'I'm sorry' songs. I hope you'll enjoy these apology songs, but more importantly, I hope you will get a few quotables and some wisdom nuggets from them.  The best apology however, is to change behaviour (got that from a Jay Z interview) so as you apologize, please remember that it was your actions that hurt them. The best apology is one which involves you not repeating those same mistakes aga...

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.

Php date: How to get the first and last day of the week

In this tutorial, I'll show you how to get the first and last day of the week in php. By default, in PHP the week runs from Monday to Sunday. However we'll cover two methods to get the first and last day for both those who consider Sunday or Monday as their first day of the week. We will be using 2 functions to achieve this: date() strtotime() We will use a combination of these two functions and store the result in a variable.  How to get the first day of the week If you want to get Sunday, use this method: $firstday = date('l Y-m-d', strtotime("sunday -1 week")); echo $firstday; If you want to get the date for Monday, this current week, use this method: $monday = date('l Y-m-d', strtotime("monday -1 week ")); echo $monday; How to get the last day of the week The following code will give you Saturday as the last day of the current week. $lastday = date('l Y-m-d', strtotime("saturday 0 week")); echo $last...