【问题标题】:jpgraph different color for each bar based on its valuejpgraph 根据每个条的值不同的颜色
【发布时间】:2016-12-17 13:34:21
【问题描述】:

我是使用 jpgraph 的新手,我有一个条形图,我希望它根据每个条的值具有不同的颜色,

所以在百分比的情况下,当柱值为< 80时,我希望它是一个红柱,当它是>= 80 && <85时,我希望它是一个黄柱 strong> 当它是>= 85 时,一个绿条

我所能做的就是为图中的所有条形提供相似的颜色

这里是代码,如果你能帮我添加条件格式,请帮忙!

 $datay=array(90,82,70,30,100,85);

// Create the graph. These two calls are always required
$graph = new Graph(300,200,"auto");    
$graph->SetScale("textlin");

// Add a drop shadow
$graph->SetShadow();

// Adjust the margin a bit to make more room for titles
$graph->img->SetMargin(40,30,20,40);

// Create a bar pot
$bplot = new BarPlot($datay);

// Adjust fill color

$graph->Add($bplot);
$bplot->value->Show();

/* I tried to add if statement here but the pic won't render */
$bplot->SetFillColor('orange');

// Display the graph
$graph->Stroke();

【问题讨论】:

    标签: php graph colors jpgraph


    【解决方案1】:

    您可以将每个条形的颜色放入一个数组中,并通过检查每个数据值来分配所需的颜色:

    $datay=array(90,82,70,30,100,85);
    $barcolors = array();
    
    // Create the graph. These two calls are always required
    $graph = new Graph(300,200,"auto");    
    $graph->SetScale("textlin");
    
    // Add a drop shadow
    $graph->SetShadow();
    
    // Adjust the margin a bit to make more room for titles
    $graph->img->SetMargin(40,30,20,40);
    
    // Create a bar pot
    $bplot = new BarPlot($datay);
    
    // Adjust fill color
    
    $graph->Add($bplot);
    $bplot->value->Show();
    
    foreach ($datay as $datayvalue) {
        if ($datayvalue < '80') $barcolors[]='red';
        elseif ($datayvalue >= '80' && $datayvalue < '85') $barcolors[]='yellow';
        elseif ($datayvalue >= '85') $barcolors[]='green';
    }
    
    $bplot->SetFillColor($barcolors);
    
    // Display the graph
    $graph->Stroke();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2020-04-08
      • 2014-10-25
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多