Google Charts in WordPress

Lesezeit: 3 Minuten

Google Charts in Wordpress
Benutzer777466

Ich möchte ein Google Graph auf einer WordPress-Seite anzeigen.

Ich habe gelernt Hier dass WordPress mit Javascript in Ordnung zu sein scheint.

Also habe ich eine Seite mit einem grundlegenden Code erstellt (es ist das Beispiel von GCharts). Meine Seite sieht so aus:

    <!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

  // Load the Visualization API and the piechart package.
  google.load('visualization', '1.0', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Topping');
    data.addColumn('number', 'Slices');
    data.addRows([
      ['Mushrooms', 3],
      ['Onions', 1],
      ['Olives', 1],
      ['Zucchini', 1],
      ['Pepperoni', 2]
    ]);

    // Set chart options
    var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':400,
                   'height':300};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>
 <div id="chart_div"></div>

Leider wird auf meiner Seite nichts angezeigt.

Hat jemand eine Ahnung was los ist?

Danke

BEARBEITEN :

Ich habe herausgefunden, dass da etwas mit dem < ist[CDATA[ tag added by WordPress. in my code, this tag may be broken by a "]" Charakter, und das würde das Problem verursachen.

Hier die Firebug-Info:

syntax error


google.setOnLoadCallback(drawChart);</p>

?page_id=22 (line 88, col 43)

das

ist in meinem Code nicht vorhanden !!

Mir ist aufgefallen, dass sich der Code ändert, wenn ich auf meiner Seite auf “Aktualisieren” klicke:

<script type="text/javascript">// <![CDATA[
          // Load the Visualization API and the piechart package.
          google.load('visualization', '1.0', {'packages':['corechart']});
          // Set a callback to run when the Google Visualization API is loaded.
          google.setOnLoadCallback(drawChart);

          // Callback that creates and populates a data table,
          // instantiates the pie chart, passes in the data and
          // draws it.
          function drawChart() {

            // Create the data table.
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Topping');
            data.addColumn('number', 'Slices');
            data.addRows([
              ['Mushrooms', 3],
              ['Onions', 1],
              ['Olives', 1],
              ['Zucchini', 1],
              ['Pepperoni', 2]
            ]);

            // Set chart options
            var options = {'title':'How Much Pizza I Ate Last Night',
                           'width':400,
                           'height':300};

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, options);
          }
// ]]></script>

Und die Lösung ist … LASSEN SIE KEINE LEEREN ZEILEN im Javascript-Code auf Ihrer Seite

Wenn Sie mit ungefiltertem HTML in Ihrer WP-Installation einverstanden sind, können Sie dieses Plugin ausprobieren:

http://pp19dd.com/wordpress-plugin-include-custom-field/

Wie der Name schon sagt, können Sie Shortcodes verwenden, um benutzerdefinierte Felder in Beiträge einzufügen. Sie erstellen ein neues benutzerdefiniertes Feld, nennen es “Diagramm”, legen all das JavaScript und HTML dort ab.

Geben Sie dann in Ihrem WordPress-Beitrag einfach ein [include chart] wo immer du es willst im Text. Dieser Shortcode sollte es Ihnen ermöglichen, den nützlichen, aber wählerischen WYSIWYG-Editor und die zarten, zerbrechlichen Teile sauber zu trennen.

.

820750cookie-checkGoogle Charts in WordPress

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy