这有点奇怪。我有代码在JFreeChart中生成图表并将其保存为.svg文件。如果我通过Eclipse中的测试方法运行代码,它输出如下:
请注意好的LogAxis标签。
现在如果我在Tomcat中的控制器中执行完全相同的代码,我会看到以下图像:
请注意,虽然弧线相同,但第二张图像上的轴不再显示出良好的对数级数。这不会打扰我,除了在第二种情况下,刻度标记不仅没有显示出很好的对数,它们也是错误的。
以下是生成图表的控制器代码。请注意,有些东西是以不正常的方式硬连线 - 这是因为这是一个快速原型
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控制器执行的相同代码在对数之间具有奇怪的,不正确的刻度标记。
答案 0 :(得分:1)
嗯,我仍然不知道为什么tomcat中的行为与Eclipse中的行为不同。也就是说,插入以下代码行(对于每个数据轴)似乎解决了这个问题:
rangeAxis1.setAutoTickUnitSelection(假);
如果有人可以在此添加其他信息,我会很感激