public function articleCount() { $categories = PostCategory::all(); $colours = RandomColor::many($categories->count(), ['hue' => 'blue']); return $categories->map(function ($category, $index) use($colours) { return ['value' => $category->posts->count(), 'color' => $colours[$index], 'highlight' => '#1D976C', 'label' => $category->name]; })->toArray(); }
<?php /** @var \common\models\Quiz $quiz */ use backend\helpers\ArrayHelper; use dosamigos\chartjs\ChartJs; use rmrevin\yii\fontawesome\FontAwesome; use yii\bootstrap\Html; $this->title = "Просмотр опроса '{$quiz->name}'"; $this->params['breadcrumbs'][] = ['url' => '/quiz', 'label' => 'Опросы']; $this->params['breadcrumbs'][] = 'Просмотр опроса'; $colors = \Colors\RandomColor::many(sizeof($quiz->answersWithCount), ['hue' => 'blue', 'luminosity' => 'light']); ?> <div class="panel panel-default"> <div class="panel-heading"> <?php echo Html::a(FontAwesome::i('arrow-left') . Html::tag('small', 'Назад'), ['/quiz/default/index'], ['class' => 'btn btn-app']); ?> <?php echo Html::a(FontAwesome::i('pencil') . Html::tag('small', 'Редактировать'), ['/quiz/edit/' . $quiz->id], ['class' => 'btn btn-app']); ?> </div> <div class="panel-body"> <div class="col-xs-6"> <?php echo ChartJs::widget(['type' => 'pie', 'data' => ['datasets' => [['data' => ArrayHelper::toPercents(array_values(ArrayHelper::getColumn($quiz->answersWithCount, 'count'))), 'label' => 'Результаты опроса', 'backgroundColor' => $colors]], 'labels' => array_values(ArrayHelper::getColumn($quiz->answersWithCount, 'text'))]]); ?> </div> <div class="col-xs-6"> <?php echo ChartJs::widget(['type' => 'bar', 'data' => ['datasets' => [['data' => array_values(ArrayHelper::getColumn($quiz->answersWithCount, 'count')), 'label' => 'Результаты опроса', 'backgroundColor' => $colors]], 'labels' => array_values(ArrayHelper::getColumn($quiz->answersWithCount, 'text'))], 'options' => []]); ?>
/** * Lavanda engine initialisation method. * * @param Request $request * @param Router $router * @param ViewFactory $viewFactory * @param ValidationFactory $validationFactory * @param Filesystem $fileSystem * @param FormBuilder $formBuilder */ public function boot(Repository $config, Request $request, Translator $translator, Router $router, ViewFactory $viewFactory, ValidationFactory $validationFactory, Filesystem $fileSystem, FormBuilder $formBuilder) { // model dependencies Model::setRequest($request); Model::setTranslator($translator); Model::setView($viewFactory); Model::setFormBuilder($formBuilder); Model::setBasePath($this->app->basePath()); Model::setPublicPath($this->app->make('path.public')); // adding Lavanda controllers to routing if (!$this->app->routesAreCached()) { $router->get('admin', ['uses' => 'Idealogica\\Lavanda\\MainController@index', 'as' => 'admin.main.index']); $router->get('admin/_image', ['uses' => 'Idealogica\\Lavanda\\ImageController@index', 'as' => 'admin.image.index']); $router->get('admin/{model}', ['uses' => 'Idealogica\\Lavanda\\EntityController@index', 'as' => 'admin.entity.index']); $router->get('admin/{model}/create', ['uses' => 'Idealogica\\Lavanda\\EntityController@create', 'as' => 'admin.entity.create']); $router->post('admin/{model}', ['uses' => 'Idealogica\\Lavanda\\EntityController@store', 'as' => 'admin.entity.store']); $router->get('admin/{model}/{id}', ['uses' => 'Idealogica\\Lavanda\\EntityController@show', 'as' => 'admin.entity.show']); $router->get('admin/{model}/{id}/edit', ['uses' => 'Idealogica\\Lavanda\\EntityController@edit', 'as' => 'admin.entity.edit']); $router->put('admin/{model}/{id}', ['uses' => 'Idealogica\\Lavanda\\EntityController@update', 'as' => 'admin.entity.update']); $router->delete('admin/{model}/{id}', ['uses' => 'Idealogica\\Lavanda\\EntityController@destroy', 'as' => 'admin.entity.destroy']); } // custom image validation method $validationFactory->extend('lavanda_image', function ($attribute, $value, $parameters) use($request) { if ($request->hasFile($attribute)) { $validator = Validator::make($request->all(), [$attribute => 'min:1|max:10000|mimes:' . implode(',', $parameters)]); return !$validator->fails(); } return true; }); // view composer $viewFactory->composer(['lavanda::layout.common', 'lavanda::main.index'], function (View $view) use($config, $fileSystem, $request) { $menu = []; $maxCount = 0; $modelPath = $config->get('lavanda.model_path'); $files = array_values($fileSystem->files($modelPath)); mt_srand(10); $colors = RandomColor::many(count($files), ['luminosity' => 'light', 'hue' => 265]); foreach ($files as $idx => $file) { $model = strtolower(basename($file, ".php")); $modelClass = getModelClass($model); if (!in_array('Idealogica\\Lavanda\\Model', class_parents($modelClass))) { continue; } $url = adminRoute($model, 'index'); $count = $modelClass::count(); $maxCount = $count > $maxCount ? $count : $maxCount; $menu[] = ['title' => $modelClass::getPluralName(), 'url' => $url, 'selected' => preg_match('/^' . preg_quote($url, '/') . '/', $request->url()), 'count' => $count, 'columns' => 1, 'color' => (string) (new Color($colors[$idx]))->desaturate(50)->lighten(5)]; } $tiles = $menu; if ($maxCount) { $rowItems = []; $countRow = function ($rowItems) { $rowColumns = 0; foreach ($rowItems as $item) { $rowColumns += $item['columns']; } return $rowColumns; }; $stretchRow = function (array &$rowItems) use($countRow) { if ($rowItems) { $count = $countRow($rowItems); if ($count < 12) { $add = (int) ((12 - $count) / count($rowItems)); foreach ($rowItems as &$rowItem) { $rowItem['columns'] += $add; } foreach ($rowItems as &$rowItem) { if ($countRow($rowItems) >= 12) { break; } $rowItem['columns']++; } } } }; usort($tiles, function ($a, $b) { if ($a['count'] == $b['count']) { return 0; } return $a['count'] > $b['count'] ? -1 : 1; }); foreach ($tiles as &$item) { $columns = (int) round(12 * $item['count'] / $maxCount); $item['columns'] = $columns ?: 1; $rowItems[] =& $item; $rowColumns = $countRow($rowItems); if ($rowColumns > 12) { unset($rowItems[count($rowItems) - 1]); $stretchRow($rowItems); $rowItems = []; $rowItems[] =& $item; } elseif ($rowColumns == 12) { $rowItems = []; } } $stretchRow($rowItems); } $view->with('menu', $menu)->with('tiles', $tiles); }); // request rebinding $this->app->rebinding('request', function ($app, $request) { $app->forgetInstance('laravel-form-helper'); $app->forgetInstance('laravel-form-builder'); $app->bindShared('laravel-form-helper', function ($app) use($request) { $configuration = $app['config']->get('laravel-form-builder'); return new FormHelper($app['view'], $request, $configuration); }); $app->bindShared('laravel-form-builder', function ($app) { return new FormBuilder($app, $app['laravel-form-helper']); }); Model::flush(); Model::setRequest($request); Model::setFormBuilder($app->make('laravel-form-builder')); }); // package configuration $packageDir = dirname(__DIR__); $viewsPath = $packageDir . '/views'; $translationsPath = $packageDir . '/lang'; $assetsPath = $packageDir . '/assets'; $configPath = $packageDir . '/config/common.php'; $this->loadViewsFrom($viewsPath, 'lavanda'); $this->loadTranslationsFrom($translationsPath, 'lavanda'); $this->mergeConfigFrom($configPath, 'lavanda'); $this->publishes([$translationsPath => base_path('resources/lang/vendor/lavanda')], 'lang'); $this->publishes([$viewsPath => base_path('resources/views/vendor/lavanda')], 'views'); $this->publishes([$assetsPath => public_path('vendor/lavanda')], 'public'); $this->publishes([$configPath => config_path('lavanda.php')], 'config'); }
<h3>Dark colors</h3> <pre>RandomColor::many(27, array('luminosity'=>'dark'));</pre> <div class="output"> <?php foreach (RandomColor::many(27, array('luminosity' => 'dark')) as $c) { echo '<span style="background:' . $c . ';"></span>'; } ?> </div> <h3>Truly random colors</h3> <pre>RandomColor::many(36, array('luminosity'=>'random', 'hue'=>'random'));</pre> <div class="output"> <?php foreach (RandomColor::many(36, array('luminosity' => 'random', 'hue' => 'random')) as $c) { echo '<span style="background:' . $c . ';"></span>'; } ?> </div> <a class="btn btn-danger btn-lg btn-download" href="https://github.com/mistic100/RandomColor.php"> Get this on GitHub <i class="glyphicon glyphicon-arrow-right"></i> </a> <footer class="footer"> <p>Made by <a href="http://www.strangeplanet.fr">Damien "Mistic" Sorel</a> based on the work of <a href="http://twitter.com/davidmerfieId">David Merfield</a>. Licensed under the MIT License.</p> </footer> </body>