Use graphics to export the picture code of a histogram

Source: Internet
Author: User
Tags set background

Package com.bstd.sys.server;

Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.FontMetrics;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.Collections;

Import Javax.imageio.ImageIO;
public class EMP {
Private final int histogramwidth = width of 60;//column chart
Private final int histogrampitch = 60;//Column Chart spacing
Private float scaling = scale of 1f;//scaling
private int maxstrwidth = 0; The maximum width required for a string
private static Font mfont = new Font ("Microsoft Jas Blackbody", Font.Bold, 24);
/**
* <pre>
* Parameters B[i] and str[i] must correspond to
* </pre>
*
* @param g
* @param title
* @param v
* @param str
* @param color
* Can be empty
*/
Public BufferedImage Paintplanehistogram (String title, int[] V, string[] str, color[] Color) {
int width = str.length * HISTOGRAMWIDTH+STR.LENGTH*HISTOGRAMPITCH+50;
int width=500;
int height = 500;
Scaling = Calculatescale (v, height);//Calculating the scaling ratio

BufferedImage bufferimage = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = bufferimage.getgraphics ();
G.setcolor (Color.White);
G.setcolor (new color (240,255,240));//Set Background color
G.fillrect (0, 0, width, height);
G.fillrect (1, 1, width-1, height-1);
FontMetrics metrics = null;
G.setfont (Mfont);
G.setcolor (New Color (100,149,237)); Set the title color
g.DrawString (title, (Bufferimage.getwidth ()-G.getfontmetrics ()
. Stringwidth (title)) >> 1, 30);//Draw title
G.setfont (Mfont);

Metrics = G.getfontmetrics ();

G.setcolor (New Color (100,149,237));

G.drawline (0, ten, height-30); Draw Y Coordinate
G.drawline (height-30, Width, height-30);//Draw x-coordinate

int j = 0;
int colorcount=color.length;

for (int i = 0; i < v.length; ++i) {

if (color! = NULL) {
G.setcolor (Color[j]);//Set foreground color
if (J+1<colorcount) {
j + +;
}else{
j=0;
}
}else{
G.setcolor (color.red);
}

int x = + I
* (Histogrampitch + histogramwidth + (maxstrwidth >> 1));//calculate x-coordinate
int y = height-30-(int) (V[i] * scaling); Calculates the y-coordinate

Proportion of paintings
g.DrawString (V[i] + "mouth", X
-((Metrics.stringwidth (V[i] + "")-Histogramwidth) >> 10),
Y-10); This is the gap between the value and the graph.

Histogram of drawing plane
G.drawrect (x, Y, histogramwidth, (int) (V[i] * scaling));
G.fillrect (x, Y, histogramwidth, (int) (V[i] * scaling));

Draw everything that is represented
g.DrawString (Str[i], X
-((Metrics.stringwidth (Str[i])-histogramwidth) >> 1),
HEIGHT-2);
}

return bufferimage;
}

/**
* Calculate zoom ratio
* @param v
* Height of @param h picture
* @return
*/
Public float Calculatescale (int[] v, int h) {
float scale = 1f;
int max = Integer.min_value;
for (int i=0, len=v.length; i < Len; ++i) {
if (v[i]>h && V[i]>max) {
Max=v[i];
}
}
if (Max > H) {
scale= ((int) (h*1.0f/max*1000)) *1.0f/1000;
}
return scale;
}

public static void Main (string[] args) {
EMP Planehistogram = new EMP ();
Color[] Colorarr=new color[] {new color (132,112,255), new color (100,149,237), new color (255,222,173), New color ( 127,255,0)};
BufferedImage image = Planehistogram.paintplanehistogram ("Color histogram",
New int[]{100,200,300,400}, new string[]{"Evaluation Well", "pre-exploratory exploration", "Production Well", "other"}, Colorarr);
Paintplanehistogram (String title, int[] V, string[] str, color[] Color)
int width = str.length * HISTOGRAMWIDTH+STR.LENGTH*HISTOGRAMPITCH+50;
File output = new file ("E:\\3331.png");
try {
Imageio.write (Image, "png", output);
} catch (IOException e) {
E.printstacktrace ();
}
}

}

Use graphics to export the picture code of a histogram

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.