高图改变单个类别中单个条的颜色

时间:2013-06-21 16:07:41

标签: graph colors yii highcharts

我有一个列范围图,想要定义单个条形线的颜色。如果你看到图片中的第二组。根据某种情况,我会把它改成不同的颜色。 $schedule[] = array($date_from, ( date('Y-m-d',strtotime($model['ProjectEndDate'])) > date('Y-m-d') )? $today*1000 : $date_to);如果endate不大于今天,则将颜色更改为红色。

enter image description here

我该怎么做?

视野

$('#container').highcharts({
    'chart':{
        'type':'columnrange',
        'inverted':true,
            },
    'exporting':{
        'enabled':true
        },

    'title':{
        'text':'Projects incomplete in 2013'
        },
    'xAxis':{
        'categories':<?=$cat?>
        },
    'yAxis':{
        'title':'Date',
        'type':'datetime',
        'dateTimeLabelFormats':{                
            'month':'%b'
        },
        'min':Date.UTC(2013,00,01)
    },

    'tooltip':{
        formatter: function(){  
                return '<b>' +this.series.name + ':</b> '+ Highcharts.dateFormat('%e %b, %Y', this.point.low) + ' - ' +  Highcharts.dateFormat('%e %b, %Y', this.point.high) +'<br/>' ;
              }  
        },
    'legend':{
        'enabled':false
        },
    'series':[
                {
                    'name':'Start - End',
                    'data':<?=$data?>
                    },
                    {
                    'name':'Forecast',
                    'data':<?=$schedule?>,
                    'color': 'green'    
                    },
                    {
                    'name':'Actual',
                    'data':<?=$complete?>,
                    'color': 'yellow'   
                    }
                ]

});

在我的控制器中,我有

public function actionGraph(){      

        $command = Yii::app()->db->createCommand("
                SELECT
                    view_webprojectreport.PROJECT,
                    view_webprojectreport.StartDATE,
                    view_webprojectreport.ProjectEndDate,
                    view_webprojectreport.PERCENT,
                    view_webprojectreport.ASAAREA
                FROM
                    view_webprojectreport
                WHERE
                view_webprojectreport.StartDATE >= '2013' AND
                view_webprojectreport.ProjectEndDate IS NOT NULL AND
                view_webprojectreport.PERCENT < 100
                ORDER BY
                view_webprojectreport.PERCENT DESC
                ")->queryAll();

        $series = array();
        $cat = array();
        $totalLength = array();
        $schedule = array();
        $complete = array();

        foreach ($command as $key => $model) {
            $cat[] = $model['PROJECT'];
            $date_from = (strtotime($model['StartDATE']) + 1*86400)*1000;
            $date_to = (strtotime($model['ProjectEndDate']) + 1*86400)*1000;            
            $totalLength[] = array($date_from,$date_to);            

            $today = time();
            $startdate = strtotime($model['StartDATE']); 
            $enddate = strtotime($model['ProjectEndDate']); 
            $diff_total = $enddate - $startdate;
            $diff_today = $today - $startdate;
            $percentage_date=round(($diff_today/$diff_total)*100,2);


            $duration = ( ((strtotime($model['ProjectEndDate']) + 1*86400)*1000) - ((strtotime($model['StartDATE']) + 1*86400)*1000) );         
            $burn = ((time() )*1000) - ((strtotime($model['StartDATE']) + 1*86400)*1000);
            $pBurned = $burn/$duration;
            $time = $time = strtotime( ($date_from + $pBurned) );

            //echo date('Y-m-d') . " : " . date('Y-m-d',strtotime($model['ProjectEndDate'])) . "<br>";
            // place check for calculating if project end date is in past
            $schedule[] = array($date_from, ( date('Y-m-d',strtotime($model['ProjectEndDate'])) > date('Y-m-d') )? $today*1000 : $date_to); 




            $percentage_to_get = round((float)$model['PERCENT'],2);
            $percentage_of_days =  ((int)$model['PERCENT'] == 0)? 0 : floor($diff_total/100*$percentage_to_get);
            //echo date('Y-m-d', $startdate) . " : " . date('Y-m-d', $startdate + $percentage_of_days ) . "<br>";
            //echo $startdate . " : " . ($startdate + $percentage_of_days) . "<br>";
            $percentComplete = (($startdate + $percentage_of_days)+ 1*86400)*1000;  

            $complete[] = array($date_from,$percentComplete);

        }


        $series = array("series"=>array(
                array(
                    'name'=>'Start - End',
                    'data'=>$totalLength
                    ),
                array(
                    'name'=>'Forecast',
                    'data'=>$schedule,
                    'color'=> 'green'   
                    ),
                array(
                    'name'=>'Actual',
                    'data'=>$complete,
                    'color'=> 'yellow'  
                    )
                ));

        print_r($series);
        $this->render('graph',array(                
                'cat'=>json_encode($cat),
                "data"=>json_encode($totalLength),
                "schedule"=>json_encode($schedule),
                "complete"=>json_encode($complete),
                "series"=>json_encode($series)
        ));
    }

计划的输出

[[1357689600000,1372064004000],[1360972800000,1.3686588e+12],[1359158400000,1372064004000],[1.3630464e+12,1365721200000],[1359417600000,1372064004000],[1.3709916e+12,1372064004000],[1.3686588e+12,1372064004000],[1.3681404e+12,1372064004000],[1.3699548e+12,1372064004000],[1366930800000,1372064004000]]

我如何将颜色合并到这个?

$schedule[] = array($date_from, ( date('Y-m-d',strtotime($model['ProjectEndDate'])) > date('Y-m-d') )? $today*1000 : $date_to);

我找到了http://jsfiddle.net/Q2JMF/2/并尝试实施但没有工作。图表不显示任何内容

关于jsfiddle的例子 http://jsfiddle.net/shorif2000/z4HXX/2/

1 个答案:

答案 0 :(得分:8)

你的观点应该是对象:

{
   y:10,
   color: 'red'
}

因此数据应该如下:

data:[{
   y:10,
   color: 'red'
},4,5,6,7,7]

如何准备正确的结构:

http://php.net/manual/en/function.json-encode.php