gpt4 book ai didi

java - 如何克服 JFreeChart 问题?

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:14 24 4
gpt4 key购买 nike

我在我的 struts 应用程序中使用 Jfree 图表。 Y 轴是超额装运件数,X 轴是供应商代码,第二轴用于超额装运百分比。我在这里面临的问题是,如果超额发货百分比值全部为零,它也会隐藏 X 轴供应商值。我该如何克服才能避免这种情况。我已附上屏幕截图供您考虑。感谢大家阅读这个问题。

enter image description here

为了让您清楚理解,我将代码粘贴在这里,

/**
*
* @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;
}

最佳答案

如果将标签放在其他位置会怎样?例如:

renderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));

关于java - 如何克服 JFreeChart 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13717632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com