Findbugs
、PMD
の
d3.js
ベースの
django-nvd3
と
それを
目次
インストールと 設定
Github
のREADME.rst
に
areski/django-nvd3: Django wrapper for nvd3 - It’s time for beautiful charts
1 . pip
を 使用して install
commnad
pip install django-nvd3
output
Successfully installed Jinja2-2.8 Unidecode-0.4.19 django-nvd3-0.9.7 python-nvd3-0.14.2 python-slugify-1.1.4
2 . node.js
の install
django-bower
を
それがnode.js
に
MAC なので、
brew で install brew install node
which コマンドで
パスを 確認する which npm
output
/usr/local/bin/npm
.bash_profileに
pathを 追加 # node export PATH="/usr/local/bin/npm:$PATH"
3. bower
の install
グローバルオプション
付きで install npm install -g bower
インストール先を
確認 which bower
output
/usr/local/bin/bower
4. django-bower を install
- command
pip install django-bower
5. django-bowerの 設定
参考サイト
以下の 手順で 設定可能です。 https://github.com/nvbn/django-bower settings.py に
djangobower
を追加 INSTALLED_APPS = [ 'djangobower', 'django_extensions', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
settings.py に
django-bower の 設定を 追加 ################################ # Django-bower settings ################################ STATICFILES_FINDERS = [ 'djangobower.finders.BowerFinder', 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ] BOWER_COMPONENTS_ROOT = BASE_DIR + '/components/' BOWER_INSTALLED_APPS = ( 'nvd3', )
bower_install コマンドを
実行 python manage.py bower_install
collectstatic コマンドを
実行 python manage.py collectstatic
コマンドを
File "/v3.4.3/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 48, in path raise ImproperlyConfigured("You're using the staticfiles app " django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
STATIC_ROOT
が
STATIC_ROOT = BASE_DIR + '/sitestatic/'
6. django_nvd3を settings.pyに 追加
INSTALLED_APPS = [ 'libs', 'analyze_pmd_rules', 'analyze_findbugs_rules', 'analyze_github_base', 'analyze_metrics_view', 'django_nvd3', 'djangobower',
サンプルを 実装
1. BOWER_INSTALLED_APPS の 設定
README.rst
に
BOWER_INSTALLED_APPS = ( 'd3', 'nvd3', )
2. templatetagsの 書き換え
django-nvd3
は
少し
ちょっと
(訂正) 2. templatetagsの
参考までに、
Github
のREADME.rst
の
サンプルのREADME.rst
を
動作しませんでした。
(index):33 Uncaught TypeError: chart.tooltipContent is not a function(anonymous function) @ (index):33c @ nv.d3.js:99
Github
のIssue
と
chart.tooltipContent
をchart.tooltip.contentGenerator
に
動作しないようです。
- 対象の
Issue
data:87 Uncaught TypeError: chart.tooltipContent is not a function · Issue #83 · areski/django-nvd3
対象箇所を
content.html
chart.tooltip.contentGenerator(function(key, y, e, graph) { var x = String(graph.point.x); var y = String(graph.point.y); {{ chart.tooltip_condition_string }}
piechart.html
chart.tooltip.contentGenerator(function(key, y, e, graph) { var x = String(key); {{ chart.tooltip_condition_string }} tooltip_str = '<center><b>'+x+'</b></center>' + y; return tooltip_str; });
(訂正) 2. templatetagsの 書き換え
上記のTooltip
が
メソッド名だけでなく、
NVD3の
参考記事 javascript - nvd3 piechart.js - How to edit the tooltip? - Stack Overflow
NVD3 1.7.1 を
インストールの ためまずVersionを 確認する。 bower info nvd3
output
Available versions: - 1.8.4 - 1.8.3 - 1.8.2 - 1.8.1 - 1.7.1 - 1.2.1 - 1.2.0
BOWER_INSTALLED_APPS の
変更 BOWER_INSTALLED_APPS = ( 'd3', 'nvd3#1.7.1', )
再インストール
python manage.py bower_install
再起動後に
動作確認したら JAVASCRIPTエラー
1.7.1 にしても、 JAVASCRIPTエラーが 発生します。
どうも、‘README.rst’ の サンプル記述に 間違いが あるようで、
chartdata に、‘extra1’ を 追加する ことで、 解消しました。
extra_serie = {"tooltip": {"y_start": "", "y_end": ""}} charttype = "pieChart" chartdata = {'x': xdata, 'y1': ydata, 'extra1': extra_serie}
3. その 他は まった ところ
SVG
のcontaierID
に大文字が 含まれると グラフが 描画されない。
Templagete側で、containerID
をstatus_of_use.chartcontainer
記述の
{% load_chart status_of_use.charttype status_of_use.chartdata status_of_use.chartcontainer status_of_use.extra %} ..... <div class="col-lg-offset-3">{% include_container status_of_use.chartcontainer 400 600 %}</div>
ID
をxdata = ["Using", "Not using"] ydata = [findbugs_file_count, repository_count - findbugs_file_count] chartdata = {'x': xdata, 'y1': ydata, 'extra1': extra_serie} chartcontainer = 'status_of_use_findbugs_Piechart' data = { 'charttype': charttype, 'chartdata': chartdata, 'chartcontainer': chartcontainer, 'extra': { 'x_is_date': False, 'x_axis_format': '', 'tag_script_js': True, 'jquery_on_ready': True, } }
展開されたHTML
、Javascript
を
javascript
側は
d3.select('#status_of_use_findbugs_piechart svg') .datum(datum) .transition().duration(500) .attr('height', 450) .call(chart);
HTML
側は、
<div class="col-lg-offset-3"><div id="status_of_use_findbugs_Piechart"><svg style="width:600px;height:400px;"></svg></div>
なんとか
pieChart
以外の
直接 NVD3
を
ラップしている
良いのかもしれません。
※ Java
の
以上です。
コメント