First Pie Chart
Let’s start with the data store. In this tutorial, I am getting the data from a remote URL. My server returns me a set of JSON object with product name and their sales value. Here is the data store:
var store = new Ext.data.JsonStore({ fields:['product','sales'], root: 'rec', url: ‘/piechart/data/’ });Unlike other charts, pie charts do not have axis. So, instead of using xField and yField properties of charts to pass data, we have dataField and categoryField in pie charts. Here is the code for pie chart:
new Ext.Panel({ title: 'Pie Chart', renderTo: 'chartDiv', width:400, height:300, items: { xtype: 'piechart', store: store, dataField: 'sales', categoryField: 'product' } });Very simple indeed! ExtJS does all the necessary calculations even if you don’t represent the data in percentage.
Adding Legend
Now, let’s add a legend to this chart. Without it, there is no much use in displaying a chart. An important property for styling is extraStyle. It helps you to add extra styles that will add or override default styles of the chart. Here is the final code for the pie chart and panel:
new Ext.Panel({ title: 'The Pie Chart', renderTo: 'chartDiv', width:400, height:300, items: { xtype: 'piechart', store: store, dataField: 'sales', categoryField: 'product', extraStyle: { legend:{ display: 'right', padding: 10, border:{ color: '#CBCBCB', size: 1 } } } } });While displaying the legend,notice that I have also made some beautification to it. I have provided a thin border using a specific color. There is no chart specific code for displaying legend, so the same code will work for other types of charts as well.
Custom Colors in your Pie chart
By default, ExtJS picks out the colors when rendering the chart. You might find these color shade dull. Lets try to brighten up our chart. I am going to apply a color scheme called Esprit_80s. In order to apply, we need to basically modify the style of series. So we will modify seriesStyles property of our pie chart:
new Ext.Panel({ title: 'The Pie Chart', renderTo: 'chartDiv', width:600, height:300, items: { xtype: 'piechart', store: store, dataField: 'sales', categoryField: 'product', seriesStyles: { colors:['#F8869D','#25CDF2', '#FFAA3C','#DEFE39', '#AB63F6'] //eSprit 80s }, extraStyle: { legend:{ display: 'right', padding: 10, border:{ color: '#CBCBCB', size: 1 } } } } });
Note that the property colors is used only for pie charts. If you need to change the colors for other charts you will have to use the color property.
Textures & images in your Pie chart
ExtJS also provides us the ability to use images instead of color. So, if you have few texture images or any other images, you can easily substitute them for colors. This is possible through the images property of seriesStyles. I have five simple texture images and here is how I am adding them to our pie chart:
new Ext.Panel({ title: 'The Pie Chart', renderTo: 'chartDiv', width:600, height:300, items: { xtype: 'piechart', store: store, dataField: 'sales', categoryField: 'product', seriesStyles: { images:['images/one.png', 'images/three.png', 'images/two.png', 'images/four.png', 'images/five.png'] }, extraStyle: { legend:{ display: 'bottom', padding: 10, border:{ color: '#CBCBCB', size: 1 } } } } });
Apart from these properties, extraStyle can be used to change the font styles as well. Have a look at the APIs and try out different customizations. I will be back with more customization tutorials on ExtJS charts. Let me know your feedback either through comments or using the contact form.
Learn how to Handle events in ExtJS charts or read other articles on ExtJS.