mirror of
https://github.com/seigler/cryptohistory.org
synced 2025-07-27 01:36:11 +00:00
added candlesticks
This commit is contained in:
parent
efc5816507
commit
9cf422cdc1
3 changed files with 71 additions and 37 deletions
|
@ -18,7 +18,7 @@ $router->map('GET', '/', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
// map cryptocurrency stuff
|
// map cryptocurrency stuff
|
||||||
$router->map( 'GET', '/charts/[dark|light|sparkline:theme]/[a:curA]-[btc:curB]/[a:duration]/[svg|png:format]', function($theme, $curA, $curB, $duration, $format) {
|
$router->map( 'GET', '/charts/[dark|light|sparkline|candlestick:theme]/[a:curA]-[btc:curB]/[a:duration]/[svg|png:format]', function($theme, $curA, $curB, $duration, $format) {
|
||||||
require __DIR__ . '/views/chart.php';
|
require __DIR__ . '/views/chart.php';
|
||||||
return renderChart(
|
return renderChart(
|
||||||
$theme,
|
$theme,
|
||||||
|
|
102
views/chart.php
102
views/chart.php
|
@ -20,6 +20,23 @@ function getJson($url) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getThemeVariable($variable, $defaults) {
|
||||||
|
if (!array_key_exists($variable, $defaults)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (array_key_exists($variable, $_GET)) {
|
||||||
|
$toReturn = htmlspecialchars($_GET[$variable]);
|
||||||
|
if (1 === preg_match('/^[a-fA-F0-9]{3,6}/', $toReturn)) {
|
||||||
|
//this is an HTML color
|
||||||
|
return '#' . $toReturn;
|
||||||
|
} else {
|
||||||
|
return $toReturn;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return $defaults[$variable];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function renderChart(
|
function renderChart(
|
||||||
$theme,
|
$theme,
|
||||||
$currencyA,
|
$currencyA,
|
||||||
|
@ -29,21 +46,30 @@ function renderChart(
|
||||||
) {
|
) {
|
||||||
|
|
||||||
$durations = [
|
$durations = [
|
||||||
|
/*
|
||||||
|
valid resolutions in seconds:
|
||||||
|
300 5m
|
||||||
|
900 15m
|
||||||
|
1800 30m
|
||||||
|
7200 2h
|
||||||
|
14400 4h
|
||||||
|
86400 24h
|
||||||
|
*/
|
||||||
'1y'=> [
|
'1y'=> [
|
||||||
'duration' => 60 * 60 * 24 * 365,
|
'duration' => 60 * 60 * 24 * 365,
|
||||||
'resolution' => 86400 // 1d
|
'resolution' => 86400 // 24h
|
||||||
],
|
],
|
||||||
'30d'=> [
|
'30d'=> [
|
||||||
'duration' => 60 * 60 * 24 * 30,
|
'duration' => 60 * 60 * 24 * 30,
|
||||||
'resolution' => 7200 // 2h
|
'resolution' => 14400 // 4h
|
||||||
],
|
],
|
||||||
'7d'=> [
|
'7d'=> [
|
||||||
'duration' => 60 * 60 * 24 * 7,
|
'duration' => 60 * 60 * 24 * 7,
|
||||||
'resolution' => 1800 // 30m
|
'resolution' => 7200 // 2h
|
||||||
],
|
],
|
||||||
'24h' => [
|
'24h' => [
|
||||||
'duration' => 60 * 60 * 24 * 1,
|
'duration' => 60 * 60 * 24 * 1,
|
||||||
'resolution' => 300 // 15m
|
'resolution' => 900 // 15m
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -74,6 +100,17 @@ function renderChart(
|
||||||
'fontSize'=>4,
|
'fontSize'=>4,
|
||||||
'yAxisEnabled'=>false,
|
'yAxisEnabled'=>false,
|
||||||
'xAxisEnabled'=>false
|
'xAxisEnabled'=>false
|
||||||
|
],
|
||||||
|
'candlestick'=>[
|
||||||
|
'width' => 1000,
|
||||||
|
'height' => 300,
|
||||||
|
'barColor' => '#000',
|
||||||
|
'risingColor' => '#0D0',
|
||||||
|
'fallingColor' => '#D00',
|
||||||
|
'labelColor' => '#000',
|
||||||
|
'fontSize' => 15,
|
||||||
|
'yAxisEnabled'=>true,
|
||||||
|
'xAxisEnabled'=>false
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -124,19 +161,26 @@ function renderChart(
|
||||||
$cacheTimeSeconds = max(60, end($poloniexJson)->date + $dataResolution - time() + 60);
|
$cacheTimeSeconds = max(60, end($poloniexJson)->date + $dataResolution - time() + 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
$chartData = [];
|
|
||||||
|
|
||||||
foreach ($poloniexJson as $item) {
|
|
||||||
$chartData[$item->date] = $item->weightedAverage;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($format == 'svg') {
|
if ($format == 'svg') {
|
||||||
$chartOptions = array_replace($themes[$theme], ['lineColor'=>'@lineColor', 'markerColor'=>'@markerColor']);
|
$chartOptions = array_replace($themes[$theme], [
|
||||||
|
'lineColor'=>'@lineColor',
|
||||||
|
'markerColor'=>'@markerColor',
|
||||||
|
'risingColor'=>'@risingColor',
|
||||||
|
'fallingColor'=>'@fallingColor'
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
$chartOptions = $themes[$theme];
|
$chartOptions = $themes[$theme];
|
||||||
}
|
}
|
||||||
|
|
||||||
$poloniexChart = new NeatCharts\LineChart($chartData, $chartOptions);
|
if ($theme == 'candlestick') {
|
||||||
|
$poloniexChart = new NeatCharts\CandlestickChart($poloniexJson, $chartOptions);
|
||||||
|
} else {
|
||||||
|
$chartData = [];
|
||||||
|
foreach ($poloniexJson as $item) {
|
||||||
|
$chartData[$item->date] = $item->weightedAverage;
|
||||||
|
}
|
||||||
|
$poloniexChart = new NeatCharts\LineChart($chartData, $chartOptions);
|
||||||
|
}
|
||||||
$result = '<?xml version="1.0" standalone="no"?>' . PHP_EOL;
|
$result = '<?xml version="1.0" standalone="no"?>' . PHP_EOL;
|
||||||
$result .= $poloniexChart->render();
|
$result .= $poloniexChart->render();
|
||||||
|
|
||||||
|
@ -165,28 +209,18 @@ function renderChart(
|
||||||
} else if ($format == 'svg') {
|
} else if ($format == 'svg') {
|
||||||
header('Content-type: image/svg+xml; charset=utf-8');
|
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"');
|
header('Content-Disposition: inline; filename="Dash-chart-' . gmdate('Y-m-d\THis+0', $startTime) . '--' . gmdate('Y-m-d\THis+0') . '.svg"');
|
||||||
|
$result = str_replace([
|
||||||
if (array_key_exists('lineColor', $_GET)) {
|
'@lineColor',
|
||||||
$lineColor = htmlspecialchars($_GET['lineColor']);
|
'@markerColor',
|
||||||
if (1 === preg_match('/^[a-fA-F0-9]{3,6}/', $lineColor)) {
|
'@risingColor',
|
||||||
//this is an HTML color
|
'@fallingColor'
|
||||||
$lineColor = '#' . $lineColor;
|
], [
|
||||||
}
|
getThemeVariable('lineColor', $themes[$theme]),
|
||||||
} else {
|
getThemeVariable('markerColor', $themes[$theme]),
|
||||||
$lineColor = $themes[$theme]['lineColor'];
|
getThemeVariable('risingColor', $themes[$theme]),
|
||||||
}
|
getThemeVariable('fallingColor', $themes[$theme])
|
||||||
|
], $result);
|
||||||
if (array_key_exists('markerColor', $_GET)) {
|
echo $result;
|
||||||
$markerColor = htmlspecialchars($_GET['markerColor']);
|
|
||||||
if (1 === preg_match('/^[a-fA-F0-9]{3,6}/', $markerColor)) {
|
|
||||||
//this is an HTML color
|
|
||||||
$markerColor = '#' . $markerColor;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$markerColor = $themes[$theme]['markerColor'];
|
|
||||||
}
|
|
||||||
|
|
||||||
echo str_replace(['@lineColor', '@markerColor'], [$lineColor, $markerColor], $result);
|
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,8 +72,8 @@
|
||||||
<section>
|
<section>
|
||||||
<h2>Transparent SVG and PNG charts with your favorite cryptocurrencies</h2>
|
<h2>Transparent SVG and PNG charts with your favorite cryptocurrencies</h2>
|
||||||
<figure>
|
<figure>
|
||||||
<img src="/charts/dark/dash-btc/30d/svg?lineColor=1C74BC" alt="Poloniex Dash/BTC price">
|
<img src="/charts/candlestick/dash-btc/7d/svg" alt="Poloniex Dash/BTC price">
|
||||||
<figcaption>30 Day Dash price in BTC <code><a href="/charts/dark/dash-btc/30d/svg?lineColor=1C74BC">http://cryptohistory.org/charts/dark/dash-btc/30d/svg?lineColor=1C74BC</a></code></figcaption>
|
<figcaption>30 day Dash price candlesticks in BTC <code><a href="/charts/candlestick/dash-btc/30d/svg">http://cryptohistory.org/charts/candlestick/dash-btc/30d/svg</a></code></figcaption>
|
||||||
</figure>
|
</figure>
|
||||||
<p>Sparklines too! ETH 7 days: <img src="/charts/sparkline/eth-btc/7d/svg" alt="ETH 7d chart" style="vertical-align: bottom;">
|
<p>Sparklines too! ETH 7 days: <img src="/charts/sparkline/eth-btc/7d/svg" alt="ETH 7d chart" style="vertical-align: bottom;">
|
||||||
<code><img src="http://cryptohistory.org/charts/sparkline/eth-btc/7d/svg" alt="ETH 7d chart" style="vertical-align: bottom;"></code>
|
<code><img src="http://cryptohistory.org/charts/sparkline/eth-btc/7d/svg" alt="ETH 7d chart" style="vertical-align: bottom;"></code>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue