Showing posts with label Graphs. Show all posts
Showing posts with label Graphs. Show all posts

Tuesday, 30 May 2023

A Comprehensive React Native Tutorial: Building an App with User Database, Web API Integration, Charts, and Payment Integration.

React Native has emerged as a powerful framework for building cross-platform mobile applications using JavaScript and React. In this tutorial, we will explore how to create a feature-rich app using React Native that includes user database functionality, web API integration, charts for data visualization, and payment integration. By the end of this tutorial, you'll have a solid understanding of these key aspects and be equipped to develop your own robust mobile apps.

Prerequisites:

Before diving into this tutorial, you should have a basic understanding of JavaScript, React, and React Native. Additionally, familiarity with RESTful APIs and database concepts will be helpful.

Refer the below link for complete sample code:-

Download Sample Code

Let's get started with the step-by-step tutorial:

Step 1: Setting up the React Native Project

To begin, ensure that you have Node.js and npm installed on your machine. Create a new React Native project using the following command:

npx react-native init MyApp

Step 2: User Database
Implementing a user database is essential for many applications. We'll use Firebase, a cloud-based platform, to store and manage user data. Sign up for a Firebase account and create a new project.
Configure Firebase in your React Native app by adding the necessary credentials.
Next, install the Firebase SDK by running the following command within your project directory:

npm install firebase

Step 3: Web API Integration
To interact with web APIs, we'll use the popular fetch function provided by React Native. Create a new file, api.js, and define functions to handle API requests. You can make use of fetch and other JavaScript techniques to fetch data, send POST requests, etc.

Step 4: Data Visualization with Charts
React Native offers several charting libraries, such as Victory Native and React Native Charts Wrapper, that can be used to create visually appealing charts. Install a charting library of your choice and explore its documentation to learn how to create different types of charts, such as line charts, bar charts, or pie charts. Use the fetched data from the API in Step 3 to populate the charts.

Step 5: Payment Integration
To enable payment functionality, we'll integrate a payment gateway into our app. Popular options include Stripe, PayPal, or Braintree. Select a payment gateway of your choice, create an account, and follow their documentation to set up payment integration within your React Native app. Implement features like accepting payments, handling callbacks, and updating user data accordingly.

Step 6: User Interface and Navigation
Design an intuitive and user-friendly interface for your app using React Native's built-in components or third-party libraries like React Navigation. Create navigation flows between screens and ensure seamless user experience.

Step 7: Testing and Deployment
Testing your app thoroughly is crucial before deploying it to production. Utilize testing frameworks like Jest and Enzyme to write unit tests for your components and integration tests for your API calls. Once you're satisfied with the app's functionality and stability, prepare it for deployment. For Android, generate a signed APK, and for iOS, create an archive and submit it to the App Store.




Conclusion:
Congratulations! You have successfully built a feature-rich React Native app with user database functionality, web API integration, charts for data visualization, and payment integration. This tutorial has provided a comprehensive overview of each aspect, empowering you to build powerful and versatile mobile applications. Keep exploring React Native's vast ecosystem to enhance your app further and leverage its full potential. Happy coding!

Thursday, 15 June 2017

HB Blog 138: Android Progress View With Water Filling Effect.

In my previous post, I showed how we can use seekbar to create vertical seekbar. For more details you can follow my blog  HB Blog 135: Android Vertical Seekbar Tutorial. . 
In this post, I will show how to create a progress view that represents water filling effect. Here, progress can be updated using vertical bar and water can gradually filled bottom to top.

There are four custom attributes that can be set in your layout xml:
  • fillColor, color - Sets the color of the fill area. Default is Color.WHITE.
  • strokeColor, color - Sets the color of the bounding circle. Default is Color.BLACK.
  • strokeWidth, float - Sets the thickness of the bounding circle. Default is 1.0.
  • value, integer: 0-100 - Sets the value for the fill area. Default is 0.
Refer the below link for complete sample code:-

Download Sample Code

Have a look on few code snippets,

//CustomFillView.java
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.harshalbenake.fillview.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import com.harshalbenake.fillview.R;

public class CustomFillView extends View {
    public static final int MIN_VALUE = 0;
    public static final int MAX_VALUE = 100;

    private PointF center = new PointF();
    private RectF circleRect = new RectF();
    private Path segment = new Path();
    private Paint strokePaint = new Paint();
    private Paint fillPaint = new Paint();

    private int radius;

    private int fillColor;
    private int strokeColor;
    private float strokeWidth;
    private int value;

    public CustomFillView(Context context) {
        this(context, null);
    }

    public CustomFillView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.CircleFillView,
                0, 0);

        try {
            fillColor = a.getColor(R.styleable.CircleFillView_fillColor, Color.CYAN);
            strokeColor = a.getColor(R.styleable.CircleFillView_strokeColor, Color.BLACK);
            strokeWidth = a.getFloat(R.styleable.CircleFillView_strokeWidth, 1f);
            value = a.getInteger(R.styleable.CircleFillView_value, 0);
            adjustValue(value);
        } finally {
            a.recycle();
        }

        fillPaint.setColor(fillColor);
        strokePaint.setColor(strokeColor);
        strokePaint.setStrokeWidth(strokeWidth);
        strokePaint.setStyle(Paint.Style.STROKE);
    }

    public void setFillColor(int fillColor) {
        this.fillColor = fillColor;
        fillPaint.setColor(fillColor);
        invalidate();
    }

    public int getFillColor() {
        return fillColor;
    }

    public void setStrokeColor(int strokeColor) {
        this.strokeColor = strokeColor;
        strokePaint.setColor(strokeColor);
        invalidate();
    }

    public int getStrokeColor() {
        return strokeColor;
    }

    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
        strokePaint.setStrokeWidth(strokeWidth);
        invalidate();
    }

    public float getStrokeWidth() {
        return strokeWidth;
    }

    public void setValue(int value) {
        adjustValue(value);
        setPaths();

        invalidate();
    }

    public int getValue() {
        return value;
    }

    private void adjustValue(int value) {
        this.value = Math.min(MAX_VALUE, Math.max(MIN_VALUE, value));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        center.x = getWidth() / 2;
        center.y = getHeight() / 2;
        radius = Math.min(getWidth(), getHeight()) / 2 - (int) strokeWidth;
        circleRect.set(center.x - radius, center.y - radius, center.x + radius, center.y + radius);

        setPaths();
    }

    private void setPaths() {
        float y = center.y + radius - (2 * radius * value / 100 - 1);
        float x = center.x - (float) Math.sqrt(Math.pow(radius, 2) - Math.pow(y - center.y, 2));

        float angle = (float) Math.toDegrees(Math.atan((center.y - y) / (x - center.x)));
        float startAngle = 180 - angle;
        float sweepAngle = 2 * angle - 180;

        segment.rewind();
        segment.addArc(circleRect, startAngle, sweepAngle);
        segment.close();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(segment, fillPaint);
        canvas.drawCircle(center.x, center.y, radius, strokePaint);
    }
}

//MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.harshalbenake.fillview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import com.harshalbenake.fillview.views.CustomFillView;
import com.harshalbenake.fillview.views.CustomVerticalSeekbar;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomVerticalSeekbar customVerticalSeekbar = (CustomVerticalSeekbar) findViewById(R.id.customverticalseekbar);
        final CustomFillView customfillview = (CustomFillView) findViewById(R.id.customfillview);
        customVerticalSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                customfillview.setValue(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

//activity_main.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:background="@android:color/white"
    android:layout_height="match_parent">
    <com.harshalbenake.fillview.views.CustomFillView
        android:layout_width="250dp"
        android:layout_margin="16dp"
        android:id="@+id/customfillview"
       android:layout_height="match_parent" />
    <com.harshalbenake.fillview.views.CustomVerticalSeekbar
        android:layout_width="wrap_content"
        android:id="@+id/customverticalseekbar"
        android:layout_height="match_parent" />
</LinearLayout>

Monday, 15 August 2016

HB Blog 117: Android Graphical Views And Charts.

Now a days, Android has became an important market in all business sectors. We see many enterprises companies coming up with their products and business solutions. Displaying statistics and graphical representation has became a need of the application UI.

MPAndroidChart is a powerful & easy to use chart library for Android. A powerful Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, dragging and animations. It runs on API level 8 and upwards. As an additional feature, this library allows cross-platform development between Android and iOS.

We can draw below chart types,
  1.     Simple Bar Chart
  2.     Grouped Bar Chart
  3.     Horizontal Bar Chart
  4.     Simple Line Chart
  5.     Line Chart with Cubic Lines
  6.     Grouped Line Chart
  7.     Combined Line and Bar Chart
  8.     Pie Chart
  9.     Scatter Chart
  10.     Candlestick Chart
  11.     Radar Chart

Refer the below link for complete sample code:-

Download Sample Code

Have a look on few code snippets,

//build.gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"
    defaultConfig {
        applicationId "com.example.harshalbenake.graphicaldiagrams"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile files('libs/mpandroidchartlibrary-2-1-5.jar')
}

//activity_main.xml
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

//MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MainActivity extends Activity implements OnChartValueSelectedListener {
    protected BarChart mChart;
    protected String[] mMonths = new String[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mChart = (BarChart) findViewById(R.id.chart1);
        mChart.setDescription("");
        mChart.setOnChartValueSelectedListener(this);
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        setData(12, 50);
    }

    private void setData(int count, float range) {
        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            xVals.add(mMonths[i]);
        }
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 0; i < count; i++) {
            float mult = (range + 1);
            float val = (float) (Math.random() * mult);
            yVals1.add(new BarEntry(val, i));
        }
        BarDataSet set1 = new BarDataSet(yVals1, "HB");
        set1.setBarSpacePercent(35f);
        ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
        dataSets.add(set1);
        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);
        mChart.setData(data);
    }

    @Override
    public void onValueSelected(Entry entry, int i, Highlight highlight) {
        Toast.makeText(MainActivity.this,"value: "+entry.getVal(),Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onNothingSelected() {
    }
}