Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Sv translation
languageen

In this tutorial you will obtain data from existing BellaDati chart view using Client API and create a custom visualization.

Result will look like this:

Image Modified

Info

All of this will be accomplished using BellaDati Client API, JavaScript, JQuery and HTML5 canvas.

 

Tutorial parts:

  • Working with BellaDati Extensions
  • Getting data from BellaDati chart view
  • Creating custom chart

 

Working with BellaDati Extensions

Before you start with Client API you have to learn how create BellaDati Extension.

Extensions allow you to to customize BellaDati front-end functions or create custom content like charts or dashboards.


This function is available for System administrators. Access it by going to Administration - Advanced settings - Extensions.

Image Modified

Extensions consist of three parts: External JavaScript libraries, code that is appended to the end of head element content and code that is appended to the beginning of body.

 

First give your extension a name.

Image Modified

Next create layout of the Extension where the custom chart will be placed:

Code Block
languagexml
<div id="wrapper">
<div id="view1" class="view">
<canvas id="canvas1" class="canvas" width="250" height="250"></canvas>
</div>
<div id="view2" class="view">
<canvas id="canvas2" class="canvas" width="250" height="250"></canvas>	
</div>
<div id="view3" class="view">
<canvas id="canvas3" class="canvas" width="250" height="250"></canvas>	
</div>
</div>
Info

Place the code to the Beginning of the body.

 

Then style the layout:

Code Block
languagecss
<style>
body {
	background-color: #F5F5F5 ;
}
	
.canvas {
    display: block;
    padding-left: 0;
    padding-right: 0;
    margin-left: auto;
    margin-right: auto;
  	padding-top: 10%;
}
.view {
	display: inline-block;
}
#wrapper {
	width: 760px;
	height: 300px;
	display: block;
    padding-left: 0;
    padding-right: 0;
    margin-left: auto;
    margin-right: auto;
    background-color: #fff;
    border: 1px solid #ccc;
}
</style>
Info

Place the code to the End of head.

 

Extension is now created so you can continue with the next part.

 

Getting data from BellaDati

This example is based on Speedometer chart. To keep it simple you will work only with one indicator value.

For this case let's assume that there is a formula indicator calculating a percentage value - so the value you get is always in 0 - 100 range.

 

You can download this BellaApp and upload it into BellaDati. It contains data set and report created following steps below.

  • Create a data set with three indicators and insert a value between 0 - 100 using browse data.
  • In the next step create a report based on previously created data set.
  • Insert 3 views and to each add a Speedometer chart with previously created indicator.

 

Everything is now ready so you can start with getting the data from the chart view.

Info

User has to have access permission to the source report. Otherwise the data from the chart cannot be obtained.

 

Click on the view from which you want to get a value with right mouse button and select "Inspect".

Image Modified

You can find the view's ID in the element of this view. It looks like "8313-0dt2jiMsXY" with "8313" being ID of the report and "0dt2jiMsXY" being ID of the view.

Image Modified

 

Now to loading the data.

Create a function with Ajax call to get data from the chart using the viewDetail endpoint so it can be later used for rendering a chart. See http://support.belladati.com/techdoc/Report+API for all Report API endpoints.

Code Block
languagejs
function loadData() {

			$.ajax({ 
	                    url: "/bi/report/api:viewDetail/reportID-viewID",	// for example "/bi/report/api:viewDetail/8306-Ua0dyvh7lc"
	                    success: function(chartReply) {
	                      var value = chartReply.data.elements[0].values[0].value; // assign the value from the chart to variable
	                      console.log(value);	// check if the code above is correct
	                    }
	                  });
		}
	loadData();

Chart data are returned in a JSON and the indicator value can be accessed using this path:

Code Block
languagejs
var value = chartReply.data.elements[0].values[0].value;

 

 

Check in the JavaScript console if your code is correct and you can see the value of the indicator from the chart.

Image Modified

 

Creating custom chart

Now that you have the value you can start building a chart.

In this example is a simple gauge created using HTML5 canvas.

In this custom chart the value you get from a chart is used to calculate radius of the gauge that will be filled and is also displayed as a percentual value inside of the gauge.

The value is rounded to the nearest integer to make it more clean in this example.

createGauge function that creates the chart has 4 parameters. ID of the canvas element in your Extension, ID of the chart view in BellaDati, color of the Guage background and color of the space that is filled.

Code Block
languagejs
function createGauge(canvasID, viewID, color, bgcolor)
	{
		//canvas initialization
		var canvas = document.getElementById(canvasID);
		var ctx = canvas.getContext("2d");
		//dimensions
		var W = canvas.width;
		var H = canvas.height;
		//Variables
		var chartData;
		var degrees = 0;
		var new_degrees = 0;
		var difference = 0;
		var text;
		var animation_loop;

		//previsouly created function to load the value
		function loadData() {
			$.ajax({ 
	                    url: "/bi/report/api:viewDetail/" + viewID,
	                    success: function(chartReply) {
	                      var value = chartReply.data.elements[0].values[0].value;
	                      chartData = Math.round(value*3.6);	//transform the value to degrees and round it
	                      draw();                      
	                    }
	                  });
					}
		loadData();
		
		function init()
		{
			//Clear the canvas everytime a chart is drawn
			ctx.clearRect(0, 0, W, H);
			
			//Background 360 degree arc
			ctx.beginPath();
			ctx.strokeStyle = bgcolor;	// color of the background
			ctx.lineWidth = 10;
			ctx.arc(W/2, H/2, 100, 0, Math.PI*2, false);
			ctx.stroke();
			
			//gauge will be a simple arc
			//Angle in radians = angle in degrees * PI / 180
			var radians = degrees * Math.PI / 180;
			ctx.beginPath();
			ctx.strokeStyle = color;	//color of the filled space
			ctx.lineWidth = 10;
			//The arc starts from the rightmost end. If we deduct 90 degrees from the angles
			//the arc will start from the topmost end
			ctx.arc(W/2, H/2, 100, 0 - 90*Math.PI/180, radians - 90*Math.PI/180, false); 
			//you can see the arc now
			ctx.stroke();
			
			//Lets add the text
			ctx.fillStyle = "#868686";	//color of the text
			ctx.font = "50px Abel";
			text = Math.round(degrees/360*100) + "%";
			//center the text
			//deducting half of text width from position x
			text_width = ctx.measureText(text).width;
			//adding manual value to position y
			ctx.fillText(text, W/2 - text_width/2, H/2 + 15);
		}
		
		function draw()
		{
			//Cancel any movement animation if a new chart is requested
			if(typeof animation_loop != undefined) clearInterval(animation_loop);
			
			//get degrees that could be filled
			new_degrees = chartData;
			difference = new_degrees - degrees;
			//This will animate the gauge to new position
			//The animation will take 1 second
			//time for each frame is 1sec / difference in degrees
			animation_loop = setInterval(animate_to, 1000/difference);
		}
		
		//function to make the chart move to new degrees
		function animate_to()
		{
			//clear animation loop if degrees reaches to new_degrees
			if(degrees == new_degrees || new_degrees === undefined) 
				clearInterval(animation_loop);
					
			if(degrees < new_degrees)
				degrees++;
		
			init();
		}
	}

 

Create gauges in canvas elements using these parameters:

Code Block
languagejs
function renderGauges() 
		{
		 createGauge("canvas1", "viewId1", "#AEEA2E", "#E6F5BE"); // e.g.: createGauge("canvas1", "8306-k1LELScByj", "#AEEA2E", "#E6F5BE"); 
		 createGauge("canvas2", "viewId2", "#1EBBD4", "#C1EEF4"); // make sure you are using correct ID of the view  
		 createGauge("canvas3", "viewId3", "#B967C9", "#EED9F0");
		} 
		renderGauges();

 

As the last step add font that is used inside of the gauge:

Code Block
languagexml
<link href='https://fonts.googleapis.com/css?family=Abel' rel='stylesheet' type='text/css'>
Info

Place the code to the End of head.

 

You have created a view with multiple charts displaying live data from BellaDati. This view can be used for example in custom reports or dashboards.

Complete code is available here - End of Head + Beginning of body.

Sv translation
languageja

このチュートリアルでは、クライアントAPIを使用して、既存のBellaDati可視化ビューからデータを取得し、カスタムグラフを作成する。

その結果がこのようなダッシュボードに統合される。

Image Added

Info

このすべてがBellaDatiクライアントAPI、JavaScript、jQueryとHTML5のキャンバスを使用して達成される。

 

チュートリアルパーツ:

  • BellaDati拡張機能で作業
  • BellaDatiチャートビューからデータを取得
  • カスタムチャートを作成

 

BellaDati拡張機能で作業

クライアントAPIで開始する前に、BellaDatiエクステンションを作成方法を学習する必要がある。

拡張機能を使用すると、BellaDatiフロントエンド機能をカスタマイズしたり、チャートやダッシュボードなどのカスタムコンテンツを作成することを可能にする。


この機能は、システム管理者のために利用可能です。拡張-拡張設定 -  拡張機能-管理に行くことによるアクセスする。

Image Added

拡張機能は、3つの部分で構成されている:外部JavaScriptライブラリ、コード本文の先頭に追加されたヘッド素子コンテンツや末尾に追加されたコード。

 

まず、あなたの拡張機能に名前を付ける。

Image Added

次のカスタムチャートが配置される拡張機能のレイアウトを作成する。

Code Block
languagexml
<div id="wrapper">
<div id="view1" class="view">
<canvas id="canvas1" class="canvas" width="250" height="250"></canvas>
</div>
<div id="view2" class="view">
<canvas id="canvas2" class="canvas" width="250" height="250"></canvas>	
</div>
<div id="view3" class="view">
<canvas id="canvas3" class="canvas" width="250" height="250"></canvas>	
</div>
</div>
Info

本文の先頭にコードを配置する。

 

その後レイアウトのスタイルする

Code Block
languagecss
<style>
body {
	background-color: #F5F5F5 ;
}
	
.canvas {
    display: block;
    padding-left: 0;
    padding-right: 0;
    margin-left: auto;
    margin-right: auto;
  	padding-top: 10%;
}
.view {
	display: inline-block;
}
#wrapper {
	width: 760px;
	height: 300px;
	display: block;
    padding-left: 0;
    padding-right: 0;
    margin-left: auto;
    margin-right: auto;
    background-color: #fff;
    border: 1px solid #ccc;
}
</style>
Info

ヘッドのエンドにコードを配置する。

 

拡張が次の部分を続けることができるように作成されない。

 

BellaDatiからデータを取得

この例では、スピードメーターチャートに基づく。シンプルに唯一のインジケータ値で動作することだけ

この例に対してあなたが得る値は0- 100の範囲このケースでのがパーセント値を計算式インジケータがあると仮定しましょう。

 

このBellaAppをダウンロードして、BellaDatiにそれをアップロードできる。これはデータセットが含まれて、作成したレポートが次の手順の通りです。

  • 3つのインジケータを用いてデータセットを作成し、- ブラウズ・データを使用している0-100値を挿入する。
  • 次のステップでは、以前に作成したデータセットに基づいてレポートを作成する。
  • 3つのビューを挿入し、それぞれに、以前に作成した指標でスピードメーターチャートを追加する。

 

すべての準備が整いましたのであなたがチャートビューからデータを取得して起動できる。

Info

ユーザーがソースレポートへのアクセス権を持っている必要がある。そうでなければ、チャートからデータを得ることができない。

 

マウスの右ボタンで値を取得したいビューフォームをクリックして、「Inspect」を選択する。

Image Added

あなたは、このビューの要素にビューのIDを見つけることができる。それは、「8313-0dt2jiMsXY」と「8313」レポートのあるIDとビューの「0dt2jiMsXY」というIDのように見える。

Image Added

 

今すぐデータをロードする。

それは後でチャートを描画するために使用することができるようviewDetailエンドポイントを使用してグラフからデータを取得するためにAjax呼び出しを持つ関数を作成する。すべてのレポートのAPIエンドポイントのhttp://support.belladati.com/techdoc/Report+APIを参照してください。

Code Block
languagejs
function loadData() {

			$.ajax({ 
	                    url: "/bi/report/api:viewDetail/reportID-viewID",	// for example "/bi/report/api:viewDetail/8306-Ua0dyvh7lc"
	                    success: function(chartReply) {
	                      var value = chartReply.data.elements[0].values[0].value; // assign the value from the chart to variable
	                      console.log(value);	// check if the code above is correct
	                    }
	                  });
		}
	loadData();

チャートデータはJSONで返され、指標値はこのパスを使用してアクセスできる。

Code Block
languagejs
var value = chartReply.data.elements[0].values[0].value;

 

 

あなたのコードが正しいかどうかのJavaScriptコンソールで確認してください、あなたは、チャートからのインジケータの値を見ることができる。

Image Added

 

カスタムチャート作成

 

今、あなたが値を持って、チャートの構築を開始できる。

この例では、HTML5のキャンバスを使用することで作成された簡単なゲージです。

このカスタムでチャートから取得する値がゲージの内側パーセント値として表示されるゲージの半径を計算するために使用される。

この例では、よりきれいにするために、値が最も近い整数に丸められる。

チャートを作成するcreateGauge機能は4つのパラメータがある。あなたの拡張機能でcanvas要素のID、BellaDatiにチャートビューのID、ゲージの背景の色と空間での色が満たされた。

Code Block
languagejs
function createGauge(canvasID, viewID, color, bgcolor)
	{
		//canvas initialization
		var canvas = document.getElementById(canvasID);
		var ctx = canvas.getContext("2d");
		//dimensions
		var W = canvas.width;
		var H = canvas.height;
		//Variables
		var chartData;
		var degrees = 0;
		var new_degrees = 0;
		var difference = 0;
		var text;
		var animation_loop;

		//previsouly created function to load the value
		function loadData() {
			$.ajax({ 
	                    url: "/bi/report/api:viewDetail/" + viewID,
	                    success: function(chartReply) {
	                      var value = chartReply.data.elements[0].values[0].value;
	                      chartData = Math.round(value*3.6);	//transform the value to degrees and round it
	                      draw();                      
	                    }
	                  });
					}
		loadData();
		
		function init()
		{
			//Clear the canvas everytime a chart is drawn
			ctx.clearRect(0, 0, W, H);
			
			//Background 360 degree arc
			ctx.beginPath();
			ctx.strokeStyle = bgcolor;	// color of the background
			ctx.lineWidth = 10;
			ctx.arc(W/2, H/2, 100, 0, Math.PI*2, false);
			ctx.stroke();
			
			//gauge will be a simple arc
			//Angle in radians = angle in degrees * PI / 180
			var radians = degrees * Math.PI / 180;
			ctx.beginPath();
			ctx.strokeStyle = color;	//color of the filled space
			ctx.lineWidth = 10;
			//The arc starts from the rightmost end. If we deduct 90 degrees from the angles
			//the arc will start from the topmost end
			ctx.arc(W/2, H/2, 100, 0 - 90*Math.PI/180, radians - 90*Math.PI/180, false); 
			//you can see the arc now
			ctx.stroke();
			
			//Lets add the text
			ctx.fillStyle = "#868686";	//color of the text
			ctx.font = "50px Abel";
			text = Math.round(degrees/360*100) + "%";
			//center the text
			//deducting half of text width from position x
			text_width = ctx.measureText(text).width;
			//adding manual value to position y
			ctx.fillText(text, W/2 - text_width/2, H/2 + 15);
		}
		
		function draw()
		{
			//Cancel any movement animation if a new chart is requested
			if(typeof animation_loop != undefined) clearInterval(animation_loop);
			
			//get degrees that could be filled
			new_degrees = chartData;
			difference = new_degrees - degrees;
			//This will animate the gauge to new position
			//The animation will take 1 second
			//time for each frame is 1sec / difference in degrees
			animation_loop = setInterval(animate_to, 1000/difference);
		}
		
		//function to make the chart move to new degrees
		function animate_to()
		{
			//clear animation loop if degrees reaches to new_degrees
			if(degrees == new_degrees || new_degrees === undefined) 
				clearInterval(animation_loop);
					
			if(degrees < new_degrees)
				degrees++;
		
			init();
		}
	}

 

これらのパラメータを使用して、canvas要素でゲージを作成する。

Code Block
languagejs
function renderGauges() 
		{
		 createGauge("canvas1", "viewId1", "#AEEA2E", "#E6F5BE"); // e.g.: createGauge("canvas1", "8306-k1LELScByj", "#AEEA2E", "#E6F5BE"); 
		 createGauge("canvas2", "viewId2", "#1EBBD4", "#C1EEF4"); // make sure you are using correct ID of the view  
		 createGauge("canvas3", "viewId3", "#B967C9", "#EED9F0");
		} 
		renderGauges();

 

最後のステップとして計の内部で使用されているフォントを追加する。

Code Block
languagexml
<link href='https://fonts.googleapis.com/css?family=Abel' rel='stylesheet' type='text/css'>
Info

ヘッドのエンドにコードを配置する。

 

あなたはBellaDatiからライブデータを表示する複数のチャートを持つビューを作成した。このビューは、カスタムレポートやダッシュボードの例に使用できる。

完全なコードはここにある - ヘッドの終わり+ボディの始まり