phpjpgraph

Displaying Percentage and Absolute value in jpgraph PiePlot


I've created a basic PiePlot chart using JPGraph, and I would like to have the Absolute value displayed alongside with the Percentage. I saw a deprecated function that actually allowed to change the value separately, but I've yet to find a solution that fits my need.

The documentation mentions a deprecated function that may have been what I needed.

I have tried calling SetLabelType() after showing the legend, but it still affects all values.

<?php
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_pie.php');
require_once ('jpgraph/jpgraph_pie3d.php');

$data_json          = '{"product_a":2,"product_b":3,"product_c":1,"product_d":1}';
$data_json          = json_decode($data_json,true);
$products           = [];
$products_clicks    = [];

foreach($data_json as $product => $clicks) {
    $products[]         = $product." (%.1f%%)";
    $products_clicks[]  = $clicks;
}

$graph          = new PieGraph(500,250,'auto');
$theme_class    = new VividTheme;

$graph->SetTheme($theme_class);
$graph->title->Set("Product Clicks");
$graph->SetBox(false);

$graph->SetMargin(40,40,40,70);
$p1 = new PiePlot3D($products_clicks);
$graph->Add($p1);
$p1->ShowBorder();
$p1->SetColor('black');

$p1->SetLabelType(PIE_VALUE_PER);  
$p1->SetLegends($products);

$p1->SetTheme("sand");
$p1->SetAngle(25);

$graph->Stroke();

While setting SetLabelType() to PIE_VALUE_PER shows percentage, it shows the percentage both on the PiePlot and the Legend.

I want to have the absolute value to be displayed in the Legend and the percentage to be displayed on the PiePlot.

If anyone ever dealt with the library is such a way, please lay me a sample of how should I tackle this problem I've had.


Solution

  • Change

    $p1->SetLegends($products);
    

    to

    $p1->SetLegends($products_clicks);
    

    That will show the actual number of clicks in the legend and leave the percentages on the chart itself.