gpt4 book ai didi

tomcat - JFreeChart 轴显示不同取决于执行环境

转载 作者:行者123 更新时间:2023-11-28 22:00:18 26 4
gpt4 key购买 nike

这有点奇怪。我有在 JFreeChart 中生成图表并将其保存为 .svg 文件的代码。如果我通过 Eclipse 中的测试方法运行代码,它的输出如下:

Graph from Test Method

注意漂亮的 LogAxis 标签。

现在,如果我在 Tomcat 的 Controller 中执行完全相同的代码,我将得到以下图像:

Graph as created from Tomcat

请注意,虽然弧线相同,但第二张图片上的轴不再显示漂亮的对数级数。这不会打扰我,除了在第二种情况下,刻度线不仅没有显示漂亮的对数,而且是错误的。

以下是生成图形的 Controller 代码。请注意,有些东西以一种通常不会出现的方式硬连线 - 这是因为这是一个快速原型(prototype)



private static final String pn = "T495D477M004ATE100";

private File generateMaxCurrentVoltageGraph()
throws IOException, InitializationException, ClassNotFoundException
{
String title = "Current and Voltage - " + pn + " @ 25\u00B0C with 2 VDC Bias";
String frequencyAxisLabel = "Frequency (Hz)";
String valueAxisLabel1 = "Max Current (ARMS)";
String valueAxisLabel2 = "Max Voltage (VRMS)";

List datasets = createVoltageCurrentDatasets();
JFreeChart chart = ChartFactory.createXYLineChart(title, frequencyAxisLabel, valueAxisLabel1, null, PlotOrientation.VERTICAL, true, false, false);
chart.setBackgroundPaint(Color.white);

final XYPlot plot = chart.getXYPlot();
plot.setDataset(0, datasets.get(0));
plot.setDataset(1, datasets.get(1));

plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);

final XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
renderer1.setSeriesShapesVisible(0, false);
renderer1.setSeriesShapesVisible(1, false);
renderer1.setSeriesPaint(0, Color.red);
plot.setRenderer(0, renderer1);
final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
renderer2.setSeriesShapesVisible(0, false);
renderer2.setSeriesShapesVisible(1, false);
renderer2.setSeriesPaint(0, Color.blue);
plot.setRenderer(1, renderer2);

final LogAxis rangeAxis1 = new LogAxis(valueAxisLabel1);
rangeAxis1.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.ENGLISH));
rangeAxis1.setRange(0.1, 10.0); //100 mA to 10 A
rangeAxis1.setNumberFormatOverride(new UnitNumberFormat(UnitValue.AMPS));
rangeAxis1.setLabelPaint(Color.red);
plot.setRangeAxis(0, rangeAxis1);

final LogAxis rangeAxis2 = new LogAxis(valueAxisLabel2);
rangeAxis2.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.ENGLISH));
rangeAxis2.setRange(0.01, 10.0); //10 mV to 10 V
rangeAxis2.setNumberFormatOverride(new UnitNumberFormat(UnitValue.VOLTS));
rangeAxis2.setLabelPaint(Color.blue);
plot.setRangeAxis(1, rangeAxis2);
plot.mapDatasetToRangeAxis(1, 1);

final LogAxis domainAxis = new LogAxis(frequencyAxisLabel);
domainAxis.setStandardTickUnits(LogAxis.createLogTickUnits(Locale.ENGLISH));
domainAxis.setRange(100, 10000000); //100Hz to 10MHz
domainAxis.setNumberFormatOverride(new UnitNumberFormat(UnitValue.HERTZ));
plot.setDomainAxis(domainAxis);

LegendTitle legendTitle = chart.getLegend();
LegendTitle legendTitleNew = new LegendTitle(plot, new ColumnArrangement(), new ColumnArrangement());
legendTitleNew.setPosition(legendTitle.getPosition());
legendTitleNew.setBackgroundPaint(legendTitle.getBackgroundPaint());
legendTitleNew.setBorder(0.25, 0.25, 0.25, 0.25);
chart.removeLegend();
chart.addLegend(legendTitleNew);

chart.getTitle().setBackgroundPaint(Color.lightGray);
chart.getTitle().setFont(rangeAxis1.getLabelFont());
chart.getTitle().setExpandToFitSpace(true);
chart.setPadding(new RectangleInsets(10, 10, 10, 10));

File graphFile = File.createTempFile("CurrentVoltageGraph", ".svg");

return writeGraphFile(chart, graphFile);
}


private List createVoltageCurrentDatasets()
throws FileNotFoundException, IOException, InitializationException, ClassNotFoundException
{
List datasets = new ArrayList();
XYSeriesCollection dataset1 = new XYSeriesCollection();
XYSeriesCollection dataset2 = new XYSeriesCollection();
// String sourceDataPath = "/etc/intellidata/prototype/CurrentVoltageTestGraphData.csv";
String sourceDataPath = "C:\\Users\\BrianTrezise\\Desktop\\Downloads\\CurrentVoltageTestGraphData.csv";
CSVRowIterator csv = new CSVRowIterator(new FileInputStream(sourceDataPath));
XYSeries series1 = new XYSeries("T495D477M004ATE100-I");
XYSeries series2 = new XYSeries("T495D477M004ATE100-V");

while(csv.hasNext())
{
Row row = csv.next();
String frequencyString = (String) row.get("Frequency");
String currentString = (String) row.get("Current");
String voltageString = (String) row.get("Voltage");

Double frequency = decodeValue(frequencyString);
Double current = decodeValue(currentString);
Double voltage = decodeValue(voltageString);

series1.add(frequency, current);
series2.add(frequency, voltage);
}

dataset1.addSeries(series1);
datasets.add(dataset1);
dataset2.addSeries(series2);
datasets.add(dataset2);

return datasets;
}


private File writeGraphFile(JFreeChart chart, File graphFile)
throws IOException
{
// Get a DOMImplementation and create an XML document
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
Document document = domImpl.createDocument(null, "svg", null);

// Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
svgGenerator.setSVGCanvasSize(new Dimension(720, 470));

// draw the chart in the SVG generator
Rectangle bounds = new Rectangle(10, 10, 700, 450);
chart.draw(svgGenerator, bounds);

// Write svg file
OutputStream outputStream = new FileOutputStream(graphFile);
Writer out = new OutputStreamWriter(outputStream, "UTF-8");
svgGenerator.stream(out, true /* use css */);
outputStream.flush();
outputStream.close();

return graphFile;
}

有人知道为什么它在 Tomcat 中的行为与直接从 Eclipse 中执行时的行为不同吗?注意:我还生成了第二个做同样事情的图表。从测试方法来看工作正常,使用 Tomcat Controller 执行的相同代码在对数之间有奇怪的、不正确的刻度线。

最佳答案

嗯,我仍然不知道为什么 tomcat 中的行为与 Eclipse 中的不同。也就是说,插入以下代码行(针对每个数据轴)似乎可以解决问题:

rangeAxis1.setAutoTickUnitSelection(false);

如果有人可以添加额外的信息,我将不胜感激

关于tomcat - JFreeChart 轴显示不同取决于执行环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110000/

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