谷歌图表API不解码HTML实体,虽然我传递它们解码

Google charts API doesn't decode HTML entities although I passed them decoded

本文关键字:解码 实体 API 谷歌 HTML      更新时间:2023-09-26

所以我使用的是谷歌图表API,但是当我解码HTML实体传递给API的数据,图表显示,标题仍然包含未解码的HTML实体,我看到的东西像‡而不是它所代表的符号…

下面是一个例子…我如何确保数据在图表中被解码http://i1283.photobucket.com/albums/a542/semi852/Capture_zps33daa7b6.png

<?php
session_start();
// disable any caching by the browser
header('Expires: Mon, 14 Oct 2002 05:00:00 GMT');              // Date in the past
header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT'); // always modified
header('Cache-Control: no-store, no-cache, must-revalidate');  // HTTP 1.1
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');                                    // HTTP 1.0
require_once('../config.php');
require_once '../_includes/auxiliary_functions.php';

?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Charts</title>
            <link rel="stylesheet" title="Style CSS" href="report_assets/style.css" type="text/css" media="all" />
            <link rel="stylesheet" title="Style CSS" href="report_assets/cwcalendar.css" type="text/css" media="all" />
            <link rel="stylesheet" title="Style CSS" href="report_assets/colorbox.css" type="text/css" media="all" />
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <script src="../_js/jquery-1.6.2.min.js" type="text/javascript" charset="utf-8"></script>
            <script src="../_js/timer.js" type="text/javascript" charset="utf-8"></script>
            <script type="text/javascript" src="http://www.google.com/jsapi"></script>
            <?php
            $data =  $_GET['data']; 
            $title = $_GET['title'];
            $data = strip_tags(DecodeSpecialChars(html_entity_decode($data))); 
?>
         <script type="text/javascript">
            function htmlEntities(str) {
    return String(str).replace(/&#135;/g, '‡;');
}
</script>

          <script type="text/javascript">
              google.load('visualization', '1', {packages: ['corechart']});
            //set callback
            google.setOnLoadCallback (createChart);
            //callback function
            function createChart() {
                //create data table object
                var dataTable = new google.visualization.DataTable();

               //create data table object
                var dataTableMulticolumn = new google.visualization.DataTable();

                //define columns for first example
                dataTable.addColumn('string','Question');
                dataTable.addColumn('number', 'Answers');
                //define rows of data for first example
                dataTable.addRows([<?php echo strip_tags(DecodeSpecialChars(html_entity_decode($data))); ?>]);


                //instantiate our chart objects
                var chart = new google.visualization.PieChart (document.getElementById('Chart'));
                //define options for visualization
                 var options = {width: 1550, height: 900,legendFontSize:14,fontSize:25,is3D: true, title: "<?php echo $title; ?>"};
                //draw our chart charts
                chart.draw(dataTable, options);
               //register callbacks
                google.visualization.events.addListener(eventsChart, 'onmouseover', showDetails);
                google.visualization.events.addListener(eventsChart, 'onmouseout', hideDetails);
            }
            function showDetails(e) {
                switch (e['row']) {
                    case 0: document.getElementById('details0').style.visibility='visible';
                        break;
                    case 1: document.getElementById('details1').style.visibility='visible';
                        break;
                    case 2: document.getElementById('details2').style.visibility='visible';
                        break;
                    case 3: document.getElementById('details3').style.visibility='visible';
                        break;
                }
            }

            function hideDetails(e) {
                switch (e['row']) {
                    case 0: document.getElementById('details0').style.visibility='hidden';
                        break;
                    case 1: document.getElementById('details1').style.visibility='hidden';
                        break;
                    case 2: document.getElementById('details2').style.visibility='hidden';
                        break;
                    case 3: document.getElementById('details3').style.visibility='hidden';
                        break;
                }
            }
   </script>
        <?php           
        echo "<div class='charts'>"; 
        echo "<div id='Chart'></div>";
        echo "</div>";
        ?>

如果你还在寻找答案,解决方案是使用javascript Unicode字符值(例如'u2021为‡),而不是HTML代码。