如何动态显示“ Id”明智的数据“图表”?

时间:2019-09-15 06:29:16

标签: javascript php google-visualization codeigniter-3

我正在研究包含图表的调查脚本。当我提交一个问题的评分时,它工作得很好。但是现在我想显示多个问题等级和问题ID的基数,我想显示图表。

#Controller
public function getBarChart() {
        $data = $this->question_model->getChart();
        echo json_encode($data);
    }


#model
 public function getChart() {
        $query = "SELECT b.label, count(a.rating) as rating_label from rating_point a
    left join rating_label b ON a.rating=b.id group by a.rating order by b.label asc";
        return $this->db->query($query)->result();
        return $query->result();
    }

#view
<div class="content-wrapper">

    <h1 align="center">Bar Chart</h1> 

    <div style="padding:100px;">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>

        <canvas id="popChart" width="55%" height="20%" style="margin-top: 10px;"></canvas>


        <script>

            var ctx = document.getElementById("popChart").getContext("2d");

            var data_label = [];
            var data_rating = [];

            $.post("<?= base_url('index.php/question/getBarChart') ?>",
                    function (data) {
                        var obj = JSON.parse(data);
                        $.each(obj, function (test, item) {
                            data_label.push(item.label);
                            data_rating.push(item.rating_label);
                        });


                        var lineChart = new Chart(ctx, {
                            type: "bar",
                            data: {
                                labels: data_label,
                                datasets: [{
                                        label: "Value",
                                        data: data_rating,
                                        backgroundColor: 'rgba(64, 217, 8, 0.7)',
                                        borderColor: "rgba(17, 28, 238, 0.55)",
                                        pointBorderColor: "rgba(38, 185, 154, 0.7)",
                                        pointHoverBackgroundColor: "#fff",
                                        pointHoverBorderColor: "rgba(220,220,220,10)",
                                        PointBorderWidth: 1
                                    }]
                            },
                            options: {
                                scales: {
                                    yAxes: [{
                                            ticks: {
                                                beginAtZero: true
                                            }
                                        }]
                                }
                            }
                        });

                    });
        </script>

    </div>

</div>

表1(rating_label)
列:

  • id
  • 标签

表1(rating_point)
列:

  • id
  • question_id
  • 评分
  • 反馈

表:
table

图表视图:
chart view

现在,我想显示question_id的图表基础。我该怎么办?

0 个答案:

没有答案