douliaotong4944 2019-02-02 20:06
浏览 54
已采纳

在wordpress插件中可视化图形

I'm a novice in php and WordPress. I'm trying to understand how to display a graph generated in html an JavaScript in a basic WordPress plugin.

This is the basic admin page where I would like the graph to be displayed.

<?php
/*
Plugin Name: Test plugin
Description: A test plugin to demonstrate wordpress functionality
Author: the author
Version: 0.1
*/
add_action('admin_menu', 'test_plugin_setup_menu');

function test_plugin_setup_menu(){
        add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
}

function test_init(){
        echo "<h1>Hello World!</h1>";
}

?>

For example, I will use chart.js for the plot with a file for the html and another one for the JavaScript.

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>

<canvas id="bar-chart" width="800" height="450"></canvas>

JavaScript

 // Bar chart
new Chart(document.getElementById("bar-chart"), {
    type: 'bar',
    data: {
      labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
      datasets: [
        {
          label: "Population (millions)",
          backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
          data: [2478,5267,734,784,433]
        }
      ]
    },
    options: {
      legend: { display: false },
      title: {
        display: true,
        text: 'Predicted world population (millions) in 2050'
      }
    }
});
  • 写回答

1条回答 默认 最新

  • douhe2305 2019-02-03 02:29
    关注

    This would be the simplest solution, enqueuing Chart.js and printing the script to start it inside the menu HTML output:

    add_action('admin_menu', 'test_plugin_setup_menu');
    
    function test_plugin_setup_menu(){
        add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
    }
    
    function test_init(){
        wp_enqueue_script( 'chart-js', '//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js' );
        echo "<h1>Hello World!</h1>";
        ?>
        <canvas id="bar-chart" width="800" height="450"></canvas>
        <script>
            window.onload = function(){
                new Chart(document.getElementById("bar-chart"), {
                    type: 'bar',
                    data: {
                      labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
                      datasets: [
                        {
                          label: "Population (millions)",
                          backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
                          data: [2478,5267,734,784,433]
                        }
                      ]
                    },
                    options: {
                      legend: { display: false },
                      title: {
                        display: true,
                        text: 'Predicted world population (millions) in 2050'
                      }
                    }
                });
            };
        </script>
        <?php
    }
    

    But I'd suggest doing it on a standard fashion and load Chart.js and your own JavaScript file conditionally to your menu page, as shown at How do I Enqueue styles/scripts on Certain /wp-admin Pages?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试