douqianke7467 2019-04-14 10:13
浏览 50
已采纳

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

I am using the latest Codeigniter and MorrisJS. I have created a View for the Charts and passing dynamic data through JSON, but that does not seem to work. I have made sure that all the dependent JS and CSS for MorrisJS is mapped on the View. When I give dummy data statically into the Chart options, it generates the chart perfectly. Only does not work when the JSON data is passed. 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.

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>
  • 写回答

1条回答 默认 最新

  • dongqiang2069 2019-04-14 10:56
    关注

    Within the index() method, change :

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

    into :

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

    From what I see from the output, you have extra data parent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表