Home > Backend Development > PHP Problem > How to solve php jpgraph garbled problem

How to solve php jpgraph garbled problem

藏色散人
Release: 2023-03-04 22:00:02
Original
2360 people have browsed it

php jpgraph garbled solution: 1. Modify the font of the title to solve the title garbled problem; 2. Modify the "jpgraph_legend.inc.php" file and set the relevant attributes to public.

How to solve php jpgraph garbled problem

# Recommended: "

PHP Video Tutorial

Recently, due to project needs, a JpGraph plug-in in php was used to solve drawing problems. During this period, I encountered many problems, specifically:

1. How to use the composer package manager to install and load.
2. How to solve the problem of garbled Chinese characters in drawings (title and Legend)

composer is very convenient to install JpGraph. I use the version "jpgraph/jpgraph":"4.0.2".

Execute
composer update to install

Usage issues:

JpGraph::load(); JpGraph::module('line'); <br><br> You need to execute the load() function first. The load() function will include the main function. When drawing the line chart, you need to introduce another file. jpgraph_line.php, at this time you need to use the
JpGraph::module('line') method to introduce the function, and no error will be reported. The php7 version will have a warning prompt for the construction method, and the configuration error prompt level will be ignored.

class JpGraph {
    static  $loaded = false ;
    static  $modules = array();
    static function load(){
        if(self::$loaded !== true){
            include_once __DIR__.&#39;/jpgraph/src/jpgraph.php&#39;;
            self::$loaded = true ;
        }
    }
    static function module($moduleName){
        self::load();
        if(!in_array($moduleName,self::$modules)){
            $path = __DIR__.&#39;/jpgraph/src/jpgraph_&#39;.$moduleName.&#39;.php&#39; ;
            if(file_exists($path)){
                include_once $path ;
            }else{
                throw new ModuleNotFoundException(&#39;The JpGraphs\&#39;s module "&#39;.$moduleName.&#39;" does not exist&#39;);
            }
        }
    }
}
Copy after login

Garbled code problem

The title and lenged logo will be garbled when displayed. I checked many blog solutions, and most of them solve the title garbled problem by modifying the font of the title.

$graph->title->SetFont(FF_SIMSUN,FS_BOLD); Can solve the garbled problem of the title:

How to solve php jpgraph garbled problem

legend solution is to modify the underlying code of composer.

Modified the jpgraph_legend.inc.php file.
class Legend There is such a sentence in the class

private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;

##private $font_family=FF_SIMSUN, $font_style=FS_NORMAL,$font_size=8;

The latest version of the existing code has set this attribute to public, You can change the font through the

$graph->legend->font_family = FF_SIMSUN;
statementSet font command Just execute it before the image is output;

$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
       $graph->legend->font_family = FF_SIMSUN;

       $image_file = $path;
       return $graph->Stroke($image_file);
Copy after login

can completely solve the problem of garbled Chinese fonts without modifying the source code.

Pay special attention here to place the font file under the How to solve php jpgraph garbled problem/usr/share/fonts/truetype
path. You need to put these two files: simhei.ttf
, simsun.ttc

# #

The above is the detailed content of How to solve php jpgraph garbled problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template