Java plot-temperature line chart

Source: Internet
Author: User
To implement the temperature line chart, we originally intended to implement it using jfreechart. After a few days of hard research, we found that jfreechart does not seem to be able to implement this complex function, because we want to implement two column headers, duplicate columnkey exists. When the difference between two points is greater than 1.5, mark it with "√... Since we didn't find a proper method for implementation, we had to write the graphics class for manual painting by ourselves. The function basically met the requirements, that is, the font, line, and appearance were still a little bad, do you have any good suggestions? Do you want to raise O (∩ _ ∩) O ~

Implementation Process

ØUse bufferedimage to create the graphics2d object, and then generate 6*7 columns and 7*5 rows (ROW)

 /**  * Column line  */          //  Time          Int [] Times = New   Int [] {3, 7, 11};  Int Looptimes = 0 ;  For ( Int I = 0; I <= cell_count; I ++, looptimes ++ ){  If (I! = Cell_count ){  //  Column header display time                  If (Looptimes = 3 ) Looptimes = 0; Graphics. setcolor (color. Black); graphics. drawstring (Times [looptimes] + "",( Int ) (Margin_left + cellwidth * I + 2.5), cellheight + 13 );}  //  Separated by delimiters every other day              If (0 = I % 6 ) {Graphics. setstroke (separator); graphics. setcolor (  New Color (162,115, 73 ); Graphics. drawline (margin_left + Cellwidth * I, 0, margin_left + cellwidth *I, height );}  Else  {Graphics. setstroke (basicstroke); graphics. setcolor (  New Color (221,221,221 ); Graphics. drawline (margin_left + Cellwidth * I, cellheight, margin_left + cellwidth * I, height );}}  /*  * Row line  */          Int Temp = Temperature; graphics. setstroke (basicstroke ); For ( Int I = 1; I <= 40; I ++ ){  //  Use separators at intervals of 1 °              If (0 = I % 5 ){  //  Temperature Scale  Graphics. setcolor (color. Black); graphics. drawstring ( -- Temp + "", margin_left-cellwidth, cellheight * I + 5 );  If (37 =Temp) graphics. setcolor (color. Red );  Else  Graphics. setcolor (  New Color (180,180,180 )); //  Split line } Else  Graphics. setcolor (  New Color (201,221,221 ));  //  The line in the second row is the title, equal to the table width              If (2 =I) graphics. Draw (  New Line2d. Float (0, cellheight * I, margin_left + cellwidth * cell_count, cellheight * I ));  Else  Graphics. Draw (  New Line2d. Float (margin_left, cellheight * I, margin_left + cellwidth * cell_count, cellheight * I ));} 

 

ØCreate a data line

Set <entry <string, float []> set = Map. entryset (); iterator <Entry <string, float []> iterator = Set. iterator ();  Int Index = 0 ; Graphics. setcolor (color. Black );  Float Cellhalfwidth = cellwidth/ 2f;  //  Position of the starting line of temperature Float starty = Null , Startx = Null  ;  Float Pretemp = 0; // Question data recorded last time          While  (Iterator. hasnext () {entry <String, float []> entry = Iterator. Next ();  //  Filling time Graphics. setfont ( New Font ("", Font. Plain, 13 ); Graphics. drawstring (entry. getkey (),(  Int ) (Margin_left + cellwidth * (index + 1.1), 12 );  //  Line Float [] values = Entry. getvalue ();  For ( Int I = 0; I <values. length; I ++ ) {Float currtemp = Values [I]; float endx = Margin_left + cellwidth * index + Cellhalfwidth;  If ( Null ! = Currtemp ){  //  Downtemperature value to coordinate point Float Endy = (temperature-currtemp) * (cellheight * 5 ); Graphics. setcolor (color. Black); graphics. filloval ((  Int ) (Endx-2 ),( Int )( Float ) Endy-2, 5, 5 );  If ( Null ! = Starty) {line2d. Float Line = New Line2d. Float (startx, starty, endx, Endy); graphics. Draw (line );  //  When the difference between the two measured values is more than 1.5, mark it with "√"                          If (Math. Abs (pretemp-currtemp)> = 1.5 ) {Graphics. setcolor (color. Red); graphics. setfont (  New Font ("", Font. Plain, 30 ); Graphics. drawstring ( "√", Endx-15, Endy-15 ) ;}} Starty =Endy; startx = Endx; pretemp = Currtemp;} Index ++ ;}} 

 

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.