How to solve the Chinese garbled characters in JpGraph

Source: Internet
Author: User

Why does JpGraph contain Chinese garbled characters?

In JpGraph, the character string is converted to utf8 by default. However, if your file is utf8 and you want to use a Chinese font, it will be converted again and the result will be converted again, garbled characters appear. As shown in the following figure:

How to solve Chinese garbled characters in JpGraph

The previous code snippet is as follows:

The code is as follows: Copy code

// Set the title font and size of the chart.
$ Graph-> title-> Set ("Accumulated bar plots ");
$ Graph-> xaxis-> title-> Set ("X-title ");
$ Graph-> yaxis-> title-> Set ("Y-title ");

// Set the font and size of the title corresponding to the title above
$ Graph-> title-> SetFont (FF_FONT1, FS_BOLD );
$ Graph-> yaxis-> title-> SetFont (FF_FONT1, FS_BOLD );
$ Graph-> xaxis-> title-> SetFont (FF_FONT1, FS_BOLD );

Change it

// Set the title font and size of the chart.
$ Graph-> title-> Set (iconv ("UTF-8", "GB2312 // IGNORE", "blog info statistical table "));
$ Graph-> xaxis-> title-> Set (iconv ("UTF-8", "GB2312 // IGNORE", "X-title "));
$ Graph-> yaxis-> title-> Set (iconv ("UTF-8", "GB2312 // IGNORE", "Y-title "));

// Set the font and size of the title corresponding to the title above
$ Graph-> title-> SetFont (FF_SIMSUN, FS_BOLD );
$ Graph-> yaxis-> title-> SetFont (FF_SIMSUN, FS_BOLD );
$ Graph-> xaxis-> title-> SetFont (FF_SIMSUN, FS_BOLD );

Using the php function from the UTF-8 to GB2312 according to the Chinese language, remember iconv due to a bug, iconv in conversion character "-" to gb2312 error, therefore, add "// IGNORE" after the code to be converted ".

FF_SIMSUN indicates simplified Chinese and the corresponding font file is simsun. ttc. Although FF_CHINESE and FF_BIG5 also indicate Chinese, their corresponding font files are different, so do not make a mistake.

The following figure is generated after correct conversion.

The complete code for debugging in this example is as follows:

The code is as follows: Copy code

Require_once ('jpgraph/jpgraph. Php ');
Require_once ('jpgraph/jpgraph_bar.php ');

$ Data1y = array );
$ Data2y = array );

// Create the graph. These two cballs are always required
$ Graph = new Graph (500,400 );
$ Graph-> SetScale ("textlin ");

$ Graph-> SetShadow ();
$ Graph-> img-> SetMargin (,); // you can specify the margins of an image.

// Create the bar plots
$ B1plot = new BarPlot ($ data1y );
$ B1plot-> SetFillColor ("orange ");
$ B1plot-> value-> Show ();
$ B2plot = new BarPlot ($ data2y );
$ B2plot-> SetFillColor ("blue ");
$ B2plot-> value-> Show ();

// Create the grouped bar plot
$ Gbplot = new AccBarPlot (array ($ b1plot, $ b2plot ));

//... And add it to the graPH
$ Graph-> Add ($ gbplot );
// Set the title font style
$ Graph-> title-> Set (iconv ("UTF-8", "GB2312 // IGNORE", "blog info statistical table "));
$ Graph-> xaxis-> title-> Set (iconv ("UTF-8", "GB2312 // IGNORE", "X-title "));
$ Graph-> yaxis-> title-> Set (iconv ("UTF-8", "GB2312 // IGNORE", "Y-title "));

$ Graph-> title-> SetFont (FF_SIMSUN, FS_BOLD );
$ Graph-> yaxis-> title-> SetFont (FF_SIMSUN, FS_BOLD );
$ Graph-> xaxis-> title-> SetFont (FF_SIMSUN, FS_BOLD );

$ Graph-> Stroke ();

Of course, I only introduced one method here, and another method is to modify the source code, but it is not recommended. I think modifying the source code may cause unexpected troubles elsewhere.

To use JpGraph, you must know the information about its version, running server, and operating system.

Now, there are so many Introduction to JpGraph.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.