如何在JFreeChart中禁用缩放? [英] How to disable scaling in JFreeChart?

查看:329
本文介绍了如何在JFreeChart中禁用缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用JFreeChart构建引擎来显示图形。这是一个在Tomcat + Java 1.5.0上运行的Web服务,并将图表呈现为PNG和JPEG(使用ChartUtilities.writeChartAs {PNG,JPEG}())。

我们遇到了一个问题,JFreeChart似乎将绘图区域内的所有东西都缩放,但只有几个像素。结果是图表看起来不一致,例如:




  • 小勾号有时会水平延伸,因此它们看起来像是两个像素宽

  • 我们使用绘图区右上方的小图像作为水印。这在水平和垂直方向上拉伸了一个像素(但不完全)在其中间。

  • 背景网格线似乎出现在子像素边界上。我还没有找到一种方法来创建一个准确点缀的网格线。



我们尝试了1.0.9和1.0.13,完全相同的结果(除了较小版本中没有的次要记号)。此外,将图像渲染为Frame而不是JPEG / PNG会产生相同的结果。

预先感谢帮助:)

编辑:一个SSCCE:

  @Test 
public void testScaling1()throws InterruptedException {

//加载图像:
组件dummy = new Component(){};
MediaTracker tracker =新MediaTracker(dummy);
Image img = Toolkit.getDefaultToolkit()。getImage(C:\\My\Image.gif);
tracker.addImage(img,0);
tracker.waitForAll();

//构建数据集和基础图表。
TimeSeriesCollection dataset = new TimeSeriesCollection();

TimeSeries ts =新的TimeSeries(Sample);
ts.add(new Second(0,0,1,1,1900),1.0);
ts.add(new Second(1,0,0,1,1,1900),3.0);
ts.add(new Second(2,0,1,1,1900),4.0);
ts.add(new Second(3,0,1,1,1900),2.0);

dataset.addSeries(ts);

JFreeChart chart = ChartFactory.createTimeSeriesChart(
blabla,
null,
null,
数据集,
true,
true,
false
);

//在右上角添加BG图像。
XYPlot xy = chart.getXYPlot();
xy.setBackgroundAlpha(0.0F);
xy.setBackgroundImage(img);
xy.setBackgroundImageAlignment(Align.NORTH_WEST);
xy.setBackgroundImageAlpha(1.0F);

paintChart(图表);




$ b $ p
$ b

使用带小字体文本或网格的图片。这将在背景图像上显示缩放效果。



编辑2:
我们使用子类或代理Renderer并在文本中绘制标签drawItem()(或类似的)方法。这很好。
然而,小蜱现在是一个问题 - 它们似乎也在缩小。例如:见第9和第15个勾号。



看看底部http://img14.imageshack.us/img14/3625/76676732.jpg

解决方案

我无法重现您使用 saveChartAsJPEG() writeChartAsPNG()使用版本描述的效果1.0.13,Java 1.5,Mac OS X,代码如下:

  try {
ChartUtilities.writeChartAsPNG(新的FileOutputStream(
新文件(test.png)),图表,600,400);
} catch(IOException ex){
ex.printStackTrace();
}

屏幕是否显示相同的工件?当您更改 WIDTH HEIGHT 参数或省略水印时会发生什么?您是否使用了特殊字体的特殊字体?您是否尝试过其他平台?



您可以运行 TimeSeriesChartDemo1 ,如下所示:


java -cp jfreechart-1.0.13.jar:jcommon-1.0.16.jar org.jfree.chart.demo.TimeSeriesChartDemo1

Mac OS 10.5.8,Java 1.5.0_24,JFreeChart 1.0.13, TimeSeriesDemo1 ,使用 saveChartAsPNG() ImageIO.read() setBackgroundImage()。但是, setBackgroundImageAlignment(Align.NORTH_WEST)有点时髦。 alt text http://i45.tinypic.com/68wxgp.png


We're using JFreeChart to build an engine to display graphs. This is a web service that runs on Tomcat + Java 1.5.0, and renders charts to PNGs and JPEGs (using ChartUtilities.writeChartAs{PNG,JPEG}() ).

We've run into a problem where JFreeChart seems to scale everything inside the Plot area, but only by a few pixels. The result is that the graph looks inconsistent, e.g.:

  • Minor ticks are sometimes stretched horizontally, so that they seem to be two pixels wide instead of one.
  • We use a small image in the top-right of the plot area as a watermark. This is stretched by one pixel horizontally and vertically somewhere near (but not exactly) its middle.
  • Background grid lines seem to appear on sub-pixel boundaries. I have not found a way to create an accurately dotted grid line.

We have tried both 1.0.9 and 1.0.13, with exactly the same results (except for the minor ticks, which were not available in the older version). Also, rendering the image to a Frame instead of JPEG/PNG produced an identical result.

Help is greatly appreciated, in advance :)

EDIT: An SSCCE:

@Test
public void testScaling1() throws InterruptedException {

    // Load Image:
    Component dummy = new Component() {};
    MediaTracker tracker = new MediaTracker(dummy);
    Image img = Toolkit.getDefaultToolkit().getImage("C:\\My\Image.gif");
    tracker.addImage(img, 0);
    tracker.waitForAll();

    // Build Data set and base chart.
    TimeSeriesCollection dataset = new TimeSeriesCollection();

    TimeSeries ts = new TimeSeries("Sample");
    ts.add(new Second(0, 0, 0, 1, 1, 1900), 1.0);
    ts.add(new Second(1, 0, 0, 1, 1, 1900), 3.0);
    ts.add(new Second(2, 0, 0, 1, 1, 1900), 4.0);
    ts.add(new Second(3, 0, 0, 1, 1, 1900), 2.0);

    dataset.addSeries(ts);

    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "blabla",
            null,
            null,
            dataset,
            true,
            true,
            false
    );

    // Add BG image in top-right corner.
    XYPlot xy = chart.getXYPlot();
    xy.setBackgroundAlpha(0.0F);
    xy.setBackgroundImage(img);
    xy.setBackgroundImageAlignment(Align.NORTH_WEST);
    xy.setBackgroundImageAlpha(1.0F);

    paintChart(chart);

}

Use an image with small-font text, or a grid. This will show the scaling effect on the background image.

Edit 2: We've resorted to subclassing or proxying Renderers and drawing the label in text in their drawItem() (or similar) methods. This works well. However, minor ticks are now a problem - they also seem to be getting scaled. E.g.: See the 9th and 15th ticks.

look at the bottom http://img14.imageshack.us/img14/3625/76676732.jpg

解决方案

I am unable to reproduce the effect you describe using either saveChartAsJPEG() or writeChartAsPNG() with version 1.0.13, Java 1.5, Mac OS X, in code like this:

try {
    ChartUtilities.writeChartAsPNG(new FileOutputStream(
        new File("test.png")), chart, 600, 400);
} catch (IOException ex) {
    ex.printStackTrace();
}

Does the screen exhibit the same artifacts? What happens when you change the WIDTH and HEIGHT parameters or omit the watermark? Are you using special fonts with unusual metrics? Have you tried a different platform?

You can run TimeSeriesChartDemo1 as follows:

java -cp jfreechart-1.0.13.jar:jcommon-1.0.16.jar org.jfree.chart.demo.TimeSeriesChartDemo1

Mac OS 10.5.8, Java 1.5.0_24, JFreeChart 1.0.13, TimeSeriesDemo1, using saveChartAsPNG(), ImageIO.read() and setBackgroundImage(). setBackgroundImageAlignment(Align.NORTH_WEST) is a little funky, though.

alt text http://i45.tinypic.com/68wxgp.png

这篇关于如何在JFreeChart中禁用缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆