2

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

测试方法的图表

注意漂亮的 LogAxis 标签。

现在,如果我在 Tomcat 的控制器中执行完全相同的代码,我会得到下图:

从 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 控制器执行的相同代码在对数标记之间有奇怪的、不正确的刻度线。

4

1 回答 1

1

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

rangeAxis1.setAutoTickUnitSelection(false);

如果有人可以添加有关此的其他信息,但是我将不胜感激

于 2013-04-19T19:55:43.860 回答