这是我用于显示特定值的代码,例如。平均一个文件。现在我想为这两个文件中的值创建条形图,并用不同的颜色表示。请帮助:
公共 JFreeChart createBarChartFromCSV() {
CSVFileReader csvReader = new CSVFileReader();
List<String[]> csvData = null;
int indexOfAverage = 0;
csvData = csvReader.getDataFromCSV(CSVFileReader.CSVFILENAME);
for(String[] columnArray : csvData)
for(int i = 0; i< columnArray.length; i++)
if(columnArray[i].equalsIgnoreCase("average")){
indexOfAverage = i;
break;
}
if(indexOfAverage == 0){
System.err.println("Error retrieving data from CSV File !!");
System.exit(0);
}
JFreeChart barChart = generateBarChart(csvData, indexOfAverage);
return barChart;
}
private JFreeChart generateBarChart(List<String[]> csvData, int columnIndex){
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
final String YAXIS_NAME = csvData.get(0)[columnIndex]; //value returned is "average"
final String XAXIS_NAME = csvData.get(0)[0]; //value returned is "sampler_label"
for(int i = 1; i < csvData.size() - 1; i++){
long averageValue = Long.parseLong(csvData.get(i)[columnIndex]);
String columnKey = csvData.get(i)[0];
dataSet.setValue(averageValue, YAXIS_NAME, columnKey); // plot the graph
}
JFreeChart chart = ChartFactory.createBarChart("Comparison between the average of 2 values", XAXIS_NAME, YAXIS_NAME, dataSet, PlotOrientation.VERTICAL, false, true, false);
return chart;
}