【问题标题】:How do I include a jpgraph in tcpdf如何在 tcpdf 中包含 jpgraph
【发布时间】:2013-11-27 02:16:24
【问题描述】:

我想生成一个包含一些信息的 PDF 文件,然后将其发送到某个电子邮件地址。 PDF 中的信息将包含我使用 jpgraph 生成的 PIE 等内容。 当然,图表将是动态的,因此不能生成静态 PIE 并将其用作静态图像。对于我生成的每个 PDF,饼图都会有所不同。 但我不知道该怎么做(如何将它包含在 tcpdf 中)。 这是我的图表:

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

$sta = $_REQUEST['start'];
$sto = $_REQUEST['end'];

// Some data
$data = array($sta,$sto);
$color   = array('#2ecc71','#e74c3c');

// A new pie graph
$graph = new PieGraph(400,400,'auto');

// Don't display the border
$graph->SetFrame(false);

// Uncomment this line to add a drop shadow to the border
// $graph->SetShadow();

// Setup title
$graph->title->Set("Procentaj realizat");
$graph->title->SetFont(FF_FONT1,FS_BOLD,18);
$graph->title->SetMargin(8); // Add a little bit more margin from the top

// Create the pie plot
$p1 = new PiePlotC($data);

// Set size of pie
$p1->SetSize(0.35);

// Label font and color setup
$p1->value->SetFont(FF_FONT1,FS_BOLD,12);
$p1->value->SetColor('white');

$p1->value->Show();

// Setup the title on the center circle
$p1->midtitle->Set("Procent\nraspunsuri corecte\n83%");
$p1->midtitle->SetFont(FF_FONT1,FS_NORMAL,14);

// Set color for mid circle
$p1->SetMidColor('white');

// Use percentage values in the legends values (This is also the default)
$p1->SetLabelType(PIE_VALUE_PER);

$p1->SetSliceColors($color);

// The label array values may have printf() formatting in them. The argument to the
// form,at string will be the value of the slice (either the percetage or absolute
// depending on what was specified in the SetLabelType() above.
$lbl = array("corecte\n%.1f%%","gresite\n%.1f%%");
$p1->SetLabels($lbl);

// Uncomment this line to remove the borders around the slices
// $p1->ShowBorder(false);

// Add drop shadow to slices
$p1->SetShadow();

// Explode all slices 15 pixels
$p1->ExplodeAll(15);

// Add plot to pie graph
$graph->Add($p1);

// .. and send the image on it's marry way to the browser
$graph->Stroke();

?>

所以上面的代码在一个名为 mychart.php 的文件中。我想将这个结果包含到一个 tcpdf 文件中。我该怎么做?

我使用 jpgraph 不是强制性的。我看到谷歌图表看起来非常完美,而且看起来很容易使用。 我可以用这个:

<html>
  <head>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {packages:['gauge']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Corecte', 83]          
        ]);

        var options = {
          width: 400, height: 250,
          redFrom: 90, redTo: 100,
          yellowFrom:75, yellowTo: 90,
          minorTicks: 5
        };

        var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id='chart_div'></div>
  </body>
</html>

但是,当包含到 tcpdf 中时...他们仍然有问题... ... 那么我能做什么呢? 请帮忙。 谢谢

【问题讨论】:

    标签: php google-visualization tcpdf jpgraph


    【解决方案1】:

    我最近遇到了对嵌入在 TCPDF 中的图形的相同要求。 Click here to see my code.

    说明:

    您需要做的是使用ob_start()ob_end_clean() 捕获JPGraph 的输出,然后使用TCPDF::Image() 显示以@ 符号为前缀的图形(它告诉TCPDF 它正在处理图像数据流而不是图像文件名)。你可以see more about this in the TCPDF examples

    您可能需要使用图形尺寸以及TCPDF::setImageScale()TCPDF::setJPEGQuality() 方法来使图形呈现得更清晰。我发现 1.7 的比例值和 80 的质量值提供了很好的平衡。

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 2013-05-07
      • 2014-06-27
      • 2013-07-03
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多