{"id":1800,"date":"2017-04-17T13:07:08","date_gmt":"2017-04-17T13:07:08","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1800"},"modified":"2021-01-21T14:11:26","modified_gmt":"2021-01-21T14:11:26","slug":"a-poll-chart-a-stacked-bar-chart-in-java-swing-that-represents-results-of-a-survey-ii","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/a-poll-chart-a-stacked-bar-chart-in-java-swing-that-represents-results-of-a-survey-ii\/","title":{"rendered":"A Poll Chart: A Stacked Bar Chart in Java Swing that Represents Results of a Survey &#8211; II"},"content":{"rendered":"<p><a href=\"http:\/\/mindfusion.dev\/blog\/a-poll-chart-a-stacked-bar-chart-in-java-swing-that-represents-results-of-a-survey\/\">Here is a link<\/a> to Part I: Overview of chart elements, the dashboard, plot and axes.<\/p>\n<p><strong>I. Bars Rendering<\/strong><\/p>\n<p>The bar graphics is rendered by a <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_BarStackRenderer.htm\">BarStackRenderer.<\/a> It requires a collection of Series and we add four:<\/p>\n<pre>private ObservableList createSeries()\n   {\n      \/\/ Important series\n\tBarSeries series1 = new BarSeries(\n\tArrays.asList(40.0, 30.0, 52.0, 62.0),\n\tArrays.asList(\"20%\", \"15%\", \"26%\", \"31%\"),\n\tnull \/* no top labels *\/);\n        \n        ............\n\n  }\n<\/pre>\n<p>Each <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_BarSeries.htm\">BarSeries<\/a> has a list with the data, a list with the inner labels and no top labels. The <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_BarStackRenderer.htm\">BarStackRenderer<\/a> renders in a single bar the values from the same index in each BarSeries e.g. the first bar renders the first double value in all BarSeries, the second bar renders the values on the second position in the series and so on. The <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_BarRenderer.htm\">BarRenderer<\/a> itself has a few customizations:<\/p>\n<pre>barRenderer = new BarStackRenderer (createSeries());\nbarRenderer.setHorizontalBars(true);\nbarRenderer.setBarSpacingRatio(0.3);\nbarRenderer.setYAxis(yAxis);\nbarRenderer.setXAxis(xAxis);\nbarRenderer.setShowDataLabels(EnumSet.of(LabelKinds.InnerLabel));\nbarRenderer.setLabelFontSize(12.0);\n<\/pre>\n<p>Let&#8217;s not forget to add it to the Plot2D:<\/p>\n<pre>plot.getSeriesRenderers().add(barRenderer);\n<\/pre>\n<p><strong>II. The Annotations<\/strong><\/p>\n<p>The labels at the X and Y axis are actually annotations &#8211; they are created by <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_AnnotationRenderer.htm\">AnnotationRenderer<\/a> instances. An AnnotationRenderer needs a collection of <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_Series.htm\">Series.<\/a> For the Y-axis we use a SimpleSeries instance, which is created and used for the annotations and does not provide data for the bars:<\/p>\n<pre>private ObservableList createAxisLabels()\n{\n   return FXCollections.observableList(Arrays.asList(\n\tnew SimpleSeries(null, null)\n\t{\n        \tpublic String getLabel(int index, LabelKinds kind)\n\t\t{\n\t\t\treturn axisLabels[index];\n\t\t}\n\t\n\t\tpublic double getValue(int index, int dimension) { return index; }\n\t\t\t\n\t\tpublic int getSize() { return axisLabels.length; }\n\n\t\tpublic int getDimensions() { return 1; }\n\n\t\tpublic EnumSet getSupportedLabels() {\n\t        \treturn EnumSet.of(LabelKinds.YAxisLabel);\n\t\t}\n\t\t\t\t\n\t\tfinal String[] axisLabels = {\n\t\t\t\"Accomodation\", \"University\\nLocation\", \"Tuition\\nPrice\", \"Quality of\\nEducation\" };\n\t\t}\n\t));\n}\n<\/pre>\n<p>Here we indicate that this SimpleSeries provides labels for the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_YAxisRenderer.htm\">YAxis<\/a> with the getSupportedLabels override, which in our case returns <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_LabelKinds.htm\">LabelKinds<\/a>.YAxisLabel. The label is returned by the getLabel method.<\/p>\n<p>The annotations on the X-axis are rendered by a special class called CustomBarSeries. We implement the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/chart.java\/index.htm?T_com_mindfusion_charting_Series.htm\">Series<\/a> interface to return a set of predefined labels for each data value in this series:<\/p>\n<pre>public CustomBarSeries(List values, List innerLabels, List topLabels) {\n\tsuper(values, innerLabels, topLabels);\n\t\t\n\tthis._values = values;\n\tdouble sum = 0;\n\t_stackedValues = new ArrayList(values.size());\n\t\t\n\tfor(double val : values)\n\t{\n\t   sum += val;\n\t  _stackedValues.add(sum);\n\t}\n\tthis._innerLabels = innerLabels;\n\t\n}\n<\/pre>\n<p>We also take into consideration that the values rendered by the bar chart where this class is used are stacked &#8211; so we sum them and return the summed value when asked:<\/p>\n<pre>public double getValue(int index, int dimension) { \n\t\t\n\tif( dimension == 0)\n\t  return _stackedValues.get(index);\n\t\n        return _values.get(index);\t\t\t\n}\n<\/pre>\n<p>Then we return the predefined label at the given position and :<\/p>\n<pre>public String getLabel(int index, LabelKinds kind)\n  {\n\tif(kind.equals(LabelKinds.InnerLabel))\n\t{\n\t\treturn _innerLabels.get(index);\n\t}\n\n\t\/\/else return the labels\n\treturn axisLabels[index];\n}\n\t\n       final String[] axisLabels = {\n\t\t\"Very Important\", \n\t\t\"Somewhat\\nImportant\", \"Slightly\\nImportant\", \n\t\t\"Not\\nImportant\" };\n<\/pre>\n<p>We create an instance of this CustomBarSeries and we assign it to the new AnnotationRenderer, that is responsible for the labels at the X-axis:<\/p>\n<pre>List sl = new ArrayList();\n\tsl.add(new CustomBarSeries(\n\t        Arrays.asList(25.0, 50.0, 50.0, 50.0),\n\t\tnull,\n\t\tnull));\n\tjavafx.collections.ObservableList olss = FXCollections.observableList(sl);\n\tannotationRenderer1 = new AnnotationRenderer(olss);\n\tannotationRenderer1.setSeries(olss);\n\n<\/pre>\n<p>Let&#8217;s not forget to bind the AnnotationRenderer to the X-axis:<\/p>\n<pre>annotationRenderer1.setXAxis(xAxis);\nannotationRenderer1.setShowDataLabels(EnumSet.of(LabelKinds.XAxisLabel));\n<\/pre>\n<p>Here is the final chart:<\/p>\n<div id=\"attachment_1784\" style=\"width: 812px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/03\/java-stacked-bar-chart.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1784\" class=\"size-full wp-image-1784\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/03\/java-stacked-bar-chart.png\" alt=\"A stacked bar chart in Java Swing\" width=\"802\" height=\"400\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/03\/java-stacked-bar-chart.png 802w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/03\/java-stacked-bar-chart-300x150.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/03\/java-stacked-bar-chart-768x383.png 768w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/03\/java-stacked-bar-chart-500x249.png 500w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><p id=\"caption-attachment-1784\" class=\"wp-caption-text\">Poll chart in Java Swing<\/p><\/div>\n<p>That&#8217;s the end of this tutorial. Here is the link to download the full sample.<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/java\/chart\/StackedBarChart.zip\">Download The Stacked Bar Chart in Java Sample<\/a><\/p>\n<p><em>About Charting for Java:<\/em> MindFusion.Charting for Java Swing is a multipurpose graphics library that lets you create and customize a large variety of chart types: bar, column, pie, doughnut, radar, polar etc., candlestick financial charts, gauges and dashboards with dynamic layout of their components. The library boasts a smart API which lets you combine and arrange multiple lots, axes, legends, images and other chart components. The chart appearance can be customized on multiple levels &#8211; from properties applied on a single element to global themes reused by all charts and series. Charts use a uniform Series interface for reading data and labels. You can implement the interface and create custom Series that matches your data source. Written in pure Java, this tool provides every type of Java Swing application with powerful charting capabilities. Read more about the component from <a href=\"http:\/\/mindfusion.dev\/java-chart.html\">here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a link to Part I: Overview of chart elements, the dashboard, plot and axes. I. Bars Rendering The bar graphics is rendered by a BarStackRenderer. It requires a collection of Series and we add four: private ObservableList createSeries() &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/a-poll-chart-a-stacked-bar-chart-in-java-swing-that-represents-results-of-a-survey-ii\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[464,463,457,454,465],"class_list":["post-1800","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-java-cbar-chart","tag-java-chart","tag-poll-chart","tag-stacked-bar-chart","tag-survey-chart-in-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-t2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1800"}],"version-history":[{"count":8,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":2618,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1800\/revisions\/2618"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}