【问题标题】:Jpgraph wont change colors on my barplotJpgraph 不会在我的条形图上改变颜色
【发布时间】:2011-10-22 19:31:23
【问题描述】:

我刚刚开始使用 jpgraph 和 XAMPP,我正在寻找制作条形图。 当我在网上阅读一些文章时,它说您可以将一组颜色传递给 setfillcolor ,以便每个条都具有不同的颜色。但是,每当我传递它的颜色时,图表都不会改变其默认的浅蓝色颜色。

图表会响应数据的变化,但不会响应颜色的变化。 我尝试了一个累积的条形图示例,它似乎是颜色响应的。

我不确定如何诊断此类问题。请帮忙!

下面的代码(Jpgraph 文档中的示例,颜色从橙色变为#B0C4DE [原始示例中的橙色不显示]):

<?php // content="text/plain; charset=utf-8"
require_once(dirname(__FILE__)."\lib\jpgraph-3.5.0b1\src\jpgraph.php");
require_once(dirname(__FILE__)."\lib\jpgraph-3.5.0b1\src\jpgraph_line.php");
require_once(dirname(__FILE__)."\lib\jpgraph-3.5.0b1\src\jpgraph_bar.php");

$datay=array(2,3,5,25,15,6,3);
$datax=array('Jan','Feb','Mar','Apr','May','Jun','Jul');

// Size of graph
$width=400;
$height=500;

// Set the basic parameters of the graph
$graph = new Graph($width,$height,'auto');
$graph->SetScale('textlin');

// Rotate graph 90 degrees and set margin
$graph->Set90AndMargin(50,20,50,30);

// Nice shadow
$graph->SetShadow();

// Setup title
$graph->title->Set('Horizontal bar graph ex 1');
$graph->title->SetFont(FF_VERDANA,FS_BOLD,14);

// Setup X-axis
$graph->xaxis->SetTickLabels($datax);
$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,12);

// Some extra margin looks nicer
$graph->xaxis->SetLabelMargin(10);

// Label align for X-axis
$graph->xaxis->SetLabelAlign('right','center');

// Add some grace to y-axis so the bars doesn't go
// all the way to the end of the plot area
$graph->yaxis->scale->SetGrace(20);

// We don't want to display Y-axis
$graph->yaxis->Hide();

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

$bplot->SetFillColor("#B0C4DE");
//  ALSO tried:
//  $bplot->SetColor(array("red","green","blue","gray"));
$bplot->SetShadow();

//You can change the width of the bars if you like
//$bplot->SetWidth(0.5);

// We want to display the value of each bar at the top
$bplot->value->Show();
$bplot->value->SetFont(FF_ARIAL,FS_BOLD,12);
$bplot->value->SetAlign('left','center');
$bplot->value->SetColor('black','darkred');
$bplot->value->SetFormat('%.1f mkr');

// Add the bar to the graph
$graph->Add($bplot);

// .. and stroke the graph
$graph->Stroke();
?>

【问题讨论】:

  • 我也有同样的问题。没有考虑与条形格式有关的任何内容(值、颜色、...)。但它对线图很好

标签: php jpgraph


【解决方案1】:

我有同样的问题,我找到的解决方案是在 BarPlot 类初始化之后定义格式

$bplot = new BarPlot($datay);
$graph->Add($bplot);
$bplot->value->Show();
$bplot->value->SetFont(FF_ARIAL,FS_BOLD,12);
$bplot->value->SetAlign('left','center');
$bplot->value->SetColor('black','darkred');
$bplot->value->SetFormat('%.1f mkr');

找到解决方案here

【讨论】:

  • 我对 LinePlot 类也有同样的问题,这似乎也解决了该类的问题。
  • 有没有办法不仅设置颜色值,还设置条本身的颜色。那么负条的颜色与正条的颜色不同?
【解决方案2】:

我有同样的问题,这是我的解决方案:只需添加“ClearTheme()”,如下所示:

     $graph = new Graph(310,200); 
     $graph->ClearTheme();    

【讨论】:

    【解决方案3】:

    这是由于 Add() 使用主题默认设置。有两种解决方案

    1. 在应用设置之前添加绘图
    2. $graph-&gt;SetScale() 之后删除主题$graph-&gt;graph_theme = null; 或调整主题

    来源:/docs/chunkhtml/ch29.html - Part VII. Theme Class

    我最近对此进行了深入研究,因为我的图例也没有正确显示 JpGraph: How to control x/y offset, margins and color in v3.5.0b1 when using AccBarPlot?

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多