JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

FusionCharts : Beautifying the Column Charts

August 7, 2015 by Mohamed Sanaulla Leave a Comment

In the previous post I stopped at building the column chart using FusionCharts library. In this post I am going to show you how to beautify the chart by changing its appearance namely background, border, canvas background, canvas border and data plot appearance. If you are coming to this post directly then I would recommend you to read this post to setup the data for the chart and also to display the basic version of the chart.

also read:

  • Using JavaFX Chart API to add charts to Swing applications
  • Pie Charts using DOJO
  • PrimeFaces Charts – Pie Chart + Ajax Behavior Events Example

In this post I am going to show you how to:

  1. Change the background and border of the chart.
  2. Change the background and border of the canvas of the chart.
  3. Change the background and border of the data plot.

I am going to create 3 buttons to perform each of the above 3 tasks. For that I have updated the column_chart_sample.html as shown below:

[code lang=”html” highlight=”17-43″]
<!DOCTYPE html>
<html>
<head>
<title>Sample for Column Chart using FusionChart</title>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h3>Top 10 run scorers in ODI cricket in the year 2015</h3>
<!– Container for rendering the chart –>
<div id="chart-container"></div>
</div>
</div>
<!– Buttons for changing the appearance of the chart –>
<div class="row">
<div>
<button type="button" id="chartBorderBtn" class="col-md-2 btn btn-primary">
Change chart border
</button>
</div>
<div>
<button type="button" id="chartBackgroundBtn" class="col-md-2 col-md-offset-1 btn btn-primary">
Change chart background
</button>
</div>
</div>
<br/>
<div class="row">
<div>
<button type="button" id="canvasBorderBtn" class="col-md-2 btn btn-primary">
Change canvas border
</button>
</div>
<div>
<button type="button" id="canvasBackgroundBtn" class="col-md-2 col-md-offset-1 btn btn-primary">
Change canvas background
</button>
</div>
</div>
<br/>
<div class="row">
<div>
<button type="button" id="dataPlotBorderBtn" class="col-md-2 btn btn-primary">
Change data plot border
</button>
</div>
<div>
<button type="button" id="dataPlotBackgroundBtn" class="col-md-2 col-md-offset-1 btn btn-primary">
Change data plot background
</button>
</div>
</div>
</div>
<!– jQuery Javascript –>
<script src="js/jquery-2.1.4.js"></script>

<script src="js/bootstrap.js"></script>
<!– FusionCharts related Javascript library –>
<script src="js/fusioncharts.js"></script>
<script src="js/fusioncharts.charts.js"></script>
<script src="js/fusioncharts.theme.zune.js"></script>
<script src="js/column_chart_sample.js"></script>
</body>
</html>
[/code]

The above highlighted code is the new addition. Loading the updated HTML results in the display as shown below:
FusionChart ColumnChart 1The javascript code also needs to be updated to accommodate the handlers for the new buttons. The updated code for column_chart_sample.js is as shown below:

[code lang=”javascript” highlight=”3-25,58-63″]
var chartData;
$(function(){
$("#chartBorderBtn").click(function(){
changeChartBorder();
});

$("#chartBackgroundBtn").click(function(){
changeChartBackground();
});

$("#canvasBorderBtn").click(function(){
changeCanvasBorder();
});

$("#canvasBackgroundBtn").click(function(){
changeCanvasBackground();
});

$("#dataPlotBorderBtn").click(function(){
changeDataPlotBorder();
});

$("#dataPlotBackgroundBtn").click(function(){
changeDataPlotBackground();
});

$.ajax({

url: ‘http://localhost:3300/scores’,
type: ‘GET’,
crossDomain: true,
success : function(data) {
chartData = data;
var chartProperties = {
"caption": "Top 10 run scorers in ODI Cricket in 2015",
"xAxisName": "Player",
"yAxisName": "Runs Scored",
"rotatevalues": "1",
"theme": "zune"
};

var apiChart = new FusionCharts({
type: ‘column2d’,
renderAt: ‘chart-container’,
width: ‘550’,
height: ‘350’,
dataFormat: ‘json’,
dataSource: {
"chart": chartProperties,
"data": chartData
}
});
apiChart.render();
}
});
});

function changeChartBorder(){}
function changeChartBackground(){}
function changeCanvasBorder(){}
function changeCanvasBackground(){}
function changeDataPlotBorder(){}
function changeDataPlotBackground(){}
[/code]

In the above javascript code the updated code has been highlighted.

In the rest of the post I would be writing the code for the 6 button handlers and during that show you how to make use of the different chart properties.

Changing the chart border

FusionCharts API provides an API setChartAttribute to modify the chart properties. The API takes either two parameters i.e the property name and its value OR one parameter which is the JSON object of the chart properties. I would be using the latter approach of building the JSON object of properties. Below is the JSON object of properties:

[code lang=”javascript”]
var borderProperties = {
//whether to show the border or not.
"showBorder":1,
//color of the border
"borderColor":"#000000",
//thickness of the border
"borderThickness":3,
//opacity of the border
"borderAlpha":90
}
[/code]

The complete implementation of the changeChartBorder method is shown below:

[code lang=”javascript”]
function changeChartBorder(){
var borderProperties = {
//whether to show the border or not.
"showBorder":1,
//color of the border
"borderColor":"#000000",
//thickness of the border
"borderThickness":3,
//opacity of the border
"borderAlpha":90
}
apiChart.setChartAttribute(borderProperties);
}
[/code]

Let us reload the page again and click on the Change chart border button to see the below changes applied:
FusionChart ColumnChart 2

Changing chart background

To fill the chart background we can use a single color, gradient and an image. To demonstrate I would make use of a gradient fill. A gradient fill would require more than 1 color and the ratio of contribution of each color to fill the background. The properties JSON object of chart background would be as shown below:

[code lang=”javascript”]
var backgroundProperties = {
//colors to be used for chart background
"bgColor":"EEEEEE,CCCCCC",
//transperancy of each color
"bgAlpha" : "70,80",
//contribution of each color to the gradient of the background
"bgRatio":"60, 40",
//setting the canvas area as transperant
"canvasBgAlpha":0
};
[/code]

The complete imlementation of changeChartBackground is shown below:

[code lang=”javascript”]
function changeChartBackground(){
var backgroundProperties = {
//colors to be used for chart background
"bgColor":"EEEEEE,CCCCCC",
//transperancy of each color
"bgAlpha" : "70,80",
//contribution of each color to the gradient of the background
"bgRatio":"60, 40",
//setting the canvas area as transperant
"canvasBgAlpha":0
};
apiChart.setChartAttribute(backgroundProperties);
}
[/code]

Let us reload the page and click on the Change chart background button to see changes. The chart after applying the chart background looks like:
FusionChart ColumnChart 3

Changing canvas border

Canvas is that area of the chart which contains the x-axis and y-axis and the are between the axis. The below is the JSON object for the canvas border properties:

[code lang=”javascript”]
var canvasBorderProperties = {
//showing canvas border or not
"showCanvasBorder" : 1,
//border of the canvas
"canvasBorderColor" : "#666666",
//thickness of the canvas border
"canvasBorderThickness": 3,
//opacity of the canvas background
"canvasBorderAlpha":80
};
[/code]

The implementation of changeCanvasBorder() method is given below:

[code lang=”javascript”]
function changeCanvasBorder(){
var canvasBorderProperties = {
//showing canvas border or not
"showCanvasBorder" : 1,
//border of the canvas
"canvasBorderColor" : "#666666",
//thickness of the canvas border
"canvasBorderThickness": 3,
//opacity of the canvas background
"canvasBorderAlpha":80
};
apiChart.setChartAttribute(canvasBorderProperties);
}
[/code]

The chart after application of canvas border looks like below:
FusionChart ColumnChart 4

Changing the canvas background

The below JSON object represents the properties for modifying the canvas background:

[code lang=”javascript”]
var canvasBackgroundProperties = {
//background color for the canvas
"canvasBgColor":"#1790e1",
//opacity of the background color
"canvasBgAlpha" : 30
};
[/code]

In this case I am not using a gradient to fill the canvas, instead using single color. The implementation of the changeCanvasBackground method is shown below:

[code lang=”javascript”]
function changeCanvasBackground(){
var canvasBackgroundProperties = {
//background color for the canvas
"canvasBgColor":"#1790e1",
//opacity of the background color
"canvasBgAlpha" : 30
};
apiChart.setChartAttribute(canvasBackgroundProperties);
}
[/code]

The chart after applying the canvas background looks like:
FusionChart ColumnChart 5

Changing data plot border

Data plot is that area which plots the data on the canvas. It is the columns in case of column chart or lines in case of line graph, pie in case of pie chart and so on. We can configure its border as well as its background. The below properties are for configuring its border:

[code lang=”javascript”]
var dataPlotBorderProperties(){
//whether to show the border for data plot
"showPlotBorder" : 1,
//whether the data plot border is dashed or not
"plotBorderDashed":1,
//length of the dash in the data plot border
"plotBorderDashLen": 5,
//gap between the dash in the data plot border
"plotBorderDashGap": 5
};
[/code]

The implementation of the changeDataPlotBorder() method is shown below:

[code lang=”javascript”]
function changeDataPlotBorder(){
var dataPlotBorderProperties(){
//whether to show the border for data plot
"showPlotBorder" : 1,
//whether the data plot border is dashed or not
"plotBorderDashed":1,
//length of the dash in the data plot border
"plotBorderDashLen": 5,
//gap between the dash in the data plot border
"plotBorderDashGap": 5
};
apiChart.setChartAttribute(dataPlotBorderProperties);
}
[/code]

The chart with the data plot border looks like below:
FusionChart ColumnChart 6

Changing the data plot background

The JSON object for data plot background properties is given below:

[code lang=”javascript”]
var dataPlotBackgroundProperties = {
//list of colors to be applied to the columns in the data plot
"paletteColors" : "#EED17F,#2d3c45,#074868,#B0D67A,#2C560A,#DD9D82",
//setting the transperancy of the data plots
"plotFillAlpha" : 90
};
[/code]

One can even fill gradient inside the data plot, but to keep it simple and demonstrate the approach I am going to fill it with different colors. The implementation of the changeDataPlotBackground() method is shown below:

[code lang=”javascript”]
function changeDataPlotBackground(){
var dataPlotBackgroundProperties = {
//list of colors to be applied to the columns in the data plot
"paletteColors" : "#EED17F,#2d3c45,#074868,#B0D67A,#2C560A,#DD9D82",
//setting the transperancy of the data plots
"plotFillAlpha" : 90
};
apiChart.setChartAttribute(dataPlotBackgroundProperties);
}
[/code]

The chart with data plot background looks like below:
FusionChart ColumnChart 7

In the above examples, I have left a few properties so as to keep the article simple and still convey the approach to apply different properties. To view all the available properties:

  • For chart border and background, click here.
  • For canvas, click here.
  • For data plot, click here.

Filed Under: JavaScript Tagged With: fusioncharts

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved