{"id":40,"date":"2016-05-06T08:54:38","date_gmt":"2016-05-06T08:54:38","guid":{"rendered":"http:\/\/symbioticindia.in\/docu\/?p=40"},"modified":"2016-05-06T08:54:38","modified_gmt":"2016-05-06T08:54:38","slug":"get-started-with-j2me-chartcomponent","status":"publish","type":"post","link":"http:\/\/symbioticindia.in\/docu\/2016\/05\/06\/get-started-with-j2me-chartcomponent\/","title":{"rendered":"Get started with J2me ChartComponent"},"content":{"rendered":"<div align=\"center\"><strong>Introduction<\/strong><\/div>\n<p><a href=\"http:\/\/www.beanizer.org\/index.php3?page=chartcomp\">J2me Chart Component<\/a> is a free MIDP2.0 library, aimed at helping J2ME developers to easily add charts to their personal or commercial midlets. I&#8217;m writing this article because some people have asked for a simple kickstarting tutorial on how to use the library. I hope this will be useful and that will also help ChartComponent expand its user-base.<\/p>\n<div align=\"center\"><strong>Overview<\/strong><\/div>\n<p>Since version 1.5 of ChartComponent, developers can choose to use it in two different ways:<\/p>\n<p>1) As a UI widget (PieChart, VBarChart, HBarChart,LineChart)<br \/>\n2) As a drawing library, using custom Graphics objects as targets<\/p>\n<p>Here we&#8217;ll explore both usages. We&#8217;ll use a Form with A Chart widget in the first example, and a Form with an Image in the second. This second usage allows also to export or manipulate created charts.<br \/>\nBefore you read on, please dowload the library <a href=\"http:\/\/www.beanizer.org\/download.php?file=chartcomp\">here<\/a> and make sure you add it to your classpath.<\/p>\n<p><strong>The code<\/strong><br \/>\nWe are going to code a minimal midlet with a form containing a vertical bar-chart. Here is the code:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"><span style=\"color: #ffffcc;\">   1:<\/span><span style=\"color: #00cc99;\">import<\/span> javax.microedition.midlet.*; <span style=\"color: #ffffcc;\">\r\n   2:<\/span><span style=\"color: #00cc99;\">import<\/span> javax.microedition.lcdui.*; <span style=\"color: #ffffcc;\">\r\n   3:<\/span><span style=\"color: #00cc99;\">import<\/span> java.util.*; <span style=\"color: #ffffcc;\">\r\n   4:<\/span> <span style=\"color: #00cd00;\">\r\n   5:<\/span><span style=\"color: #00cc99;\">import<\/span> org.beanizer.j2me.charts.VBarChart; <span style=\"color: #ffffcc;\">\r\n   6:<\/span> <span style=\"color: #ffffcc;\">\r\n   7:<\/span><span style=\"color: #ffa500;\">public<\/span> <span style=\"color: #cc6600;\">class<\/span> ChartExample1 <span style=\"color: #ffa500;\">extends<\/span> MIDlet <span style=\"color: #ffa500;\">implements<\/span> CommandListener <span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n   8:<\/span>    <span style=\"color: #ffa500;\">private<\/span> Display display; <span style=\"color: #ffffcc;\">\r\n   9:<\/span>    <span style=\"color: #ffa500;\">private<\/span> Form mainForm; <span style=\"color: #00cd00;\">\r\n  10:<\/span>    <span style=\"color: #ffa500;\">private<\/span> <span style=\"color: #ffa500;\">final<\/span> <span style=\"color: #ffa500;\">static<\/span> Command CMD_EXIT <span style=\"color: #00ffff;\">=<\/span> <span style=\"color: #ffa500;\">new<\/span> <span style=\"color: #b2dfee;\">Command<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">Exit<\/span><span style=\"color: #00cd00;\">\"<\/span>, Command.EXIT, <span style=\"color: #cdcd00;\">1<\/span>); <span style=\"color: #ffffcc;\">\r\n  11:<\/span>    <span style=\"color: #ffa500;\">final<\/span> VBarChart item<span style=\"color: #00ffff;\">=<\/span> <span style=\"color: #ffa500;\">new<\/span> <span style=\"color: #b2dfee;\">VBarChart<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">\"<\/span>);  <span style=\"color: #ffffcc;\">\r\n  12:<\/span>     <\/span><\/pre>\n<\/div>\n<p>Nothing fancy up to now. The only noticeable thing is the instantiation of a VBarChart item. The interesting part comes now, in the startApp method:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"> <span style=\"color: #ffffcc;\">\r\n  13:<\/span>    <span style=\"color: #ffa500;\">public<\/span> <span style=\"color: #cc6600;\">void<\/span> <span style=\"color: #b2dfee;\">startApp<\/span>() <span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n  14:<\/span>        display <span style=\"color: #00ffff;\">=<\/span> Display.<span style=\"color: #b2dfee;\">getDisplay<\/span>(<span style=\"color: #ffff66;\">this<\/span>); <span style=\"color: #00cd00;\">\r\n  15:<\/span>         <span style=\"color: #ffffcc;\">\r\n  16:<\/span>        mainForm <span style=\"color: #00ffff;\">=<\/span> <span style=\"color: #ffa500;\">new<\/span> <span style=\"color: #b2dfee;\">Form<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">VBarChart<\/span> <span style=\"color: #00cd00;\">Demo<\/span><span style=\"color: #00cd00;\">\"<\/span>); <span style=\"color: #ffffcc;\">\r\n  17:<\/span>        item.<span style=\"color: #b2dfee;\">setFont<\/span>(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL); <span style=\"color: #ffffcc;\">\r\n  18:<\/span>        item.<span style=\"color: #b2dfee;\">setDrawAxis<\/span>(<span style=\"color: #ffff66;\">true<\/span>); <span style=\"color: #ffffcc;\">\r\n  19:<\/span>        item.<span style=\"color: #b2dfee;\">setPreferredSize<\/span>(mainForm.<span style=\"color: #b2dfee;\">getWidth<\/span>(),<span style=\"color: #cdcd00;\">120<\/span>); <span style=\"color: #00cd00;\">\r\n  20:<\/span>        item.<span style=\"color: #b2dfee;\">setMargins<\/span>(<span style=\"color: #cdcd00;\">5<\/span>,<span style=\"color: #cdcd00;\">45<\/span>,<span style=\"color: #cdcd00;\">10<\/span>,<span style=\"color: #cdcd00;\">15<\/span>); <span style=\"color: #ffffcc;\">\r\n  21:<\/span>        item.<span style=\"color: #b2dfee;\">showShadow<\/span>(<span style=\"color: #ffff66;\">true<\/span>); <span style=\"color: #ffffcc;\">\r\n  22:<\/span>        item.<span style=\"color: #b2dfee;\">setShadowColor<\/span>(<span style=\"color: #cdcd00;\">20<\/span>,<span style=\"color: #cdcd00;\">20<\/span>,<span style=\"color: #cdcd00;\">20<\/span>); <span style=\"color: #ffffcc;\">\r\n  23:<\/span>        item.<span style=\"color: #b2dfee;\">setColor<\/span>(<span style=\"color: #cdcd00;\">40<\/span>, <span style=\"color: #cdcd00;\">40<\/span>, <span style=\"color: #cdcd00;\">200<\/span>); <span style=\"color: #ffffcc;\">\r\n  24:<\/span>        item.<span style=\"color: #b2dfee;\">resetData<\/span>(); <span style=\"color: #00cd00;\">\r\n  25:<\/span>        item.<span style=\"color: #b2dfee;\">addElement<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">a<\/span><span style=\"color: #00cd00;\">\"<\/span>,<span style=\"color: #cdcd00;\">40<\/span>,<span style=\"color: #cdcd00;\">0<\/span>,<span style=\"color: #cdcd00;\">0<\/span>,<span style=\"color: #cdcd00;\">255<\/span>); <span style=\"color: #ffffcc;\">\r\n  26:<\/span>        item.<span style=\"color: #b2dfee;\">addElement<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">b<\/span><span style=\"color: #00cd00;\">\"<\/span>,<span style=\"color: #cdcd00;\">23<\/span>,<span style=\"color: #cdcd00;\">0<\/span>,<span style=\"color: #cdcd00;\">255<\/span>,<span style=\"color: #cdcd00;\">0<\/span>); <span style=\"color: #ffffcc;\">\r\n  27:<\/span>        item.<span style=\"color: #b2dfee;\">addElement<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">c<\/span><span style=\"color: #00cd00;\">\"<\/span>,<span style=\"color: #cdcd00;\">75<\/span>,<span style=\"color: #cdcd00;\">255<\/span>,<span style=\"color: #cdcd00;\">0<\/span>,<span style=\"color: #cdcd00;\">0<\/span>); <span style=\"color: #ffffcc;\">\r\n  28:<\/span>        item.<span style=\"color: #b2dfee;\">setMaxValue<\/span>(<span style=\"color: #cdcd00;\">100<\/span>); <span style=\"color: #ffffcc;\">\r\n  29:<\/span>        mainForm.<span style=\"color: #b2dfee;\">append<\/span>(item); <span style=\"color: #00cd00;\">\r\n  30:<\/span>        mainForm.<span style=\"color: #b2dfee;\">addCommand<\/span>(CMD_EXIT); <span style=\"color: #ffffcc;\">\r\n  31:<\/span>        mainForm.<span style=\"color: #b2dfee;\">setCommandListener<\/span>(<span style=\"color: #ffff66;\">this<\/span>); <span style=\"color: #ffffcc;\">  32:<\/span>        display.<span style=\"color: #b2dfee;\">setCurrent<\/span>(mainForm); <span style=\"color: #ffffcc;\">\r\n  33:<\/span>    <span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #ffffcc;\">\r\n  34:<\/span> <\/span><\/pre>\n<\/div>\n<p>Let&#8217;s focus just on important lines:<\/p>\n<ol>\n<li>Line 18: we want X and Y axis to be shown<\/li>\n<li>Line 21: a shadow makes the chart look 3D<\/li>\n<li>Line 22: the colour of the shadow<\/li>\n<li>Line 24: reset our data just in case we&#8217;re updating the widget<\/li>\n<li>Line 25-26-27: here we actually push data in our chart. The parameters are:\n<ol>\n<li>a label to be shown on the X axis<\/li>\n<li>a value (for the Y axis)<\/li>\n<li>the bar colour in R,G,B notation<\/li>\n<\/ol>\n<\/li>\n<li>Line 28: here we set the maximum Y value<\/li>\n<\/ol>\n<p>The rest of the code is just what we usually find in any midlet.<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\">  <span style=\"color: #00cd00;\">  35:<\/span>    <span style=\"color: #ffa500;\">public<\/span> <span style=\"color: #cc6600;\">void<\/span> <span style=\"color: #b2dfee;\">commandAction<\/span>(Command c, Item e) <span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n  36:<\/span>            <span style=\"color: #ffa500;\">if<\/span>(c.<span style=\"color: #b2dfee;\">getLabel<\/span>().<span style=\"color: #b2dfee;\">equals<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">Exit<\/span><span style=\"color: #00cd00;\">\"<\/span>))<span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n  37:<\/span>                <span style=\"color: #b2dfee;\">destroyApp<\/span>(<span style=\"color: #ffff66;\">false<\/span>); <span style=\"color: #ffffcc;\">\r\n  38:<\/span>                <span style=\"color: #b2dfee;\">notifyDestroyed<\/span>();         <span style=\"color: #ffffcc;\">\r\n  39:<\/span>            <span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #00cd00;\">\r\n  40:<\/span>    <span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #ffffcc;\">\r\n  41:<\/span>    <span style=\"color: #ffa500;\">public<\/span> <span style=\"color: #cc6600;\">void<\/span> <span style=\"color: #b2dfee;\">commandAction<\/span>(Command c, Displayable d)<span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n  42:<\/span>            <span style=\"color: #ffa500;\">if<\/span>(c.<span style=\"color: #b2dfee;\">getLabel<\/span>().<span style=\"color: #b2dfee;\">equals<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">Exit<\/span><span style=\"color: #00cd00;\">\"<\/span>))<span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n  43:<\/span>                <span style=\"color: #b2dfee;\">destroyApp<\/span>(<span style=\"color: #ffff66;\">false<\/span>); <span style=\"color: #ffffcc;\">\r\n  44:<\/span>                <span style=\"color: #b2dfee;\">notifyDestroyed<\/span>();         <span style=\"color: #00cd00;\">\r\n  45:<\/span>            <span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #ffffcc;\">\r\n  46:<\/span>    <span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #ffffcc;\">\r\n  47:<\/span> <span style=\"color: #ffffcc;\">\r\n  48:<\/span>    <span style=\"color: #ffa500;\">protected<\/span> <span style=\"color: #cc6600;\">void<\/span> <span style=\"color: #b2dfee;\">pauseApp<\/span>() <span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n  49:<\/span>    <span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #00cd00;\">\r\n  50:<\/span> <span style=\"color: #ffffcc;\">\r\n  51:<\/span>    <span style=\"color: #ffa500;\">protected<\/span> <span style=\"color: #cc6600;\">void<\/span> <span style=\"color: #b2dfee;\">destroyApp<\/span>(<span style=\"color: #cc6600;\">boolean<\/span> arg0)<span style=\"color: #00ffff;\">{<\/span> <span style=\"color: #ffffcc;\">\r\n  52:<\/span>    <span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #ffffcc;\">\r\n  53:<\/span>     <span style=\"color: #ffffcc;\">\r\n  54:<\/span><span style=\"color: #00ffff;\">}<\/span> <span style=\"color: #00cd00;\">\r\n  55:<\/span> <\/span><\/pre>\n<\/div>\n<p>Should we need a line chart or a horizontal bar chart we just need to change line 11:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"> <span style=\"color: #ffffcc;\">  11:<\/span>    <span style=\"color: #ffa500;\">final<\/span> LineChart item<span style=\"color: #00ffff;\">=<\/span> <span style=\"color: #ffa500;\">new<\/span> <span style=\"color: #b2dfee;\">LineChart<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">\"<\/span>); <\/span><\/pre>\n<\/div>\n<p>or<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\"> <span style=\"color: #ffffcc;\">  11:<\/span>    <span style=\"color: #ffa500;\">final<\/span> HBarChart item<span style=\"color: #00ffff;\">=<\/span> <span style=\"color: #ffa500;\">new<\/span> <span style=\"color: #b2dfee;\">HBarChart<\/span>(<span style=\"color: #00cd00;\">\"<\/span><span style=\"color: #00cd00;\">\"<\/span>);  <\/span><\/pre>\n<\/div>\n<p>Keep in mind that with HBarChart axis are inverted.<br \/>\nI&#8217;ll leave the explaination of PieChart to the next article, as it slightly differs from the other type of charts.<\/p>\n<p><strong>Using the library as a drawing tool<\/strong><br \/>\nThe simple usage presented covers most needs a j2me developer should have when using charts, but there are times where we don&#8217;t need a widget, and the chart needs being exported or manipulated. This is possible with ChartComponent simply using <em>drawChart<\/em> method ( drawPie for PieChart). <em>drawChart<\/em> accepts as parameters a Graphics object and the width and height of the chart.<br \/>\nSo, if you need to draw a chart inside an Image, you can still use the previous code, just substitute line 29 with something like this:<\/p>\n<div>\n<pre><span style=\"color: #f5deb3;\">Image image<span style=\"color: #00ffff;\">=<\/span> Image.<span style=\"color: #b2dfee;\">createImage<\/span>(<span style=\"color: #cdcd00;\">150<\/span>,<span style=\"color: #cdcd00;\">150<\/span>);\r\n Graphics g<span style=\"color: #00ffff;\">=<\/span> image.<span style=\"color: #b2dfee;\">getGraphics<\/span>();\r\n item.<span style=\"color: #b2dfee;\">drawChart<\/span>(g,<span style=\"color: #cdcd00;\">120<\/span>, <span style=\"color: #cdcd00;\">120<\/span>);\r\n mainForm.<span style=\"color: #b2dfee;\">append<\/span>(image);\r\n <\/span><\/pre>\n<\/div>\n<p>As you can see, we create an image, draw the chart inside of its Graphics object, and add the image to the form, instead of a ChartItem.<\/p>\n<p><strong>Conclusions<\/strong><br \/>\nThere&#8217;s enough to get started with the library. Next article will focus on PieChart and a bunch of other useful features of ChartComponent.<br \/>\nChartComponent is and will remain free for both personal or commercial usage. If you find it and this article useful, please blog about it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction J2me Chart Component is a free MIDP2.0 library, aimed at helping J2ME developers to easily add charts to their personal or commercial midlets. I&#8217;m writing this article because some people have asked for a simple kickstarting tutorial on how to use the library. I hope this will be useful and that will also help ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"http:\/\/symbioticindia.in\/docu\/2016\/05\/06\/get-started-with-j2me-chartcomponent\/\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":1,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":41,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/40\/revisions\/41"}],"wp:attachment":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}