我在我的struts應用程序中使用Jfree圖表。 Y軸在裝運件上,X軸是供應商代碼,副軸用於裝運量%。我在這裏面臨的問題是,如果超過貨運%值全部達到零,它也會隱藏X軸供應商值。我如何克服,以避免這一點。我附上了這裏的屏幕截圖供您考慮。感謝所有人閱讀這個問題。如何克服JFreeChart問題?
我粘貼在這裏我的代碼爲您清晰的認識,
/**
*
* @param response
* @throws IOException
*/
public void writeOccurrenceBasedParetoChart(HttpServletResponse response) throws IOException
{
String METHOD_NAME = "writeOccurrenceBasedParetoChart";
log.entering(CLASS_NAME, METHOD_NAME);
CategoryDataset dataset1 = createDataSetForPercentBasedChart1();
CategoryDataset dataset2 = createDataSetForPercentBasedChart2();
String rangeAxisLabel = "";
String numOfSupplrs = "";
//Behind Schedule "4"
if(searchRatingElement.equalsIgnoreCase("4"))
{
System.out.println("*******************searchRatingElement"+searchRatingElement);
rangeAxisLabel = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpcs");
}
else
{
rangeAxisLabel = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpcs");
}
JFreeChart chart = ChartFactory.createBarChart(
"", // chart title
"", // domain axis label
rangeAxisLabel, // range axis label
dataset1, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDataset(1, dataset2);
plot.mapDatasetToRangeAxis(1, 1);
CategoryItemRenderer renderer1 = plot.getRenderer();
renderer1.setSeriesItemLabelGenerator(0,new CategoryItemLabelGenerator() {
public String generateRowLabel(final CategoryDataset arg0, final int arg1) {
// TODO Auto-generated method stub
return null;
}
public String generateLabel(final CategoryDataset dataset1,final int series,final int category) {
String result = null;
//CHAPTER 12. ITEM LABELS 91
final Number value = dataset1.getValue(series, category);
if (value != null) {
final double v = value.doubleValue();
if (v > 0) {
result = value.toString(); // could apply formatting here
}
}
return result;
}
public String generateColumnLabel(final CategoryDataset arg0, final int arg1) {
// TODO Auto-generated method stub
return null;
}
});
renderer1.setSeriesItemLabelsVisible(0,true);
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
renderer.setSeriesOutlinePaint(0,Color.BLACK);
renderer.setDrawBarOutline(true);
renderer.setMaximumBarWidth(0.02);
renderer.setSeriesPaint(0,new Color(170, 0, 85));
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(.6));
domainAxis.setLabelFont(new Font("Helvetica", Font.BOLD, 14));
domainAxis.setTickLabelFont(new Font("Helvetica", Font.BOLD, 14));
//Behind Schedule "4"
if(searchRatingElement.equalsIgnoreCase("4"))
{
System.out.println("||||||||||||||||||||||||||||||||searchRatingElement"+searchRatingElement);
numOfSupplrs = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.bhndschpctforchart");
}
else
{
numOfSupplrs = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpctforchart");
}
ValueAxis axis2 = new NumberAxis(numOfSupplrs);
axis2.setLabelFont(new Font("Helvetica", Font.BOLD, 14));
axis2.setTickLabelFont(new Font("Helvetica", Font.PLAIN, 14));
//if(!this.isValueDataZeros)
//axis2.setRange(0,13);
plot.setRangeAxis(1, axis2);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
// if(!this.isNumberDataZeros)
//rangeAxis.setRange(0,90);
//rangeAxis.setTickUnit(new NumberTickUnit(1));
rangeAxis.setLabelFont(new Font("Helvetica", Font.BOLD, 14));
rangeAxis.setTickLabelFont(new Font("Helvetica", Font.PLAIN, 14));
LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
renderer2.setSeriesPaint(0, Color.BLUE);
/*Start */
renderer2.setSeriesItemLabelGenerator(0,new CategoryItemLabelGenerator() {
public String generateRowLabel(final CategoryDataset arg0, final int arg1) {
// TODO Auto-generated method stub
return null;
}
public String generateLabel(final CategoryDataset dataset1,final int series,final int category) {
String result = null;
//CHAPTER 12. ITEM LABELS 91
final Number value = dataset1.getValue(series, category);
if (value != null) {
final double v = value.doubleValue();
if (v > 0) {
result = value.toString(); // could apply formatting here
}
}
return result;
}
public String generateColumnLabel(final CategoryDataset arg0, final int arg1) {
// TODO Auto-generated method stub
return null;
}
});
renderer2.setSeriesItemLabelsVisible(0,true);
/* End */
plot.setRenderer(1, renderer2);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart,
1150, 450);
}
/**
*
*/
public DefaultCategoryDataset createDataSetForPercentBasedChart1()
{
String METHOD_NAME = "createDataSetForPercentBasedChart1";
log.entering(CLASS_NAME, METHOD_NAME);
DefaultCategoryDataset dataSetForOccurrenceBasedChart = new DefaultCategoryDataset();
boolean flagForPercentPareto = false;
String occurrenceCountLabelName = null;
try
{
if(paretoReportBasedDataQO != null && paretoReportBasedDataQO.size()>0)
{
//Behind Schedule "4"
if(searchRatingElement.equalsIgnoreCase("4"))
{
occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.bhndschpctforchart");
}
else
{
occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpcs");
}
if(!flagForPercentPareto)
{
for(int i =0;i<paretoReportBasedDataQO.size();i++)
{
dataSetForOccurrenceBasedChart.addValue(paretoReportBasedDataQO.get(i).getOverShipmentPiecesCount(), occurrenceCountLabelName, paretoReportBasedDataQO.get(i).getSupplierName());
}
}
}
}
catch(Exception exceptionOccurrenceBasedChart)
{
flagForPercentPareto = true;
System.out.println("Exception In createDataSetForPercentBasedChart1 : "+exceptionOccurrenceBasedChart.getMessage());
}
log.exiting(CLASS_NAME, METHOD_NAME);
return dataSetForOccurrenceBasedChart;
}
/**
*
*/
public DefaultCategoryDataset createDataSetForPercentBasedChart2()
{
String METHOD_NAME = "createDataSetForPercentBasedChart2";
log.entering(CLASS_NAME, METHOD_NAME);
DefaultCategoryDataset dataSetForOccurrenceBasedChart = new DefaultCategoryDataset();
boolean flagForPercentPareto = false;
String occurrenceCountLabelName = null;
try
{
if(paretoReportBasedDataQO != null && paretoReportBasedDataQO.size()>0)
{
if(searchRatingElement.equalsIgnoreCase("4"))
{
occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.bhndschpctforchart");
}
else
{
occurrenceCountLabelName = I18nMessageUtil.getMessage(CommonUtil.getLocale(), "label.reports.wdid.ovrshptpctforchart");
}
if(!flagForPercentPareto)
{
for(int i =0;i<paretoReportBasedDataQO.size();i++)
{
dataSetForOccurrenceBasedChart.addValue(paretoReportBasedDataQO.get(i).getOverShipmentPercentageCount(), occurrenceCountLabelName, paretoReportBasedDataQO.get(i).getSupplierName());
}
}
}
}
catch(Exception exceptionOccurrenceBasedChart)
{
flagForPercentPareto = true;
System.out.println("Exception In createDataSetForPercentBasedChart2 : "+exceptionOccurrenceBasedChart.getMessage());
}
log.exiting(CLASS_NAME, METHOD_NAME);
return dataSetForOccurrenceBasedChart;
}
它很難找出什麼你問,我可以看到你在出貨量是零,但你仍然有X(域)軸。你可以舉一個非零過度的例子 – GrahamA
如果供應商在場,它顯示在X軸上(AA8SA)。我可以在這裏發佈代碼嗎? –