简体   繁体   English

无法使用JSON数据作为图表输入在Codeigniter页面上显示Morris.JS图表

[英]Unable to display Morris.JS Chart on Codeigniter page using JSON Data as input for Charts

I am using the latest Codeigniter and MorrisJS. 我正在使用最新的Codeigniter和MorrisJS。 I have created a View for the Charts and passing dynamic data through JSON, but that does not seem to work. 我已经为图表创建了一个视图,并通过JSON传递了动态数据,但这似乎不起作用。 I have made sure that all the dependent JS and CSS for MorrisJS is mapped on the View. 我确保MorrisJS的所有从属JS和CSS都映射在视图上。 When I give dummy data statically into the Chart options, it generates the chart perfectly. 当我将静态数据静态地提供给Chart选项时,它会完美地生成图表。 Only does not work when the JSON data is passed. 仅在传递JSON数据时不起作用。 Please could you guide me on this, I am sure I have made a silly mistake, but am not able to figure it out. 请您对此进行指导,我确定我犯了一个愚蠢的错误,但无法弄清楚。

My View: 我的观点:

<div id="myfirstchart" style="height: 250px;"></div>
    <script>
        Morris.Bar({
            element: 'myfirstchart',
            data: <?php echo $graphData; ?>,
            xkey: 'MonthName',
            ykeys: ['totalTicket'],
            labels: ['Value']
        });
    </script>

My Controller: 我的控制器:

    public function index(){
        $x['graphData']=json_encode($result);
        $this->load->view('common/header');
        $this->load->view('common/main_top_navbar');
        $this->load->view('reports/trends');
        $this->load->view('common/footer',$x);
    }

My Model: 我的模特:

class Reports_model extends CI_Model{
    function display_monthOnMonth_records(){
        $this->db->select('MONTHNAME(CreatedOn) as MonthName, count(TicketID) as totalTicket');
        $this->db->from('TBL_tickets');
        $this->db->group_by('MonthName');
        $this->db->order_by('
            FIELD(
                MonthName,
                "January",
                "February",
                "March",
                "April",
                "May",
                "June",
                "July",
                "August",
                "September",
                "October",
                "November",
                "December"
            )
            ');
        $query=$this->db->get();
        return $query->result();
    }
}

When I run this, I see on the Chrome Browser/page Inspect(F12), that data is showing for the data input, but it is not creating the Chart on Page. 运行此命令时,我在Chrome浏览器/页面Inspect(F12)上看到该数据针对输入的数据显示,但未在页面上创建图表。

From the Browser Inspect Result: 从浏览器检查结果:

<script>

        Morris.Bar({
            element: 'myfirstchart',
            data: {"data":[{"MonthName":"January","totalTicket":"2500"},{"MonthName":"February","totalTicket":"2200"},{"MonthName":"March","totalTicket":"2350"}]},
            xkey: 'MonthName',
            ykeys: ['totalTicket'],
            labels: ['Value']
        });

</script>

Within the index() method, change : index()方法中,更改:

    $x['graphData']=json_encode($result);

into : 进入:

    $x['graphData']=json_encode($result->data);

From what I see from the output, you have extra data parent. 从输出中可以看到,您有额外的data父级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM