JavaFX Drawing Histogram

Source: Internet
Author: User

Package histogram;
/**
* Copyright (c), Oracle and/or its affiliates.
* All rights reserved. Use are subject to license terms.
*/
Import Java.util.Vector;

Import javafx.application.Application;
Import Javafx.scene.Group;
Import Javafx.scene.Scene;
Import Javafx.stage.Stage;
Import javafx.collections.FXCollections;
Import javafx.collections.ObservableList;
Import Javafx.scene.chart.BarChart;
Import Javafx.scene.chart.CategoryAxis;
Import Javafx.scene.chart.NumberAxis;

/**
* A chart that displays rectangular bars with heights indicating data values
* for categories. Used for displaying information in least one axis has
* Discontinuous or discrete data.
*
* @see Javafx.scene.chart.BarChart
* @see Javafx.scene.chart.Chart
* @see Javafx.scene.chart.Axis
* @see Javafx.scene.chart.CategoryAxis
* @see Javafx.scene.chart.NumberAxis
*
*/
public class V_optimal extends application {
private static int[] dataset={1,3,4,7,2,8,3,6,3,6,8,2,1,6,3,5,3,4,7,2,6,7,2};
private static int bucketnum=3;

Define variables, save the corresponding segment values
private static int a_sec;
private static int b_sec;
private static int c_sec;
private static int minValue;
private static int maxValue;
Define variables to save the frequency of each segment
private static int a_num=0;
private static int b_num=0;
private static int c_num=0;

private void init (Stage primarystage) {
Group root = new Group ();
Primarystage.setscene (The new Scene (root));
X-Axis
String aname= "[" + MinValue + "," + (MinValue + a_sec) + "]";
String bname= "[" + (MinValue + a_sec + 1) + "," + (MinValue + a_sec + b_sec) + "]";
String cname= "[" + (MinValue + a_sec + b_sec + 1) + "," + MaxValue + "]";
System.out.println (Aname);
string[] section = {Aname,bname,cname};
Categoryaxis Xaxis = new Categoryaxis ();
Xaxis.setcategories (fxcollections.<string>observablearraylist (section));
Y-Axis
Numberaxis YAxis = new Numberaxis ("Frequency", 0, 10, 1);
@SuppressWarnings ({"Unchecked", "Rawtypes"})
observablelist<barchart.series> Barchartdata = Fxcollections.observablearraylist (
New Barchart.series ("V_optimal histogram", Fxcollections.observablearraylist (
New Barchart.data (Section[0], a_num),
New Barchart.data (section[1], b_num),
New Barchart.data (section[2], c_num)
))
);
@SuppressWarnings ({"Rawtypes", "Unchecked"})
Barchart chart = new Barchart (Xaxis, YAxis, barchartdata,0);
Chart.settitle ("V-optimized histogram");
Root.getchildren (). Add (chart);

}

@Override public void Start (Stage primarystage) throws Exception {
Init (primarystage);
Primarystage.show ();
}
public static void Main (string[] args) {
Sort data from small to large
DataSet = Bublesort (DataSet);
Print_array (Bublesort (DataSet));

Finding data interval Segments
MinValue = min (DataSet);
MaxValue = max (DataSet);
int section = Maxvalue-minvalue;

Dividing the interval range of each bucket
Vector<integer> alist = new vector<integer> ();
vector<integer> blist = new vector<integer> ();
vector<integer> cList = new vector<integer> ();
vector<double> variancevaluelist = new vector<double> ();

If (Bucketnum > section + 1)
System.out.println ("Excessive number of buckets! ");
Else if (Bucketnum < 1)
System.out.println ("The number of buckets must be a positive integer!") ");
Else {
for (int a = 0; a <= section-2; a++) {
for (int b = 1; b <= section-1-A; b++) {
vector< integer> first = new vector<integer> ();
vector<integer> Second = new vector<integer> ();
Vector<integer> third = new vector<integer> ();
//Get 3 interval sets per loop
for (int i = 0; i < dataset.length; i++) {
if (Dataset[i] >= minValue
&& datase T[i] <= MinValue + a)
First.add (Dataset[i]);
else if (Dataset[i] > MinValue + a
&& dataset[i] <= MinValue + A + b)
Second.add (Dataset[i]);
else
Third.add (dataset[i]);
}
//Convert to 3 fixed-length arrays
int firstsection[] = Transfervectortoint (first);
int secondsection[] = Transfervectortoint (Second);
int thirdsection[] = Transfervectortoint (third);

The total weighted variance of the group is obtained at this time
Double valuevariance = cal_variance (firstsection)
+ cal_variance (secondsection)
+ cal_variance (thirdsection);

Save the total weighted variance at this time and each grouping a,b,c
Variancevaluelist.add (valuevariance);
Alist.add (a);
Blist.add (b);
Clist.add (Section-a-B);
}
}
}
System.out.println ("Number of cycles:" + variancevaluelist.size ());
Find the minimum value of the total weighted variance, and the a,b,c of that group
int loc = Minlocation (variancevaluelist);
A_sec = Alist.get (loc);
B_sec = Blist.get (loc);
C_sec = Clist.get (loc);

Find out the frequency of each grouping
for (int ii=0;ii<dataset.length;ii++) {
if (dataset[ii]>=minvalue&&dataset[ii]<=minvalue+a_sec)
a_num++;
else if (Dataset[ii]>minvalue+a_sec&&dataset[ii]<=minvalue + a_sec + b_sec)
b_num++;
Else
c_num++;
}

Result output:
Print_array (Transfervectortodouble (variancevaluelist));
SYSTEM.OUT.PRINTLN ("Minimum weighted variance is:" + variancevaluelist.get (Loc));
SYSTEM.OUT.PRINTLN ("Best scheme for the first" + loc + "secondary cycle!) ");
System.out.println ("a_sec=" + a_sec + "b_sec=" + b_sec + "c_sec="
+ c_sec);
System.out.println ("V optimized Histogram method Best Practices:" + "[" + MinValue + ","
+ (MinValue + a_sec) + "]" + "[" + (MinValue + a_sec + 1)
+ "," + (MinValue + a_sec + b_sec) + "]" + "["
+ (MinValue + a_sec + b_sec + 1) + "," + MaxValue + "]");

Go to Drawing
Launch (args);

}

/**
* Find the location of the minimum value in a set of values */
private static int minlocation (vector<double> list) {
int loc=0;
Double Min=list.get (LOC);
for (int i=1;i<list.size (); i++) {
if (List.get (i) <list.get (Loc))
Loc=i;
}
Return LOC;
}

/**
* Convert vector arrays to normally specified long arrays */
private static int[] Transfervectortoint (vector<integer> list) {
Int[] Array=new int[list.size ()];
for (int i=0;i<list.size (); i++) {
array[i]= (int) list.elementat (i);
}
return array;
}

private static double[] transfervectortodouble (vector<double> list) {
Double[] Array=new double[list.size ()];
for (int i=0;i<list.size (); i++) {
array[i]= (double) list.elementat (i);
}
return array;
}

/**
* Calculate the weighted variance of a set of data */
@SuppressWarnings ("unused")
private static Double cal_variance (double array[]) {
Double sum=0;
for (int i=0;i<array.length;i++) {
Sum+=array[i];
}
Double avg=sum/array.length;
Double NSS=0;//SS represents variance, NSS represents weighted variance
for (int j=0;j<array.length;j++) {
nss+= (Avg-array[j]) * (Avg-array[j]);
}
return NSS;
}

private static double cal_variance (int array[]) {
Double sum=0;
for (int i=0;i<array.length;i++) {
Sum+=array[i];
}
Double avg=sum/array.length;
Double NSS=0;//SS represents variance, NSS represents weighted variance
for (int j=0;j<array.length;j++) {
nss+= (Avg-array[j]) * (Avg-array[j]);
}
return NSS;
}


/**
* Bubble sort, small to large * *
@SuppressWarnings ("unused")
private static double[] Bublesort (double array[]) {
int n=array.length;
Double temp;
for (int i=0;i<n;i++) {
for (int j=i+1;j<n;j++) {
if (Array[i]>array[j]) {
Temp=array[i];
ARRAY[I]=ARRAY[J];
Array[j]=temp;
}
}
}
return array;
}

private static int[] Bublesort (int array[]) {
int n=array.length;
int temp;
for (int i=0;i<n;i++) {
for (int j=i+1;j<n;j++) {
if (Array[i]>array[j]) {
Temp=array[i];
ARRAY[I]=ARRAY[J];
Array[j]=temp;
}
}
}
return array;
}

/** Find Minimum value */
private static int min (int[] data) {
int size=data.length;
int minvalue=data[0];
for (int i=1;i<size;i++) {
if (Minvalue>data[i])
Minvalue=data[i];
}
return minValue;

}

/** Maximum Value */
private static int Max (int[] data) {
int size=data.length;
int maxvalue=data[0];
for (int i=1;i<size;i++) {
if (Maxvalue<data[i])
Maxvalue=data[i];
}
return maxValue;

}

/** Output Array */
private static void Print_array (double arr[]) {
for (int i=0;i<arr.length;i++)
System.out.print (arr[i]+ "");
System.out.println ();
}

private static void Print_array (int arr[]) {
for (int i=0;i<arr.length;i++)
System.out.print (arr[i]+ "");
System.out.println ();
}

}

JavaFX Drawing 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.