【问题标题】:Using Highchart with Codeigniter将 Highchart 与 Codeigniter 一起使用
【发布时间】:2016-06-21 09:46:49
【问题描述】:

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

图表显示正常,但 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 月等。

请在这方面帮助我。

提前致谢,

【问题讨论】:

  • 看起来你应该改变你的系列数据数组。您可以尝试使您的数据看起来类似于:[['month1',1],['month2',1]],在您的情况下应该可以正常工作。
  • 当我使用 print json_encode($result1, JSON_NUMERIC_CHECK);然后输出为 [{"name":Category", "data":["January","February","March".."December"]}]。我只需要捕获数据部分并放入类别。在此过程中的任何帮助将不胜感激,因为我无能为力。

标签: json codeigniter highcharts


【解决方案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 轴上显示为一月、二月、三月..等。 我在包括stackoverflow在内的许多论坛中进行了搜索,但没有找到任何解决方案。

我发帖是因为它可能对将来的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多