Adding Special Characters
FusionCharts offers multiple options to format numbers on the chart. You can configure number prefixes and suffixes, decimal places, and scale numbers based on a predefined scale. This article focuses on how you customize the prefix and suffix of the numbers on the chart using ember-fusioncharts component.
To customize the prefix and suffix of the numbers on the chart, use the following attributes:
Specify the prefix for all the values on the chart using the
numberPrefixattribute. Note that the value of this attribute works only if you don't specifically mention the values of theyNumberPrefixandxNumberPrefixattributes.Specify the prefix for all the Y-axis values on the chart using the
yNumberPrefixattribute. If you don't mention this attribute, the chart will inherit the default value from thenumberPrefixattribute.Specify the prefix for all the X-axis values on the chart using the
xNumberPrefixattribute. If you don't mention this attribute, the chart will inherit the default value from thenumberPrefixattribute.Specify the suffix for all the values on the chart using the
numberSuffixattribute. Note that the value of this attribute works only if you don't specifically mention the values of theyNumberSuffixandxNumberSuffixattributes.Specify the suffix for all the Y-axis values on the chart using the
yNumberSuffixattribute. If you don't mention this attribute, the chart will inherit the default value from thenumberSuffixattribute.Specify the suffix for all the X-axis values on the chart using the
xNumberSuffixattribute. If you don't mention this attribute, the chart will inherit the default value from thenumberSuffixattribute.
A chart configured to customize the prefix of the numbers on the chart is shown below:
{
"chart": {
"caption": "Monthly revenue for last year",
"subCaption": "Harry's SuperMart",
"xAxisName": "Month",
"yAxisName": "Revenues (In USD)",
"numberPrefix": "$",
"theme": "fusion"
},
"data": [
{
"label": "Jan",
"value": "420000"
},
{
"label": "Feb",
"value": "810000"
},
{
"label": "Mar",
"value": "720000"
},
{
"label": "Apr",
"value": "550000"
},
{
"label": "May",
"value": "910000"
},
{
"label": "Jun",
"value": "510000"
},
{
"label": "Jul",
"value": "680000"
},
{
"label": "Aug",
"value": "620000"
},
{
"label": "Sep",
"value": "610000"
},
{
"label": "Oct",
"value": "490000"
},
{
"label": "Nov",
"value": "900000"
},
{
"label": "Dec",
"value": "730000"
}
]
}
Setup ember-cli-build.js
In this step we will include all the necessary files and add the dependency to create the chart. The code is given below:
/* eslint-env node */
'use strict';
const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
module.exports = function (defaults) {
let app = new EmberAddon(defaults, {
// Add options here
});
// Import FusionCharts library
app.import('bower_components/fusioncharts/fusioncharts.js');
app.import('bower_components/fusioncharts/fusioncharts.charts.js');
app.import('bower_components/fusioncharts/themes/fusioncharts.theme.fusion.js');
return app.toTree();
};
In the above code, include the necessary libraries and components using import. For example, ember-fusioncharts, fusioncharts, etc.
If you need to use different assets in different environments, specify an object as the first parameter. That object's keys should be the environment name and the values should be the asset to use in that environment.
Add chart data to chart-viewer.js
Add the following code to chart-viewer.js:
import Component from '@ember/component';
export default Component.extend({
id: 'run-time-chart',
width: 700,
height: 400,
type: 'column2d',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "Monthly revenue for last year",
"subCaption": "Harry's SuperMart",
"xAxisName": "Month",
"yAxisName": "Revenues (In USD)",
"numberPrefix": "$",
"theme": "fusion"
},
"data": [{
"label": "Jan",
"value": "420000"
}, {
"label": "Feb",
"value": "810000"
}, {
"label": "Mar",
"value": "720000"
}, {
"label": "Apr",
"value": "550000"
}, {
"label": "May",
"value": "910000"
}, {
"label": "Jun",
"value": "510000"
}, {
"label": "Jul",
"value": "680000"
}, {
"label": "Aug",
"value": "620000"
}, {
"label": "Sep",
"value": "610000"
}, {
"label": "Oct",
"value": "490000"
}, {
"label": "Nov",
"value": "900000"
}, {
"label": "Dec",
"value": "730000"
}]
},
actions: {
changeChart(value) {
FusionCharts.items['run-time-chart'].chartType(value);
}
}
});
In the above code:
Create a chart component to render the chart.
Store the chart configuration in a JSON object. In the JSON object:
- Set the chart type as
column2d. Find the complete list of chart types with their respective alias here . - Set the width and height of the chart in pixels.
- Set the
dataFormatas JSON. - Embed the json data as the value of
dataSource.
- Set the chart type as
In the
dataSourceobject, addnumberPrefixattribute inchartobject. Set thenumberPrefixto$.Add the
changeChart()function to change the chart type at runtime.
Add data to chart-viewer.hbs
Add the following code to chart-viewer.hbs:
{{fusioncharts-xt
id=id
width=width
height=height
type=type
dataFormat=dataFormat
dataSource=dataSource
}}
In the above code add the fusioncharts component to render the chart.