• We just launched and are currently in beta. Join us as we build and grow the community.

Pie/Doughnut Chart using ChartJS, AngularJS and PHP/MySQLi

mickel2000

SEO Keyword Architect
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
65
Likes
115
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Getting Started

I've used CDN for Bootstrap, Angular JS and Chart JS so you need internet connection for them to work.

Creating our Database

First, we're gonna create our MySQL Database where we get our data.

1. Open phpMyAdmin.
2. Click databases, create a database and name it as angular.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

  1. CREATE

    TABLE

    `fruits`

    (
  2. `fruitid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `fruitname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  4. PRIMARY KEY

    (

    `fruitid`

    )
  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  6. CREATE

    TABLE

    `sales`

    (
  7. `saleid`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  8. `fruitid`

    int

    (

    11

    )

    NOT

    NULL

    ,
  9. `amount`

    double

    NOT

    NULL

    ,
  10. PRIMARY KEY

    (

    `saleid`

    )
  11. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  1. INSERT

    INTO

    `fruits`

    (

    `fruitid`

    ,

    `fruitname`

    )

    VALUES


  2. (

    1

    ,

    'Apple'

    )

    ,
  3. (

    2

    ,

    'Orange'

    )

    ,
  4. (

    3

    ,

    'Strawberry'

    )

    ,
  5. (

    4

    ,

    'Mango'

    )

    ;

database_6_56.png

index.html

This is our index which contains our add form to update our chart and the chart itself.

  1. <!DOCTYPE html>
  2. <html

    ng-app=

    "app"

    >
  3. <head

    >
  4. <title

    >

    Pie/Doughnut Chart using ChartJS, AngularJS and PHP/MySQLi</

    title

    >
  5. <meta

    charset

    =

    "utf-8"

    >
  6. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  7. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"

    ></

    script

    >
  8. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"

    ></

    script

    >
  9. <style

    type

    =

    "text/css"

    >
  10. canvas{
  11. margin:auto;
  12. }
  13. .alert{
  14. margin-top:20px;
  15. }
  16. </

    style

    >
  17. </

    head

    >
  18. <body

    ng-controller=

    "myCtrl"

    >
  19. <div

    class

    =

    "container"

    >
  20. <div

    class

    =

    "row"

    >
  21. <div

    class

    =

    "col-sm-3 col-md-offset-1"

    ng-init=

    "fetchfruit()"

    >
  22. <h3

    class

    =

    "page-header text-center"

    >

    Add Purchase</

    h3

    >
  23. <div

    class

    =

    "form-group"

    >
  24. <label

    >

    Select Fruit:</

    label

    >
  25. <select

    ng-model=

    "buy.fruitid"

    class

    =

    "form-control"

    >
  26. <option

    ng-repeat=

    "fruit in fruits"

    value

    =

    "{{fruit.fruitid}}"

    >

    {{fruit.fruitname}}</

    option

    >
  27. </

    select

    >
  28. </

    div

    >
  29. <div

    class

    =

    "form-group"

    >
  30. <label

    >

    Amount:</

    label

    >
  31. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "buy.amount"

    >
  32. </

    div

    >
  33. <button

    type

    =

    "button"

    ng-click=

    "purchase()"

    class

    =

    "btn btn-primary"

    >

    Buy</

    button

    >
  34. <div

    class

    =

    "alert alert-success text-center"

    ng-show=

    "success"

    >
  35. <button

    type

    =

    "button"

    class

    =

    "close"

    aria-hidden=

    "true"

    ng-click=

    "clear()"

    >

    &times;

    </

    button

    >
  36. {{ message }}
  37. </

    div

    >
  38. <div

    class

    =

    "alert alert-danger text-center"

    ng-show=

    "error"

    >
  39. <button

    type

    =

    "button"

    class

    =

    "close"

    aria-hidden=

    "true"

    ng-click=

    "clear()"

    >

    &times;

    </

    button

    >
  40. {{ message }}
  41. </

    div

    >
  42. </

    div

    >
  43. <div

    class

    =

    "col-sm-7"

    ng-init=

    "fetchsales()"

    >
  44. <h3

    class

    =

    "page-header text-center"

    >

    Sales Chart</

    h3

    >
  45. <canvas id

    =

    "dvCanvas"

    height

    =

    "400"

    width

    =

    "400"

    ></

    canvas>
  46. </

    div

    >
  47. </

    div

    >
  48. </

    div

    >
  49. <script

    src

    =

    "app.js"

    ></

    script

    >
  50. </

    body

    >
  51. </

    html

    >

app.js

This contains our angular js scripts.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    ]

    )

    ;

  2. app.controller

    (

    'myCtrl'

    ,

    function

    (

    $scope,

    $http)

    {

  3. $scope.error

    =

    false

    ;
  4. $scope.success

    =

    false

    ;

  5. $scope.fetchfruit

    =

    function

    (

    )

    {
  6. $http.get

    (

    'fetchfruit.php'

    )

    .success

    (

    function

    (

    data)

    {
  7. $scope.fruits

    =

    data;
  8. }

    )

    ;
  9. }

  10. $scope.purchase

    =

    function

    (

    )

    {
  11. $http.post

    (

    'purchase.php'

    ,

    $scope.buy

    )
  12. .success

    (

    function

    (

    data)

    {
  13. if

    (

    data.error

    )

    {
  14. $scope.error

    =

    true

    ;
  15. $scope.success

    =

    false

    ;
  16. $scope.message

    =

    data.message

    ;
  17. }
  18. else

    {
  19. $scope.success

    =

    true

    ;
  20. $scope.error

    =

    false

    ;
  21. $scope.message

    =

    data.message

    ;
  22. $scope.fetchsales

    (

    )

    ;
  23. $scope.buy

    =

    ''

    ;
  24. }
  25. }

    )

    ;
  26. }

  27. //this fetches the data for our table
  28. $scope.fetchsales

    =

    function

    (

    )

    {
  29. $http.get

    (

    'fetchsales.php'

    )

    .success

    (

    function

    (

    data)

    {
  30. var

    ctx =

    document.getElementById

    (

    "dvCanvas"

    )

    .getContext

    (

    '2d'

    )

    ;
  31. var

    myChart =

    new

    Chart(

    ctx,

    {
  32. type:

    'pie'

    ,

    // change the value of pie to doughtnut for doughnut chart
  33. data:

    {
  34. datasets:

    [

    {
  35. data:

    data.total

    ,
  36. backgroundColor:

    [

    'blue'

    ,

    'green'

    ,

    'red'

    ,

    'yellow'

    ]
  37. }

    ]

    ,
  38. labels:

    data.fruitname
  39. }

    ,
  40. options:

    {
  41. responsive:

    false
  42. }
  43. }

    )

    ;

  44. }

    )

    ;
  45. }

  46. $scope.clear

    =

    function

    (

    )

    {
  47. $scope.error

    =

    false

    ;
  48. $scope.success

    =

    false

    ;
  49. }

  50. }

    )

    ;

fetchfruit.php

This is our PHP api that fetches data for our add form.

  1. <?php

  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "angular"

    )

    ;

  3. $out

    =

    array

    (

    )

    ;

  4. $sql

    =

    "SELECT * FROM fruits"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $out

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $out

    )

    ;

  10. ?>

purchase.php

This is our PHP api/code that adds data into our database.

  1. <?php

  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "angular"

    )

    ;

  3. $out

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  4. $data

    =

    json_decode

    (

    file_get_contents

    (

    "php://input"

    )

    )

    ;

  5. $fruitid

    =

    $data

    ->

    fruitid

    ;
  6. $amount

    =

    $data

    ->

    amount

    ;

  7. $sql

    =

    "INSERT INTO sales (fruitid, amount) VALUES ('$fruitid

    ', '$amount

    ')"

    ;
  8. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  9. if

    (

    $query

    )

    {
  10. $out

    [

    'message'

    ]

    =

    "Purchase added successfully"

    ;
  11. }
  12. else

    {
  13. $out

    [

    'error'

    ]

    =

    true

    ;
  14. $out

    [

    'message'

    ]

    =

    "Cannot add purchase"

    ;
  15. }

  16. echo

    json_encode

    (

    $out

    )

    ;

  17. ?>

fetchsales.php

Lastly, this is our PHP api that fetches data for our chart.

  1. <?php

  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "angular"

    )

    ;

  3. $out

    =

    array

    (

    )

    ;

  4. $sql

    =

    "SELECT *, sum(amount) AS total FROM sales LEFT JOIN fruits ON fruits.fruitid=sales.fruitid GROUP BY sales.fruitid"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $out

    [

    'total'

    ]

    [

    ]

    =

    $row

    [

    'total'

    ]

    ;
  8. $out

    [

    'fruitname'

    ]

    [

    ]

    =

    $row

    [

    'fruitname'

    ]

    ;
  9. }

  10. echo

    json_encode

    (

    $out

    )

    ;

  11. ?>

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

334,167

334,175

Top