{"id":1342,"date":"2017-05-03T09:36:27","date_gmt":"2017-05-03T09:36:27","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1342"},"modified":"2024-11-16T14:58:26","modified_gmt":"2024-11-16T14:58:26","slug":"how-to-draw-a-chart-for-test-automation-result","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-draw-a-chart-for-test-automation-result\/","title":{"rendered":"How to draw a chart for Test Automation result?"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>In this post, we would be using the <strong>JFreeChart<\/strong> library to write a sample Java program to generate PieChart for test automation results. We will also discuss JFreeChart API and a sample program to generate PieChart for our Test Automation Result.<\/p>\n<h3><strong>Sample data:<\/strong><\/h3>\n<p>Sample data for our test automation result would be:<\/p>\n<p>Passed test cases :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 50<\/p>\n<p>Skipped test cases :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 20<\/p>\n<p>Failed test cases:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 30<\/p>\n<p>\u2014\u2014<\/p>\n<p>Total Automation Test cases : 100<\/p>\n<h3><strong>JFreeChart API<\/strong><\/h3>\n<p>JFreeChart is a free Java chart library that makes it easy for developers to display charts in their applications. Its features include support for different chart types, a well-documented API. More information can be found on the official website at:<\/p>\n<p><a href=\"http:\/\/www.jfree.org\/jfreechart\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.jfree.org\/jfreechart\/<\/a><\/p>\n<p><strong>JFreeChart<\/strong> supports bar charts, line charts, pie charts. It allows you to draw a chart on a graphics device. It plots different charts using <strong>ChartFactory<\/strong>. <strong>ChartFactory<\/strong> is an abstract class with a collection of methods for creating standard charts with JFreeChart.<\/p>\n<p>You should use a <strong>ChartPanel<\/strong> to display a chart in a GUI. It is a Swing GUI component for displaying a chart object. The panel registers with the chart to receive notification of changes to any component of the chart.<\/p>\n<p><strong>ChartPanel<\/strong> implements many listener interfaces like ChartChangeListener, ChartProgressListener, ActionListener, MouseListener etc.<\/p>\n<p><strong>PiePlot<\/strong> class is a plot that displays data in the form of a pie chart, using data from any class that implements the PieDataset interface.<\/p>\n<h3><strong>Sample Java Program<\/strong><\/h3>\n<p>We will write a program to plot the chart for the automation test result.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class PieChartExample extends ApplicationFrame\r\n{\r\n private static final long serialVersionUID = 1L;\r\n\r\npublic PieChartExample(String title) {\r\n super(title);\r\n \/\/ TODO Auto-generated constructor stub\r\n }\r\n\r\npublic JFreeChart createPieChart(String title, PieDataset dataset)\r\n {\r\n JFreeChart chart = ChartFactory.createPieChart(\r\n\r\ntitle, \r\n dataset, \r\n true, \r\n true, \r\n false);\r\n\r\nPiePlot plot = (PiePlot)chart.getPlot();\r\n plot.setStartAngle(290.0D);\r\n plot.setDirection(Rotation.CLOCKWISE);\r\n plot.setForegroundAlpha(0.5F);\r\n \r\n ChartPanel chartPanel = new ChartPanel(chart);\r\n chartPanel.setPreferredSize(new Dimension(750, 550));\r\n setContentPane(chartPanel);\r\n return chart;\r\n }\r\n\r\nprivate static PieDataset createPieChartDataset(int total, int passed, int skipped, int failed)\r\n {\r\n DefaultPieDataset result = new DefaultPieDataset();\r\n if (total &gt; 0)\r\n {\r\n if (passed &gt; 0) {\r\n result.setValue(\" Passed (\" + passed + \") \", passed);\r\n }\r\n if (skipped &gt; 0) {\r\n result.setValue(\" Skipped (\" + skipped + \") \", skipped);\r\n }\r\n if (failed &gt; 0) {\r\n result.setValue(\" Failed (\" + failed + \") \", failed);\r\n }\r\n }\r\n return result;\r\n }\r\n\r\npublic static void main(String args[])\r\n {\r\n PieChartExample chart = new PieChartExample(\"Test Result PieChart\");\r\n chart.createPieChart(\"Test Result Chart\",createPieChartDataset(100, 50,20,30));\r\n chart.pack();\r\n RefineryUtilities.centerFrameOnScreen(chart);\r\n chart.setVisible(true);\r\n\r\n}\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Program Output<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-615\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/2D_PieChart.jpeg\" alt=\"Chart for Test Automation Result\" width=\"1113\" height=\"727\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>3D Pie Chart:<\/strong><\/h3>\n<p>In-case, you need 3D chart, you can use <strong>PiePlot3D<\/strong> class and <strong>ChartFactory.createPieChart3D<\/strong> method to generate pie chart in 3D as shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-616\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/TestResult_PieChart3D.jpeg\" alt=\"3D PieChart\" width=\"1365\" height=\"724\" title=\"\"><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1342\" data-postid=\"1342\" class=\"themify_builder_content themify_builder_content-1342 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this post, we would be using the JFreeChart library to write a sample Java program to generate PieChart for test automation results. We will also discuss JFreeChart API and a sample program to generate PieChart for our Test Automation Result. Sample data: Sample data for our test automation result would be: Passed test [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1342","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1342"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1342\/revisions"}],"predecessor-version":[{"id":19614,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1342\/revisions\/19614"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}