Struts アプリケーションで Jfree チャートを使用しています。Y 軸は超過出荷個数、X 軸はサプライヤ コードで、第 2 軸は超過出荷 % に使用されます。私がここで直面した問題は、超過出荷 % 値がすべてゼロになると、X 軸のサプライヤー値も非表示になることです。これを回避するにはどうすれば克服できますか。ご参考までに、ここにスクリーンショットを添付しました。この質問を読んでくれてありがとう。
あなたの明確な理解のために、ここに私のコードを貼り付けました。
/**
*
* @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;
}