1

i have some problem to fetch data into my chart using Canvas js. So, i have result json_encode() like this :

[{"jumlah_berita":2,"nama_kategori":"Music"},{"jumlah_berita":1,"nama_kategori":"Sport"},{"jumlah_berita":1,"nama_kategori":"Agama"},{"jumlah_berita":0,"nama_kategori":"Teknologi"},{"jumlah_berita":1,"nama_kategori":"Pendidikan"},{"jumlah_berita":0,"nama_kategori":"Cinta"},{"jumlah_berita":0,"nama_kategori":"coba test"}]

And my chart like Thischartbefore

I want implement my chart Like this, how can i make this ? After Chart

It's my Code to create CHART

<script>
    window.onload = function() {
        var jumlahBerita = new CanvasJS.Chart("chartContainer2", {
            animationEnabled: true,
            theme: "light2", // "light1", "light2", "dark1", "dark2"
            title: {
                text: "Jumlah Berita(per Kategori)"
            },

            data: [{
                type: "column",
                dataPoints: [{
                        y: 3,
                        label: "< 30 Tahun"
                    },
                    {
                        y: 25,
                        label: "30-40 Tahun"
                    },
                    {
                        y: 5,
                        label: "> 40 Tahun"
                    }
                ]
            }]
        });
        jumlahBerita.render();

    }
</script>

The problem is when i'm place data json_encode() in dataPoints like this:

dataPoints:<?= $tampil_kategori ?>

My chart just render blank white

Can you help me with this ?

Thank's

If you need my Controller and Model to fetch json_encode()

Controller

$data['tampil_kategori'] = json_encode($this->home->getAllKategori(), JSON_NUMERIC_CHECK);

Model

public function getAllKategori()
    {
        $this->db->select('jumlah_berita,nama_kategori');
        return $this->db->get('tb_kategori')->result();
    }

Oh i forgot , can i'm modified the result json_encode() from :

[{"jumlah_berita":2,"nama_kategori":"Music"},{"jumlah_berita":1,"nama_kategori":"Sport"},{"jumlah_berita":1,"nama_kategori":"Agama"},{"jumlah_berita":0,"nama_kategori":"Teknologi"},{"jumlah_berita":1,"nama_kategori":"Pendidikan"},{"jumlah_berita":0,"nama_kategori":"Cinta"},{"jumlah_berita":0,"nama_kategori":"coba test"}]

To this ?

[
{1,"Pendidikan,}
{2,"Sport"},
{3,"Agama"},
]
1
  • Please, format your code and structure your question to complete it. It is too hard to read right now. You can find info and tips about it at stackoverflow.com/help/how-to-ask Commented Sep 16, 2019 at 13:11

1 Answer 1

1

Try like this.

use JSON.parse if you're passing encoded data.

var tampil_kategori = '<?php  echo $tampil_kategori; ?>';
var newdataPoints = JSON.parse(tampil_kategori);

dataPoints : newdataPoints
Sign up to request clarification or add additional context in comments.

2 Comments

Not work for me, my chart not render just blank white.
I dont know this chart plugin Canvas JS the y and label i think it's absolute / cant be modified

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.