Commit 06c69ebe authored by Denis Chenu's avatar Denis Chenu
Browse files

[feature][WIP] Use PivotTable.js for admin statitics

parent ecb6e503
/**
* @file statsPivotTable plugin for limesurvey, javascript part
* @author Denis Chenu
* @copyright Denis Chenu <http://www.sondages.pro>
* @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
*/
/**
* @file statsPivotTable plugin for limesurvey, javascript part
* @author Denis Chenu
* @copyright Denis Chenu <http://www.sondages.pro>
* @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL v3.0
*/
$(function(){
//~ var derivers = $.pivotUtilities.derivers;
//~ var renderers = $.extend(
//~ $.pivotUtilities.renderers,
//~ $.pivotUtilities.c3_renderers,
//~ $.pivotUtilities.d3_renderers,
//~ $.pivotUtilities.export_renderers
//~ );
$.getJSON(LS.plugin.statsPivotTable.jsonUrl, function(responses) {
$("#pivot-table").pivotUI(responses, { });
});
});
<?php
class jsonPivotWriter extends Writer
{
/**
* The open filehandle
*/
private $file = null;
/**
* first don't need seperator
*/
protected $havePrev = false;
public function init(SurveyObj $survey, $sLanguageCode, FormattingOptions $oOptions)
{
parent::init($survey, $sLanguageCode, $oOptions);
$sStartOutput='[';
if ($oOptions->output=='display') {
header("Content-type: application/json");
echo $sStartOutput;
} elseif ($oOptions->output == 'file') {
$this->file = fopen($this->filename, 'w');
fwrite($this->file, $sStartOutput);
}
}
protected function outputRecord($headers, $values, FormattingOptions $oOptions)
{
$aJson=array_combine ($headers,$values);
$sJson=json_encode($aJson);
if($this->havePrev){
$sJson=','.$sJson;
}
$this->havePrev=true;
if ($oOptions->output=='display')
{
echo $sJson;
} elseif ($oOptions->output == 'file') {
fwrite($this->file, $sJson);
}
}
public function close()
{
$sEndOutput=']';
if (!$this->file)
{
echo $sEndOutput;
}
else
{
fwrite($this->file, $sEndOutput);
fclose($this->file);
}
}
}
<?php
/**
* statsPivotTable
*
*
* @category Plugin
*/
class statsPivotTable extends \ls\pluginmanager\PluginBase
{
static protected $description = 'See dynamic statitics with pivot table';
static protected $name = 'statsPivotTable';
protected $storage = 'DbStorage';
/**
* Init stuff
*
* @return void
*/
public function init()
{
//~ $this->subscribe('newDirectRequest');
//~ $this->subscribe('afterSurveyMenuLoad');
/* To add the menu icon in javascript */
//~ $this->subscribe('beforeControllerAction');
/* Add language */
$this->subscribe('beforeToolsMenuRender');
/* Add the link in menu */
$this->subscribe('beforeToolsMenuRender');
/* Display the json */
$this->subscribe('newDirectRequest');
$this->subscribe('listExportPlugins');
$this->subscribe('newExport');
}
/**
* see beforeToolsMenuRender event
*
* @return void
*/
public function beforeToolsMenuRender()
{
$event = $this->getEvent();
$surveyId = $event->get('surveyId');
$href = Yii::app()->createUrl(
'admin/pluginhelper',
array(
'sa' => 'sidebody',
'plugin' => get_class($this),
'method' => 'actionIndex',
'surveyId' => $surveyId
)
);
$menuItem = new \ls\menu\MenuItem(
array(
'label' => $this->_translate('Pivot table'),
'iconClass' => 'fa fa-table',
'href' => $href
)
);
$event->append('menuItems', array($menuItem));
}
/**
* Main function
* @param int $surveyId Survey id
*
* @return string
*/
public function actionIndex($surveyId)
{
if(!Permission::model()->hasSurveyPermission($surveyId,'statistics','read')){
throw new CHttpException(401);
}
$aData=array();
/* Language part */
$aData['gT']['title']=$this->_translate('Pivot table');
/* Usage part */
$jsOptions=array(
'jsonUrl'=>Yii::app()->createUrl('plugins/direct',array(
'plugin'=>get_class($this),
'sid'=>$surveyId
)),
'lang'=>array(
),
);
$jsInit = "var LS = LS || {};\n"
. "LS.plugin = LS.plugin || {};\n"
. "LS.plugin.statsPivotTable = ".json_encode($jsOptions).";\n";
$assetUrl=Yii::app()->assetManager->publish(dirname(__FILE__) . '/assets');
$pivotAssetUrl=Yii::app()->assetManager->publish(dirname(__FILE__) . '/vendor/pivottable/dist');
$c3AssetUrl=Yii::app()->assetManager->publish(dirname(__FILE__) . '/vendor/c3');
$d3AssetUrl=Yii::app()->assetManager->publish(dirname(__FILE__) . '/vendor/d3');
Yii::app()->clientScript->registerScript('statsPivotTable-jsInit',$jsInit,CClientScript::POS_END);
Yii::app()->clientScript->registerScriptFile($d3AssetUrl."/d3.min.js",CClientScript::POS_BEGIN);
Yii::app()->clientScript->registerScriptFile($c3AssetUrl."/c3.min.js",CClientScript::POS_BEGIN);
Yii::app()->clientScript->registerScriptFile($pivotAssetUrl."/pivot.js",CClientScript::POS_BEGIN);
Yii::app()->clientScript->registerScriptFile($pivotAssetUrl."/c3_renderers.min.js",CClientScript::POS_BEGIN);
Yii::app()->clientScript->registerScriptFile($pivotAssetUrl."/d3_renderers.min.js",CClientScript::POS_BEGIN);
Yii::app()->clientScript->registerScriptFile($pivotAssetUrl."/export_renderers.min.js",CClientScript::POS_BEGIN);
Yii::app()->clientScript->registerScriptFile($assetUrl."/statsPivotTable.js",CClientScript::POS_BEGIN);
Yii::app()->clientScript->registerCssFile($c3AssetUrl."/c3.min.css");
Yii::app()->clientScript->registerCssFile($pivotAssetUrl."/pivot.min.css");
Yii::app()->clientScript->registerCssFile($assetUrl."/statsPivotTable.css");
$content = $this->renderPartial('index', $aData, true);
return $content;
}
/**
* @see PluginManager->newDirectRequest()
*/
public function newDirectRequest()
{
if($this->event->get('target')!=get_class($this)){
return;
}
$iSurveyId=\Yii::app()->getRequest()->getParam('sid');
if(!\Permission::model()->hasSurveyPermission($iSurveyId,'statistics','read')){
throw new \CHttpException(401);
}
if (!tableExists('{{survey_' . $iSurveyId . '}}')) {
throw new \CHttpException(500,'Invalid survey id');
}
if(!\SurveyDynamic::model($iSurveyId)->getMaxId()) {
header("Content-type: application/json");
echo json_encode(array());
return;
}
$language=\Survey::model()->findByPk($iSurveyId)->language;
$aFields=array_keys(createFieldMap($iSurveyId,'full',true,false,$language));
Yii::app()->loadHelper('admin/exportresults');
Yii::import('application.helpers.viewHelper');
Yii::setPathOfAlias(get_class($this), dirname(__FILE__));
Yii::import(get_class($this).".JsonPivotWriter");
$oFormattingOptions=new \FormattingOptions();
$oFormattingOptions->responseMinRecord=1;
$oFormattingOptions->responseMaxRecord=SurveyDynamic::model($iSurveyId)->getMaxId();
$oFormattingOptions->selectedColumns=$aFields;
$oFormattingOptions->responseCompletionState='complete';
$oFormattingOptions->headingFormat='complete';// Maybe make own to have code + abbreviated
$oFormattingOptions->answerFormat='long';
$oFormattingOptions->output='display';
$oExport=new \ExportSurveyResultsService();
$content=$oExport->exportSurvey($iSurveyId,$language, 'json-pivot',$oFormattingOptions, '');
}
/**
* @see newExport event
*/
public function newExport()
{
$event = $this->getEvent();
$type = $event->get('type');
switch ($type) {
case "json-pivot":
default:
$writer = new jsonPivotWriter();
break;
}
$event->set('writer', $writer);
}
/**
* Registers this export type
*/
public function listExportPlugins()
{
$event = $this->getEvent();
$exports = $event->get('exportplugins');
$exports['json-pivot'] = get_class();
$event->set('exportplugins', $exports);
}
public function beforeControllerAction()
{
// TODO
}
/**
* Launch a json arry to browser
* @param mixed $object to display
*/
private function _displayJson($object)
{
header("Content-type: application/json");
echo json_encode($object);
Yii::app()->end();
}
/**
* get translation
* @param string
* @return string
*/
private function _translate($string){
return Yii::t('',$string,array(),get_class($this));
}
/**
* Add this translation just after loaded all plugins
* @see event afterPluginLoad
*/
public function afterPluginLoad(){
// messageSource for this plugin:
$messageSource=array(
'class' => 'CGettextMessageSource',
'cacheID' => get_class($this).'Lang',
'cachingDuration'=>3600,
'forceTranslation' => true,
'useMoFile' => true,
'basePath' => __DIR__ . DIRECTORY_SEPARATOR.'locale',
'catalog'=>'messages',// default from Yii
);
Yii::app()->setComponent(get_class($this),$messageSource);
}
}
The MIT License (MIT)
Copyright (c) 2013 Masayuki Tanaka
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c3 [![Build Status](https://travis-ci.org/c3js/c3.svg?branch=master)](https://travis-ci.org/c3js/c3) [![Dependency Status](https://david-dm.org/c3js/c3.svg)](https://david-dm.org/c3js/c3) [![devDependency Status](https://david-dm.org/c3js/c3/dev-status.svg)](https://david-dm.org/c3js/c3#info=devDependencies) [![license](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/c3js/c3/blob/master/LICENSE) [![codecov.io](https://codecov.io/github/c3js/c3/coverage.svg?branch=master)](https://codecov.io/github/c3js/c3?branch=master)
==
c3 is a D3-based reusable chart library that enables deeper integration of charts into web applications.
Follow the link for more information: [http://c3js.org](http://c3js.org/)
## Tutorial and Examples
+ [Getting Started](http://c3js.org/gettingstarted.html)
+ [Examples](http://c3js.org/examples.html)
Additional samples can be found in this repository:
+ [https://github.com/c3js/c3/tree/master/htdocs/samples](https://github.com/c3js/c3/tree/master/htdocs/samples)
You can run these samples as:
```
$ cd c3/htdocs
$ python -m SimpleHTTPServer 8080
```
## Google Group
For general C3.js-related discussion, please visit our [Google Group at https://groups.google.com/forum/#!forum/c3js](https://groups.google.com/forum/#!forum/c3js).
## Gitter
[![Join the chat at https://gitter.im/c3js/c3](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/c3js/c3?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
## Using the issue queue
The [issue queue](https://github.com/c3js/c3/issues) is to be used for reporting defects and problems with C3.js, in addition to feature requests and ideas. It is **not** a catch-all support forum. **For general support enquiries, please use the [Google Group](https://groups.google.com/forum/#!forum/c3js) at https://groups.google.com/forum/#!forum/c3js.** All questions involving the interplay between C3.js and any other library (such as AngularJS) should be posted there first!
Before reporting an issue, please do the following:
1. [Search for existing issues](https://github.com/c3js/c3/issues) to ensure you're not posting a duplicate.
1. [Search the Google Group](https://groups.google.com/forum/#!forum/c3js) to ensure it hasn't been addressed there already.
1. Create a JSFiddle or Plunkr highlighting the issue. Please don't include any unnecessary dependencies so we can isolate that the issue is in fact with C3. *Please be advised that custom CSS can modify C3.js output!*
1. When posting the issue, please use a descriptive title and include the version of C3 (or, if cloning from Git, the commit hash — C3 is under active development and the master branch contains the latest dev commits!), along with any platform/browser/OS information that may be relevant.
## Pull requests
Pull requests are welcome, though please post an issue first to see whether such a change is desirable.
If you choose to submit a pull request, please do not bump the version number unless asked to, and please include test cases for any new features. Squash all your commits as well, please.
## Playground
Please fork this fiddle:
+ [http://jsfiddle.net/masayuki0812/7kYJu/](http://jsfiddle.net/masayuki0812/7kYJu/)
## Dependency
+ [D3.js](https://github.com/mbostock/d3) `~3.5.0`
## License
MIT
/*-- Chart --*/
.c3 svg {
font: 10px sans-serif;
-webkit-tap-highlight-color: transparent; }
.c3 path, .c3 line {
fill: none;
stroke: #000; }
.c3 text {
-webkit-user-select: none;
-moz-user-select: none;
user-select: none; }
.c3-legend-item-tile,
.c3-xgrid-focus,
.c3-ygrid,
.c3-event-rect,
.c3-bars path {
shape-rendering: crispEdges; }
.c3-chart-arc path {
stroke: #fff; }
.c3-chart-arc text {
fill: #fff;
font-size: 13px; }
/*-- Axis --*/
/*-- Grid --*/
.c3-grid line {
stroke: #aaa; }
.c3-grid text {
fill: #aaa; }
.c3-xgrid, .c3-ygrid {
stroke-dasharray: 3 3; }
/*-- Text on Chart --*/
.c3-text.c3-empty {
fill: #808080;
font-size: 2em; }
/*-- Line --*/
.c3-line {
stroke-width: 1px; }
/*-- Point --*/
.c3-circle._expanded_ {
stroke-width: 1px;
stroke: white; }
.c3-selected-circle {
fill: white;
stroke-width: 2px; }
/*-- Bar --*/
.c3-bar {
stroke-width: 0; }
.c3-bar._expanded_ {
fill-opacity: 0.75; }
/*-- Focus --*/
.c3-target.c3-focused {
opacity: 1; }
.c3-target.c3-focused path.c3-line, .c3-target.c3-focused path.c3-step {
stroke-width: 2px; }
.c3-target.c3-defocused {
opacity: 0.3 !important; }
/*-- Region --*/
.c3-region {
fill: steelblue;
fill-opacity: .1; }
/*-- Brush --*/
.c3-brush .extent {
fill-opacity: .1; }
/*-- Select - Drag --*/
/*-- Legend --*/
.c3-legend-item {
font-size: 12px; }
.c3-legend-item-hidden {
opacity: 0.15; }
.c3-legend-background {
opacity: 0.75;
fill: white;
stroke: lightgray;
stroke-width: 1; }
/*-- Title --*/
.c3-title {
font: 14px sans-serif; }
/*-- Tooltip --*/
.c3-tooltip-container {
z-index: 10; }
.c3-tooltip {
border-collapse: collapse;
border-spacing: 0;
background-color: #fff;
empty-cells: show;
-webkit-box-shadow: 7px 7px 12px -9px #777777;
-moz-box-shadow: 7px 7px 12px -9px #777777;
box-shadow: 7px 7px 12px -9px #777777;
opacity: 0.9; }
.c3-tooltip tr {
border: 1px solid #CCC; }
.c3-tooltip th {
background-color: #aaa;
font-size: 14px;
padding: 2px 5px;
text-align: left;
color: #FFF; }
.c3-tooltip td {
font-size: 13px;
padding: 3px 6px;
background-color: #fff;
border-left: 1px dotted #999; }
.c3-tooltip td > span {
display: inline-block;
width: 10px;
height: 10px;
margin-right: 6px; }
.c3-tooltip td.value {
text-align: right; }
/*-- Area --*/
.c3-area {
stroke-width: 0;
opacity: 0.2; }
/*-- Arc --*/
.c3-chart-arcs-title {
dominant-baseline: middle;
font-size: 1.3em; }
.c3-chart-arcs .c3-chart-arcs-background {
fill: #e0e0e0;
stroke: none; }
.c3-chart-arcs .c3-chart-arcs-gauge-unit {
fill: #000;
font-size: 16px; }
.c3-chart-arcs .c3-chart-arcs-gauge-max {
fill: #777; }
.c3-chart-arcs .c3-chart-arcs-gauge-min {
fill: #777; }
.c3-chart-arc .c3-gauge-value {
fill: #000;
/* font-size: 28px !important;*/ }
This diff is collapsed.
.c3 svg{font:10px sans-serif;-webkit-tap-highlight-color:transparent}.c3 line,.c3 path{fill:none;stroke:#000}.c3 text{-webkit-user-select:none;-moz-user-select:none;user-select:none}.c3-bars path,.c3-event-rect,.c3-legend-item-tile,.c3-xgrid-focus,.c3-ygrid{shape-rendering:crispEdges}.c3-chart-arc path{stroke:#fff}.c3-chart-arc text{fill:#fff;font-size:13px}.c3-grid line{stroke:#aaa}.c3-grid text{fill:#aaa}.c3-xgrid,.c3-ygrid{stroke-dasharray:3 3}.c3-text.c3-empty{fill:gray;font-size:2em}.c3-line{stroke-width:1px}.c3-circle._expanded_{stroke-width:1px;stroke:#fff}.c3-selected-circle{fill:#fff;stroke-width:2px}.c3-bar{stroke-width:0}.c3-bar._expanded_{fill-opacity:.75}.c3-target.c3-focused{opacity:1}.c3-target.c3-focused path.c3-line,.c3-target.c3-focused path.c3-step{stroke-width:2px}.c3-target.c3-defocused{opacity:.3!important}.c3-region{fill:#4682b4;fill-opacity:.1}.c3-brush .extent{fill-opacity:.1}.c3-legend-item{font-size:12px}.c3-legend-item-hidden{opacity:.15}.c3-legend-background{opacity:.75;fill:#fff;stroke:#d3d3d3;stroke-width:1}.c3-title{font:14px sans-serif}.c3-tooltip-container{z-index:10}.c3-tooltip{border-collapse:collapse;border-spacing:0;background-color:#fff;empty-cells:show;-webkit-box-shadow:7px 7px 12px -9px #777;-moz-box-shadow:7px 7px 12px -9px #777;box-shadow:7px 7px 12px -9px #777;opacity:.9}.c3-tooltip tr{border:1px solid #CCC}.c3-tooltip th{background-color:#aaa;font-size:14px;padding:2px 5px;text-align:left;color:#FFF}.c3-tooltip td{font-size:13px;padding:3px 6px;background-color:#fff;border-left:1px dotted #999}.c3-tooltip td>span{display:inline-block;width:10px;height:10px;margin-right:6px}.c3-tooltip td.value{text-align:right}.c3-area{stroke-width:0;opacity:.2}.c3-chart-arcs-title{dominant-baseline:middle;font-size:1.3em}.c3-chart-arcs .c3-chart-arcs-background{fill:#e0e0e0;stroke:none}.c3-chart-arcs .c3-chart-arcs-gauge-unit{fill:#000;font-size:16px}.c3-chart-arcs .c3-chart-arcs-gauge-max,.c3-chart-arcs .c3-chart-arcs-gauge-min{fill:#777}.c3-chart-arc .c3-gauge-value{fill:#000}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Copyright 2010-2016 Mike Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# D3: Data-Driven Documents
<a href="https://d3js.org"><img src="https://d3js.org/logo.svg" align="left" hspace="10" vspace="6"></a>
**D3** (or **D3.js**) is a JavaScript library for visualizing data using web standards. D3 helps you bring data to life using SVG, Canvas and HTML. D3 combines powerful visualization and interaction techniques with a data-driven approach to DOM manipulation, giving you the full capabilities of modern browsers and the freedom to design the right visual interface for your data.
## Resources
* [API Reference](https://github.com/d3/d3/blob/master/API.md)
* [Release Notes](https://github.com/d3/d3/releases)
* [Gallery](https://github.com/d3/d3/wiki/Gallery)
* [Examples](https://bl.ocks.org/mbostock)
* [Wiki](https://github.com/d3/d3/wiki)
## Installing