Use JFreeChart to draw a scatter chart

Source: Internet
Author: User

Because the project needs to start Java and JFreeChart class libraries... It really hurts. I read this class library topic of 51cto: Workshop.

It's quite good. It's quite clear. It's just a bit coincident with the official demo. It's all pieChart. Do you dare to change another chart?

Below I will list the three necessary modules for drawing, and the other parts will not be provided one by one. If you need them, please take a look:

 
 
  1. // Load dataset. Ignore the specific business logic
  2. Public static XYDataset createxydataset (ArrayList <LineInfo> xydatalist, ArrayListMultimap <Integer, Integer> tabBat ){
  3. DefaultXYDataset dataset = new DefaultXYDataset ();
  4. LineInfo tabTmp = xydatalist. get (0 );
  5. Int numTmp = tabTmp. getMeterNum ();
  6. Set <Integer> allKey = tabBat. keySet ();
  7. Iterator <Integer> it = allKey. iterator ();
  8. Int keyTmp = 0;
  9. While (it. hasNext ())
  10. {
  11. KeyTmp = it. next ();
  12. If (tabBat. containsEntry (keyTmp, numTmp ))
  13. Break;
  14. }
  15. List <Integer> allNum = tabBat. get (keyTmp );
  16. Int allNumSize = allNum. size ();
  17. // Data is the core of data
  18. Double [] [] data = new double [2] [allNumSize];
  19. It = allNum. iterator ();
  20. Iterator <LineInfo> it2 = xydatalist. iterator ();
  21. Int tmp = 0, count = 0;
  22. LineInfo lineTmp = new LineInfo ();
  23. While (it. hasNext ())
  24. {
  25. Tmp = it. next ();
  26. It2 = xydatalist. iterator ();
  27. While (it2.hasNext ())
  28. {
  29. LineTmp = it2.next ();
  30. If (tmp = lineTmp. getMeterNum ())
  31. Break;
  32. }
  33. Data [0] [count] = lineTmp. getIntercept ();
  34. Data [1] [count] = lineTmp. getSlope ();
  35. Count ++;
  36. }
  37. // I still don't know the first parameter in addSeries. From the generated chart, it's just the description text in the chart.
  38. Dataset. addSeries ("table", data );
  39. Return dataset;
  40. }
  41.  
  42. // Generate a chart object
  43. Public static JFreeChart createChart (XYDataset xydataset, String title, String xAxis, String yAxis ){
  44. JFreeChart scatterChart = ChartFactory. createScatterPlot (title, xAxis, yAxis, xydataset, PlotOrientation. VERTICAL, true, false, false );
  45. Return scatterChart;
  46. }
  47.  
  48. // According to the chart object, set the Image Parameters and draw a picture. This is very important. If you do not pay attention to it, the text of the comment will be garbled.
  49. Public static void drawScatterChart (JFreeChart scatterChart, String title, String noDataMsg ){
  50. // Set the font for title, legend, and plot as follows:
  51. TextTitle textTitle = scatterChart. getTitle ();
  52. TextTitle. setFont (new Font ("", Font. BOLD, 20 ));
  53. LegendTitle legend = scatterChart. getLegend ();
  54. If (legend! = Null)
  55. {
  56. Legend. setItemFont (new Font ("", Font. BOLD, 20 ));
  57. }
  58. XYPlot scatterPlot = scatterChart. getXYPlot ();
  59. ScatterPlot. setNoDataMessage (noDataMsg );
  60. // Draw the following three rows:
  61. ChartFrame scatterFrame = new ChartFrame (title, scatterChart );
  62. ScatterFrame. pack ();
  63. ScatterFrame. setVisible (true );
  64. }

In general, it is quite simple to draw other diagrams to check the API.

This article is from the title cannot be blank blog, please be sure to keep this source http://hector.blog.51cto.com/4229131/926904

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.