WordPress 评论统计图表绘制

如题,给你的Wordpress绘制一个评论统计吧!点击这里预览。代码比较简单,见下文,转自牧羊人。代码大概的原理是:首先读取数据库,依照不同目的进行数据库查询,然后把输出的数据格式化,最后调用 Google Chart Api 绘制图形(所以你不许翻墙,不然看不到演示)。

使用方法应该不必多言,在主题目录下新建个 php 文件,填入以上代码即可,当然,相关的样式你可以随便改改。然后新建-新建页面即可。另外值得一提的是:图表有时候可能不会正确绘制或者打不开,那是因为我大天朝的长城防火墙导致的,那还烦请您欢个墙试试~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!--?php /** * @author leniy */ /* Template Name: Comments Chart Page */ ?-->
<!--?php get_header(); ?-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<p style="text-align: center;"><a title="点此查看本页如何生成,图标不可见?被墙了-_-" href="http://www.youth-daily.com/wordpress%E8%AF%84%E8%AE%BA%E7%BB%9F%E8%AE%A1%E5%9B%BE%E8%A1%A8%E7%BB%98%E5%88%B6.html" target="_blank" rel="noopener noreferrer">本站活跃用户排名</a></p>

<div id="chart_user_div" style="width: 640px; height: 400px; margin: auto;"></div>
&nbsp;
<p style="text-align: center;">评论数量图表</p>

<div id="chart_day_div" style="width: 640px; height: 500px; margin: auto;"></div>
&nbsp;
<p style="text-align: center;">评论数量图表</p>

<div id="chart_month_div" style="width: 640px; height: 500px; margin: auto;"></div>
<!--?php global $wpdb; $numbers_day = 30; $numbers_month = 9; $numbers_user = 8; $query_day ="SELECT COUNT(*) AS `cnt` , DATE_FORMAT( `comment_date` , '%Y-%m-%d' ) AS d FROM $wpdb-&gt;comments GROUP BY d ORDER BY `d` DESC LIMIT 0 , " . $numbers_day;&lt;br ?--> $query_month="SELECT COUNT(*) AS `cnt` , DATE_FORMAT( `comment_date` , '%Y-%m' ) AS d FROM $wpdb-&gt;comments GROUP BY d ORDER BY `d` DESC LIMIT 0 , " . $numbers_month;
$query_user ="
SELECT
COUNT( comment_author_email ) AS number,
comment_author_email,
comment_author
FROM (
SELECT *
FROM $wpdb-&gt;comments
LEFT OUTER JOIN $wpdb-&gt;posts
ON ( $wpdb-&gt;posts.ID = $wpdb-&gt;comments.comment_post_ID )
WHERE
comment_date &gt; date_sub( NOW(), INTERVAL 180 DAY )
AND user_id = '0'
AND comment_approved = '1'
ORDER BY comment_ID DESC
) AS tempcmt
GROUP BY comment_author_email
ORDER BY number DESC
LIMIT {$numbers_user}";
$output_day = $wpdb-&gt;get_results($query_day);
$output_month = $wpdb-&gt;get_results($query_month);
$output_user = $wpdb-&gt;get_results($query_user);
?&gt;
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart_day);
    google.setOnLoadCallback(drawChart_month);
    google.setOnLoadCallback(drawChart_user);
    function drawChart_day() {
        var data = google.visualization.arrayToDataTable([
        ['date', 'comments'],
        <?php foreach (array_reverse($output_day) as $o) {echo "['" . $o->d . "'," . $o->cnt . "],";} ?>
        ]);
        var options = {
            title: '每日评论数图'
        };
        var chart = new google.visualization.LineChart(document.getElementById('chart_day_div'));
        chart.draw(data, options);
    }
    function drawChart_month() {
        var data = google.visualization.arrayToDataTable([
        ['date', 'comments'],
        <?php foreach (array_reverse($output_month) as $o) {echo "['" . $o->d . "'," . $o->cnt . "],";} ?>
        ]);
        var options = {
            title: '每月评论数图'
        };
        var chart = new google.visualization.ColumnChart(document.getElementById('chart_month_div'));
        chart.draw(data, options);
    }
    function drawChart_user() {
        var data = google.visualization.arrayToDataTable([
        ['comment_author', 'comments'],
        <?php foreach ($output_user as $o) {echo "['" . $o->comment_author . "'," . $o->number . "],";} ?>
        ]);
        var options = {
            title: '半年内最活跃用户图'
        };
        var chart = new google.visualization.PieChart(document.getElementById('chart_user_div'));
        chart.draw(data, options);
    }
</script>

<!--?php get_footer(); ?-->

共有 0 条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注