使用Highchart和Codeigniter

时间:2016-06-21 09:46:50

标签: json codeigniter highcharts

我正在使用Highchart和Codeigniter。我正在显示不同月份的项目请求。

图表显示正常,但x轴值显示的是通用值(0,1,2 ...)而不是月份名称。这是我的控制器代码:

public function data()
{

$data = $this->project->get_data();

$ab = array();
$category['name'] = 'Category';

$series1 = array();
$series1['name'] = 'WordPress';

$series2 = array();
$series2['name'] = 'Code Igniter';

$series3 = array();
$series3['name'] = 'Highcharts';

foreach ($data as $row)
{
$category['data'][] = $row->month;
$series1['data'][] = $row->wordpress;
$series2['data'][] = $row->codeigniter;
$series3['data'][] = $row->highcharts;
}

$result = array();
$result1 = array();
array_push($result1,$category);
array_push($result,$series1);
array_push($result,$series2);
array_push($result,$series3);

$this->view_data['ctg'] = json_encode($result1, JSON_NUMERIC_CHECK);
$this->view_data['series_data'] = json_encode($result, JSON_NUMERIC_CHECK);
$this->load->view('chart_high', $this->view_data);
}

以下是chart_high视图:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Line Chart</title>
<script type="text/javascript"      src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var test =
$(function () { 
                $('#container').highcharts({
       chart: {
           renderTo: 'container',
           type: 'column',
           marginRight: 130,
           marginBottom: 25
       },
       title: {
           text: 'Project Requests',
           x: -20 //center
       },
       subtitle: {
           text: '',
           x: -20
       },
       xAxis: {
           categories: []
       },
       yAxis: {
           title: {
               text: 'Requests'
           },
           plotLines: [{
               value: 0,
               width: 1,
               color: '#808080'
           }]
       },
       tooltip: {
           formatter: function() {
                   return '<b>'+ this.series.name +'</b><br/>'+
                   this.x +': '+ this.y;
           }
       },
      credits: { enabled: false },
       legend: {
           layout: 'vertical',
           align: 'right',
           verticalAlign: 'top',
           x: -10,
           y: 100,
           borderWidth: 0
       },

       series: <?php echo $series_data ?>
    });
            });

</script>
<script type="text/javascript" language="javascript" src="<?php echo base_url(); ?>js/highcharts.js"></script>
<script type="text/javascript" language="javascript" src="<?php echo base_url(); ?>js/exporting.js"></script>

</head>
<body>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto">     </div>
</body>
</html>

如果我将类别硬编码为[“January”,“February”],那么x轴值将显示为1月,2月等,就像我想要的那样。

请帮助我。

提前致谢,

1 个答案:

答案 0 :(得分:1)

我使用以下代码解决了它:

    $result = array();

    array_push($result,$series1);
    array_push($result,$series2);
    array_push($result,$series3);

    $ctg=$category['month']; 

    $this->view_data['month'] = json_encode($ctg, JSON_NUMERIC_CHECK);

然后在chart_high视图中,我使用了

xAxis: {
           categories: <?php echo $month ; ?>
       },

现在,月份以x轴显示为1月,2月,3月......等等。 我在许多论坛中搜索过,包括stackoverflow,但没有找到任何解决方案。

我发帖,因为它可能在将来帮助某人。