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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度