Use jfreechart to output the dashboard graph to the local device.

Source: Internet
Author: User

The project also needs to use jfreechart to generate a dashboard, and quickly found the code on the internet, used it to modify it, and finally output the image to the local device. the project needs are changing, and everything is possible.

Import java. AWT. basicstroke;
Import java. AWT. color;
Import java. AWT. dimension;
Import java. AWT. Font;
Import java. Io. file;

Import javax. Swing. jpanel;

Import org. jfree. Chart. chartpanel;
Import org. jfree. Chart. chartutilities;
Import org. jfree. Chart. jfreechart;
Import org. jfree. Chart. Plot. dialshape;
Import org. jfree. Chart. Plot. meterinterval;
Import org. jfree. Chart. Plot. meterplot;
Import org. jfree. Data. range;
Import org. jfree. Data. General. defaultvaluedataset;
Import org. jfree. Data. General. valuedataset;
Import org. jfree. UI. applicationframe;
Import org. jfree. UI. refineryutilities;

Public class meterchartdemo1 extends applicationframe ...{

Public meterchartdemo1 (string s )...{
Super (s );
Jpanel = createdemopanel ();
Jpanel. setpreferredsize (new dimension (500,270 ));
Setcontentpane (jpanel );
}

// Create a gauge chart
Private Static jfreechart createchart (valuedataset )...{
Meterplot = new meterplot (valuedataset );
Meterplot. setrange (new range (0.0d, 60d ));
Meterplot. addinterval (New meterinterval ("normal ",
New range (0.0d, 35d), color. lightgray, new basicstroke (2.0f ),
New Color (0,255, 0, 64 )));
Meterplot. addinterval (New meterinterval ("warning ",
New range (35d, 50d), color. lightgray, new basicstroke (2.0f ),
New Color (255,255, 0, 64 )));
Meterplot. addinterval (New meterinterval ("critical", new range (50d,
60d), color. lightgray, new basicstroke (2.0f), new color (255, 0,
0,128 )));
Meterplot. setneedlepaint (color. darkgray );
Meterplot. setdialbackgroundpaint (color. White );
Meterplot. setdialoutlinepaint (color. Gray );
Meterplot. setdialshape (dialshape. chord );
Meterplot. setmeterangle (260 );
Meterplot. setticklabelsvisible (true );
Meterplot. setticklabelfont (new font ("dialog", 1, 10 ));
Meterplot. setticklabelpaint (color. darkgray );
Meterplot. setticksize (5D );
Meterplot. settickpaint (color. lightgray );
Meterplot. setvaluepaint (color. Black );
Meterplot. setvaluefont (new font ("dialog", 1, 14 ));
Jfreechart = new jfreechart ("meter Chart 1 ",
Jfreechart. default_title_font, meterplot, true );
Return jfreechart;
}

// Graphic display for debugging and calling of the Output Method
Public static jpanel createdemopanel ()...{
Dataset = new defaultvaluedataset (23D );
Jfreechart = createchart (Dataset );
Outputjpg (jfreechart );
Chartpanel = new chartpanel (jfreechart );
Return chartpanel;
}

Public static void main (string ARGs [])... {
Meterchartdemo1 meterchartdemo1 = new meterchartdemo1 (
"Meter chart demo 1 ");
Meterchartdemo1.pack ();
Refineryutilities. centerframeonscreen (meterchartdemo1 );
Meterchartdemo1.setvisible (true );

}
// Output the image to the local directory
Public static void outputjpg (jfreechart chart )...{
Try ...{
File fp = new file ("C:/test.jpg ");
Chartutilities. savechartasjpeg (FP, charts, 300,300 );
} Catch (exception e )...{

}

}

Private Static defaultvaluedataset dataset;
}

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.