commit e1784bdd4ccc76cfae1cfe0ddc670cf129c10518 Author: Joshua Seigler Date: Thu Jun 30 21:01:08 2016 -0400 functioning rest-style chart server! diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8146f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# https://git-scm.com/docs/gitignore +# https://help.github.com/articles/ignoring-files +# Example .gitignore files: https://github.com/github/gitignore +/bower_components/ +/node_modules/ +/vendor/ +/.brackets.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0112a57 --- /dev/null +++ b/composer.json @@ -0,0 +1,7 @@ +{ + "require": { + "seigler/neat-charts": "@dev", + "altorouter/altorouter": "1.1.0", + "phpFastCache/phpFastCache": "^4.3" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..f182d4c --- /dev/null +++ b/composer.lock @@ -0,0 +1,187 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "1bb799d5b600f9167c5a63c390825771", + "content-hash": "8a896903dde64517a29ad409d716c428", + "packages": [ + { + "name": "altorouter/altorouter", + "version": "v1.1.0", + "source": { + "type": "git", + "url": "https://github.com/dannyvankooten/AltoRouter.git", + "reference": "09d9d946c546bae6d22a7654cdb3b825ffda54b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dannyvankooten/AltoRouter/zipball/09d9d946c546bae6d22a7654cdb3b825ffda54b4", + "reference": "09d9d946c546bae6d22a7654cdb3b825ffda54b4", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "AltoRouter.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Danny van Kooten", + "email": "dannyvankooten@gmail.com", + "homepage": "http://dannyvankooten.com/" + }, + { + "name": "Koen Punt", + "homepage": "https://github.com/koenpunt" + }, + { + "name": "niahoo", + "homepage": "https://github.com/niahoo" + } + ], + "description": "A lightning fast router for PHP", + "homepage": "https://github.com/dannyvankooten/AltoRouter", + "keywords": [ + "lightweight", + "router", + "routing" + ], + "time": "2014-04-16 09:44:40" + }, + { + "name": "phpfastcache/phpfastcache", + "version": "4.3.17", + "source": { + "type": "git", + "url": "https://github.com/PHPSocialNetwork/phpfastcache.git", + "reference": "348f765dbb426f5da1d009802ab496909e4ad691" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPSocialNetwork/phpfastcache/zipball/348f765dbb426f5da1d009802ab496909e4ad691", + "reference": "348f765dbb426f5da1d009802ab496909e4ad691", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "suggest": { + "ext-apc": "*", + "ext-mbstring": "*", + "ext-memcache": "*", + "ext-memcached": "*", + "ext-predis": "*", + "ext-redis": "*", + "ext-sqlite": "*" + }, + "type": "library", + "autoload": { + "files": [ + "src/phpFastCache/phpFastCache.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Khoa Bui", + "email": "khoaofgod@gmail.com", + "homepage": "http://www.phpfastcache.com", + "role": "Developer" + }, + { + "name": "Georges.L", + "email": "contact@geolim4.com", + "homepage": "https://github.com/Geolim4", + "role": "Developer" + } + ], + "description": "PHP Cache Class - Supported Redis, Cookie, Predis, APC, MemCached, WinCache, Files, PDO, Reduce mySQL Call by Caching", + "homepage": "http://www.phpfastcache.com", + "keywords": [ + "APC Cache", + "apc", + "cache", + "cache class", + "caching", + "cookie", + "files cache", + "memcache", + "memcached", + "mysql cache", + "pdo cache", + "php cache", + "predis", + "redis", + "wincache" + ], + "time": "2016-05-17 22:29:47" + }, + { + "name": "seigler/neat-charts", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/seigler/neat-charts.git", + "reference": "352794cbfcd9d7e4d7d6973188cf35e69d21f723" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/seigler/neat-charts/zipball/352794cbfcd9d7e4d7d6973188cf35e69d21f723", + "reference": "352794cbfcd9d7e4d7d6973188cf35e69d21f723", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "autoload": { + "psr-0": { + "NeatCharts\\LineChart": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Joshua Seigler", + "email": "joshua@seigler.net", + "homepage": "https://joshua.seigler.net/", + "role": "Developer" + } + ], + "description": "Generates clean-looking SVG charts", + "homepage": "https://github.com/seigler/neat-charts", + "keywords": [ + "chart", + "charts", + "svg" + ], + "time": "2016-06-29 18:58:29" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": { + "seigler/neat-charts": 20 + }, + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..5f12263 --- /dev/null +++ b/index.php @@ -0,0 +1,54 @@ + 'files', + 'path' => sys_get_temp_dir().'/cryptohistory-cache/' +]); + +$router = new AltoRouter(); + +$router->map('GET', '/', function() { + require __DIR__ . '/views/index.php'; +}); + +// map cryptocurrency stuff +$router->map( 'GET', '/charts/[BTC_DASH:pair]/[7d|24h:duration]/[svg|png:format]', function($pair, $duration, $format) { + require __DIR__ . '/views/chart.php'; + renderChart( + $pair, + 60 * 60 * 24 * ($duration == '7d' ? 7 : 1), + ($duration == '7d' ? 1800 : 300), + $format, + '#000', + 800, + 200, + 12 + ); + return true; +}); + +// match current request url +$match = $router->match(); + +// call closure or throw 404 status +try { + if( !$match || !is_callable( $match['target'] ) || false === call_user_func_array( $match['target'], $match['params'] )) { + // no route was matched + header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found'); + echo '

404 Not Found

Page not found

'; + } +} catch (Exception $e) { + header($_SERVER["SERVER_PROTOCOL"]." 500 Server Error", true, 500); + echo 'There was some problem generating that for you.'; + return true; +} + +?> diff --git a/views/chart.php b/views/chart.php new file mode 100644 index 0000000..afa1399 --- /dev/null +++ b/views/chart.php @@ -0,0 +1,89 @@ +date] = $item->weightedAverage; + } + + $poloniexChart = new NeatCharts\LineChart($chartData, [ + 'width'=>800, + 'height'=>200, + 'lineColor'=>"#1C75BC", // Dash blue + 'labelColor'=>"#000", + 'smoothed'=>false, + 'fontSize'=>12 + ]); + $result = $poloniexChart->render(); + + if ($format == 'png') { + $im = new Imagick(); + $im->setBackgroundColor(new ImagickPixel("transparent")); + $im->readImageBlob($result); + $im->setImageFormat("png32"); + $result = $im->getImageBlob(); + $im->clear(); + $im->destroy(); + } + CacheManager::set('poloniex-'.$pair.'-'.$dataDuration.'-'.$format, $result, $dataDuration); + $resultExpires = time() + $dataDuration; + } else { + $resultExpires = CacheManager::getInfo('poloniex-'.$pair.'-'.$dataDuration.'-'.$format)[ 'expired_time' ]; + $startTime = $resultExpires - $dataDuration; + } + + header('Expires: '.gmdate("D, d M Y H:i:s", $resultExpires)); + if ($format == 'svg') { + header('Content-type: image/svg+xml; charset=utf-8'); + header('Content-Disposition: inline; filename="Dash-chart-' . gmdate('Y-m-d\THis+0', $startTime) . '--' . gmdate('Y-m-d\THis+0') . '.svg"'); + } else if ($format == 'png') { + header("Content-Type: image/png"); + header('Content-Disposition: inline; filename="Dash-chart-' . gmdate('Y-m-d\THis+0', $startTime) . '--' . gmdate('Y-m-d\THis+0') . '.png"'); + } + echo $result; +} diff --git a/views/fakeStockData.php b/views/fakeStockData.php new file mode 100644 index 0000000..d653ef4 --- /dev/null +++ b/views/fakeStockData.php @@ -0,0 +1,23 @@ +500, + "height"=>150, + "fontSize"=>10 +]); +print $stockChart->render(); + +?> diff --git a/views/fakeTempData.php b/views/fakeTempData.php new file mode 100644 index 0000000..64c8e31 --- /dev/null +++ b/views/fakeTempData.php @@ -0,0 +1,24 @@ +700, + "height"=>400, + "lineColor"=>"#D00", + "labelColor"=>"#777", + "smoothed"=>true +]); +print $tempChart->render(); +?> diff --git a/views/index.php b/views/index.php new file mode 100644 index 0000000..8c624a0 --- /dev/null +++ b/views/index.php @@ -0,0 +1,43 @@ + + + + + + NeatCharts demo + + + + +
+

NeatCharts demo

+
+
+
+

Poloniex Dash/BTC Price

+ Poloniex Dash/BTC price +
+ +
+

Fake Stock Market Data

+
+ +
+

Monotonically Smoothed Chart

+
+
+ + +