2021年12月26日
今回も先日開催されたJTUG(Japan Tableau User Group)冬におけるLM-7の講演内容を一部アップデートしてTableauに関連するマニアックなテクニックを紹介する。
This entry introduces a maniacal technique related to Tableau, which is partly an update of the presentation at LM-7 in JTUG (Japan Tableau User Group) winter 2021.
Tableauに関連すると書いたが、本手法自体はTableauと関係がない。Tableauではテキストラベルを任意の座標に配置することができるが、回転させることができない(バージョン2021.4時点)。そのため、Vizの内容に合わせて回転させたテキストラベルがほしいときには、別途作成した画像を配置して組み合わせる必要がある。
I mentioned that it is related to Tableau, but this technique itself is not a Tableau technique. In Tableau, text labels can be placed at arbitrary coordinates, but they cannot be rotated (as of version 2021.4). Therefore, if you want a text label that is rotated to match the contents of the viz, you need to use a separately created image.
ただ所望の位置、回転角のテキスト画像を作成するのは手間がかかる。Pythonなどのスクリプトを駆使すれば可能だが、誰でもできる方法はないかと考えて、SVGとExcelを組み合わせて実現するようにしたのが本手法だ。
However, it takes time and effort to create a text image with the desired position and rotation angle. While this can be done by using Python or other scripting tools, I wondered if there was a way for anyone to easily generate the desired text label images, and discovered this method, which is achieved by combining SVG and Excel.
本手法を用いることによってTableauで次のようなvizを作成することができる。
By using this method, the following viz can be created in Tableau.
This entry introduces a maniacal technique related to Tableau, which is partly an update of the presentation at LM-7 in JTUG (Japan Tableau User Group) winter 2021.
Tableauに関連すると書いたが、本手法自体はTableauと関係がない。Tableauではテキストラベルを任意の座標に配置することができるが、回転させることができない(バージョン2021.4時点)。そのため、Vizの内容に合わせて回転させたテキストラベルがほしいときには、別途作成した画像を配置して組み合わせる必要がある。
I mentioned that it is related to Tableau, but this technique itself is not a Tableau technique. In Tableau, text labels can be placed at arbitrary coordinates, but they cannot be rotated (as of version 2021.4). Therefore, if you want a text label that is rotated to match the contents of the viz, you need to use a separately created image.
ただ所望の位置、回転角のテキスト画像を作成するのは手間がかかる。Pythonなどのスクリプトを駆使すれば可能だが、誰でもできる方法はないかと考えて、SVGとExcelを組み合わせて実現するようにしたのが本手法だ。
However, it takes time and effort to create a text image with the desired position and rotation angle. While this can be done by using Python or other scripting tools, I wondered if there was a way for anyone to easily generate the desired text label images, and discovered this method, which is achieved by combining SVG and Excel.
本手法を用いることによってTableauで次のようなvizを作成することができる。
By using this method, the following viz can be created in Tableau.
SVGの概要 / SVG Overview
SVGはXMLベースのベクトル画像フォーマットだ。任意の座標、回転角のテキスト画像を生成するのは容易い。下記は左上の座標(x0, y0)、幅width、高さheightのキャンバスにおいて、座標(xn, yn)、回転角θnにtext_labelを配置するときの記述だ。SVG is an XML-based vector image format. It is easy to generate a text image with any coordinates and rotation angle. The following is a description of placing a text_label at coordinates (xn, yn) and rotation angle θn on a canvas with the upper left corner coordinates (x0, y0), width, and height.
注意点としては、Tableauとはyの正の方向が逆であり、あわせて回転角方向も逆ということだ。また、Tableauでは回転角はラジアンで表すが、SVGでは度で表す。このあたり変換が面倒だが、今回は変換をすべて自動で行うExcelを用意したので特に気にする必要はない。
Note that the positive direction of y in SVG is the opposite of that in Tableau, and the rotation angle direction is also the opposite. In Tableau, rotation angles are expressed in radians, but in SVG, they are expressed in degrees. Although this conversion is troublesome, there is no need to worry about it since I have prepared an Excel file that automatically performs all conversions.
テキストラベルの座標と位置の取得 / Acquire the coordinates and position of a text label.
まずは、表示したいテキストラベルの位置と回転角と内容をTableauで求める。座標系はTableau上の座標系でよく、回転角もラジアンで良い。回転角は詳細に入れて「データの表示」からデータをCSVでエクスポートする。First, you need to obtain the position, rotation angle, and content of the text label you want to display in Tableau. The coordinates are expressed in a coordinate system in Tableau, and the rotation angle is also expressed in radians. The rotation angle is entered in the details and the data is exported as CSV from "View Data".
ExcelによるSVGの出力 / SVG output by Excel
次のURLでSVG出力用のExcelを配布しているので、ダウンロードする。Download Excel for SVG output from the following URL.
- https://drive.google.com/file/d/1xFDeloRrof7ehz9R031j-VDZIyXUDCIE/view?usp=sharing
- 無保証です。No warranty.
- 利用にはVizに合わせた調整が必要になります。Requires adjustment to fit the Viz.
- 無保証です。No warranty.
先程出力したCSVからテキストラベルのx座標、y座標、回転角θ、内容をData Inputタブに貼り付ける。このExcelは500行まで対応するように作っているが、500行を超える場合には適当に修正してもらいたい。
Paste the x-coordinates, y-coordinates, rotation angle θ, and the contents of the text label from the CSV file you just output into the Data Input tab. This Excel is designed to handle up to 500 lines. If you have more than 500 lines, please modify it appropriately.
次にParameter Settingsタブに移り、いくつかのパラメータを設定する。倍率は自動設定されるが、出力される画像サイズが大きすぎたり小さすぎたりする場合には適当に修正する。
Next, move to the Parameter Settings tab to set some parameters. The magnification is set automatically, but if the output image size is too large or too small, modify it appropriately.

キャンバスサイズはデフォルトでは(0,0)を中心に1500×1500の正方形としているが、テキストラベルがキャンバスからはみ出る場合には、調整が必要だ。
The default canvas size is a 1500x1500 square centered at (0,0), but if the text label extends beyond the canvas, it needs to be adjusted.
フォント色はCSSフォントカラーもしくはフォントコードで指定を行う。
The font color can be specified by CSS font color or font code.
最後にSVG Outputタブに移動する。フィルタを再適用するために必ずフィルタアイコンを押下し、OKボタンを押す。この作業をしないと適切に出力されない。
Finally, go to the SVG Output tab. Be sure to press the filter icon to reapply the filter, and then press the OK button. If you don't do this, the output will not be generated properly.
出力されたSVG(<SVG>から</SVG>の間)をメモ帳などに貼り付けて拡張子.svgで保存する。SVGファイルはEdgeやChromeなどのブラウザで閲覧することができる。思ったような出力ができていない場合には入力データやパラメータを調整してSVGを再出力する。
Paste the output SVG description (between <SVG> and </SVG>) into Notepad or other program and save it with the .svg extension. The SVG file can be viewed in a browser such as Edge or Chrome. If the output does not look as expected, adjust the input data and parameters and re-output the SVG.
背景画像の作成 / Creating a background image
所望のSVG出力が得られれば、最後に適当な画像編集ソフト(GIMP, Photoshop, Affinity Photo等)を用いて、SVGファイルをPNGファイルに変換する。GIMPは無料なのでおすすめだが、読み込み時のデフォルト解像度が低いので3倍ぐらいの解像度にしたほうが良い。PNG画像ができれば、それをTableau上に配置すれば良い。次のURLからワークブックがダウンロードできるので確認してもらいたい。
Once the desired SVG output is obtained, the last step is to convert it to a PNG file using an appropriate image editing software (GIMP, Photoshop, Affinity Photo, etc.). GIMP is recommended because it is free. However, GIMP's default resolution when importing SVG is low, so you should increase the resolution three times.
After you have created the PNG image, you can place it in Tableau. You can download the workbook from the following URL.
lunarmodule7 at 23:44│Comments(0)│