Построить гистограмму с помощью php - PullRequest
4 голосов
/ 24 апреля 2020

Я строю гистограмму, используя библиотеку jpgraph, но я нахожу способ сделать несколько изменений, таких как: (a) На оси X уже есть тексты A, B, C, D. Как добавить другой набор текстов, например, график А имеет 3 бара, и я хочу обозначить каждый столбец как 1, 2, 3 как заданный

(b) Как записать значения каждого столбца в верхней части столбца?

<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');

$data1y=array(47,80,40,116);
$data2y=array(61,30,82,105);
$data3y=array(115,50,70,93);


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

$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);

$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);

$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b2plot = new BarPlot($data2y);
$b3plot = new BarPlot($data3y);

// Create the grouped bar plot
$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot));
// ...and add it to the graPH
$graph->Add($gbplot);


$b1plot->SetColor("white");
$b1plot->SetFillColor("#cc1111");

$b2plot->SetColor("white");
$b2plot->SetFillColor("#11cccc");

$b3plot->SetColor("white");
$b3plot->SetFillColor("#1111cc");

$graph->title->Set("Bar Plots");

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

Barplot

РЕДАКТИРОВАТЬ: Я нашел решение для (б), добавив в конце кода:

$b1plot->value->Show();
$b2plot->value->Show();
$b3plot->value->Show();

Я до сих пор не нашел решения для (а).

1 Ответ

4 голосов
/ 29 апреля 2020

Вы можете установить легенду на каждом bar_plot с помощью ->legend = '1';

Затем вы можете установить макет легенды в 1 столбец и добавить правое поле на график, чтобы у легенды было место.

Вот мой рабочий код:

$data1y=array(47,80,40,116);
$data2y=array(61,30,82,105);
$data3y=array(115,50,70,93);


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

$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);

$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150, 180, 210), array(15,45,75,105,135));
$graph->SetBox(false);

$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array('A','B','C','D'));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// Create the bar plots
$b1plot = new BarPlot($data1y);
$b2plot = new BarPlot($data2y);
$b3plot = new BarPlot($data3y);

// Create the grouped bar plot
$gbplot = new GroupBarPlot(array($b1plot,$b2plot,$b3plot));
// ...and add it to the graPH
$graph->Add($gbplot);


$b1plot->SetColor("white");
$b1plot->SetFillColor("#cc1111");
$b1plot->value->Show();
$b1plot->legend = '1';

$b2plot->SetColor("white");
$b2plot->SetFillColor("#11cccc");
$b2plot->value->Show();
$b2plot->legend = '2';

$b3plot->SetColor("white");
$b3plot->SetFillColor("#1111cc");
$b3plot->value->Show();
$b3plot->legend = '3';


$graph->title->Set("Bar Plots");

$graph->SetMargin(40,80,40,40);
$graph->legend->Pos(0.05,0.5, 'right', 'center');
$graph->legend->SetColumns(1);

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

Вывод: enter image description here

...