Reputation: 3
I have 6 numbers, all from 0-100. I need to put the inside a chart, I already have the codes for the chart but don't know how to link it to my array. the chart uses bars. This is my code below, its the average of two other arrays that I have:
public static void computeResults(double[] examMarks, double[] courseworkmarks)
{
double avgMarks[] =new double[examMarks.length];
System.out.println ("The total average of each module is ");
for(int i=0;i<avgMarks.length;i++){
int cwWeighting=40;
avgMarks[i]=(examMarks[i]*(100-cwWeighting)+courseworkmarks[i]*cwWeighting)/100;
System.out.print(avgMarks[i] + "\t" );
}
}
Upvotes: 0
Views: 2695
Reputation: 3806
Here is a basic example of how you can set the values of a bar chart in JFreechart which may be able to help you, depending on the library you are using to create your chart:
public class BarChartExample {
public static void main(String[] args) {
// Create a simple Bar chart
double[] dub = {12.2, 15.4, 18.3, 9.3, 7.7}; //Array
String[] student = {"Bob", "Dave", "William", "Boris", "Rick"}; //Array
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); //Create dataset
for(int i = 0; i < dub.length; i++){
dataset.setValue(dub[i], "Marks", student[i]); //Setting the values
}
JFreeChart chart = ChartFactory.createBarChart3D("Goal comparison",
"Marks", "Students", dataset, PlotOrientation.VERTICAL,
false, true, false); //Chart creation
try {
ChartUtilities.saveChartAsJPEG(new File("D:\\Users\\user2777005\\Desktop\\Barchart.jpg"), chart, 500, 300);
} catch (IOException e) {
System.err.println("Problem occurred creating chart.");
}}}
Good luck!
Upvotes: 2