Skip to content

Commit dd36e84

Browse files
committed
Add simple theme functionality
1 parent 43e28f9 commit dd36e84

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

index.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
<?php
22

3-
require __DIR__ . '/libs/parse_results.php';
4-
require __DIR__ . '/libs/make_graph.php';
3+
Parse_Results: {
4+
require __DIR__ . '/libs/parse_results.php';
5+
$results = parse_results(__DIR__ . '/output/results.hello_world.log');
6+
}
7+
8+
Load_Theme: {
9+
$theme = isset($_GET['theme']) ? $_GET['theme'] : 'default';
10+
if (! ctype_alnum($theme)) {
11+
exit('Invalid theme');
12+
}
513

6-
$results = parse_results(__DIR__ . '/output/results.hello_world.log');
14+
if ($theme === 'default') {
15+
require __DIR__ . '/libs/make_graph.php';
16+
} else {
17+
$file = __DIR__ . '/libs/' . $theme . '/make_graph.php';
18+
if (is_readable($file)) {
19+
require $file;
20+
} else {
21+
require __DIR__ . '/libs/make_graph.php';
22+
}
23+
}
24+
}
725

826
// RPS Benchmark
927
list($chart_rpm, $div_rpm) = make_graph('rps', 'Throughput', 'requests per second');

libs/mruz/make_chart_parts.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @copyright (c) 2013, Kenji Suzuki, Kenichi Ando, Naoaki Yamada, Yoshiyuki Yamamoto, Yuta Sakurai, Hitoshi Asano
4+
* @license https://github.com/php-recipe/php-recipe-2nd/blob/master/LICENSE BSD-3-Clause
5+
* @link https://github.com/php-recipe/php-recipe-2nd/blob/master/htdocs/php-recipe/04/06/make_chart_parts.php
6+
*/
7+
8+
# グラフを描画するJavaScriptの関数とグラフを表示させる<div>タグを
9+
# 生成するユーザー定義関数を定義します。
10+
function makeChartParts($data, $options, $type)
11+
{
12+
// JavaScriptの関数名、<div>タブのIDが
13+
// 重複しないようにするための連番☆レシピ037☆(静的変数とは?)
14+
static $index = 1;
15+
16+
// グラフの種類からAPIロード時の「packages」を確定し、APIロードを生成
17+
$package = 'corechart';
18+
$special_type = array('GeoChart', 'AnnotatedTimeLine','TreeMap', 'OrgChart',
19+
'Gauge', 'Table', 'TimeLine', 'GeoMap', 'MotionChart');
20+
if (in_array($type, $special_type)) {
21+
$package = strtolower($type);
22+
}
23+
$load = 'google.load("visualization", "1", {packages:["' . $package . '"]});';
24+
25+
// データとオプションをJSON形式へ
26+
$jsData = json_encode($data);
27+
$jsonOptions = json_encode($options);
28+
29+
// グラフを描画するJavaScript関数を生成
30+
$chart = <<<CHART_FUNC
31+
{$load}
32+
google.setOnLoadCallback(drawChart{$index});
33+
function drawChart{$index}() {
34+
var data = {$jsData};
35+
var chartData = new google.visualization.arrayToDataTable(data);
36+
var view = new google.visualization.DataView(chartData);
37+
view.setColumns([0, 1,
38+
{ calc: "stringify",
39+
sourceColumn: 1,
40+
type: "string",
41+
role: "annotation" },
42+
2]);
43+
var options = {$jsonOptions};
44+
var chartDiv = document.getElementById('chart{$index}');
45+
var chart = new google.visualization.{$type}(chartDiv);
46+
chart.draw(view, options);
47+
}\n
48+
CHART_FUNC;
49+
50+
// グラフを表示する<div>タグを生成
51+
$div = '<div id="chart' . $index . '"></div>';
52+
53+
$index++; // 連番を1加算しておく
54+
return array($chart, $div);
55+
}
56+
/* ?>終了タグ省略 ☆レシピ001☆(サーバーのPHP情報を知りたい) */

libs/mruz/make_graph.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
require __DIR__ . '/make_chart_parts.php';
4+
5+
function make_graph($id, $title, $hAxis_title)
6+
{
7+
global $results;
8+
9+
$barColors = array(
10+
'DarkBlue', 'DarkCyan', 'DarkGoldenRod', 'DarkGray', 'DarkGreen',
11+
'DarkKhaki', 'DarkMagenta', 'DarkOliveGreen', 'DarkOrange', 'DarkOrchid',
12+
'DarkRed', 'DarkSalmon', 'DarkSeaGreen', 'DarkSlateBlue', 'DarkSlateGray',
13+
'DarkBlue', 'DarkCyan', 'DarkGoldenRod', 'DarkGray', 'DarkGreen',
14+
'DarkKhaki', 'DarkMagenta', 'DarkOliveGreen', 'DarkOrange', 'DarkOrchid',
15+
'DarkRed', 'DarkSalmon', 'DarkSeaGreen', 'DarkSlateBlue', 'DarkSlateGray',
16+
);
17+
$graphWidth = 1000;
18+
$graphHeight = 400;
19+
20+
$data = array();
21+
$data[] = array('', $id, array('role' => 'style')); // header
22+
23+
$colors = $barColors;
24+
foreach ($results as $fw => $result) {
25+
$data[] = array($fw, $result[$id], array_shift($colors));
26+
}
27+
//var_dump($data); exit;
28+
29+
$options = array(
30+
'title' => $title,
31+
'titleTextStyle' => array('fontSize' => 16),
32+
'hAxis' => array('title' => $hAxis_title,
33+
'titleTextStyle' => array('bold' => true)),
34+
'vAxis' => array('minValue' => 0, 'maxValue' => 0.01),
35+
'width' => $graphWidth,
36+
'height' => $graphHeight,
37+
'bar' => array('groupWidth' => '90%'),
38+
'legend' => array('position' => 'none')
39+
);
40+
$type = 'ColumnChart';
41+
return makeChartParts($data, $options, $type);
42+
}

0 commit comments

Comments
 (0)