如何使用 JFreeChart 创建条形图,通过可见的提示缩短太长的条形图?

发布于 2024-08-03 23:58:06 字数 465 浏览 8 评论 0原文

我想创建一个条形图,但是应该缩短非常高的值。下图就是一个示例:

缩短图
(来源:epa.gov

我希望我想要的很清楚。

我的问题是:如何使用 JFreeChart 做到这一点。如果 JFreeChart 无法实现,您可以推荐替代的开源 Java 库来生成此类输出。

I want to create a bar-chart, but extraordinary high values should be shortened. An example is this image:

shortened graph
(source: epa.gov)

I hope it is clear what I want.

My question is: How can I do this with JFreeChart. If it isn't possible with JFreeChart you can possibly recommend alternative open-source Java-libraries to produce such an output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2024-08-10 23:58:06

您可以使用 CombinedDomainCategoryPlotCombinedDomainXYPlot 来完成此操作。将第一个图的范围轴设置为截止值,然后对第二个图执行类似的操作。然后将它们添加到组合图中。

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class PlayChart {

    public static void main(String[] args) {


        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        ds.addValue(100, "A", "A");
        ds.addValue(200, "A", "B");
        ds.addValue(400, "A", "C");
        ds.addValue(500, "A", "D");
        ds.addValue(2000, "A", "E");


        JFreeChart bc = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts",  ds, PlotOrientation.VERTICAL, true, false, false);
        JFreeChart bcTop = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts",  ds, PlotOrientation.VERTICAL, true, false, false);

        CombinedDomainCategoryPlot combinedPlot = new CombinedDomainCategoryPlot();
        CategoryPlot topPlot = bcTop.getCategoryPlot();
        NumberAxis topAxis = (NumberAxis) topPlot.getRangeAxis();
        topAxis.setLowerBound(1500);
        topAxis.setUpperBound(2000);

        combinedPlot.add(topPlot, 1);
        CategoryPlot mainPlot = bc.getCategoryPlot();
        combinedPlot.add(mainPlot, 5);

        NumberAxis mainAxis = (NumberAxis) mainPlot.getRangeAxis();;
        mainAxis.setLowerBound(0);
        mainAxis.setUpperBound(600);

        JFreeChart combinedChart = new JFreeChart("Test", combinedPlot);

        ChartFrame cf = new ChartFrame("Test", combinedChart);
        cf.setSize(800, 600);
        cf.setVisible(true);

    }

}

这些图将共享相同的 X 轴。您需要使用渲染器来设置颜色和标签。

删除了无效的 ImageShack 链接

You could do it with a CombinedDomainCategoryPlot or CombinedDomainXYPlot. Set the range axis of the first plot to your cut off value and then do something similar with the second plot. Then add them to a combined plot.

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class PlayChart {

    public static void main(String[] args) {


        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        ds.addValue(100, "A", "A");
        ds.addValue(200, "A", "B");
        ds.addValue(400, "A", "C");
        ds.addValue(500, "A", "D");
        ds.addValue(2000, "A", "E");


        JFreeChart bc = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts",  ds, PlotOrientation.VERTICAL, true, false, false);
        JFreeChart bcTop = ChartFactory.createBarChart("My Bar Chart", "Things", "Counts",  ds, PlotOrientation.VERTICAL, true, false, false);

        CombinedDomainCategoryPlot combinedPlot = new CombinedDomainCategoryPlot();
        CategoryPlot topPlot = bcTop.getCategoryPlot();
        NumberAxis topAxis = (NumberAxis) topPlot.getRangeAxis();
        topAxis.setLowerBound(1500);
        topAxis.setUpperBound(2000);

        combinedPlot.add(topPlot, 1);
        CategoryPlot mainPlot = bc.getCategoryPlot();
        combinedPlot.add(mainPlot, 5);

        NumberAxis mainAxis = (NumberAxis) mainPlot.getRangeAxis();;
        mainAxis.setLowerBound(0);
        mainAxis.setUpperBound(600);

        JFreeChart combinedChart = new JFreeChart("Test", combinedPlot);

        ChartFrame cf = new ChartFrame("Test", combinedChart);
        cf.setSize(800, 600);
        cf.setVisible(true);

    }

}

The plots will share the same X-axis. You'll need to play with the renderers to set colours and labels.

removed dead ImageShack link

椒妓 2024-08-10 23:58:06

我不确定您是否可以在 JFreeChart 中做到这一点。

一个解决方案(这不是很好)是将图表渲染为图像,然后使用 RenderedImage,而不是 JFreeChart。不幸的是,这会有点痛苦,因为你可能想在 y 轴等的特定位置进行切割。

I'm not sure you can do that in JFreeChart.

A solution (which is not nice) is to render the chart to an image and then manipulate it (chop it up etc.) as an image, using RenderedImage, and not as a JFreeChart. Unfortunately that's going to be a bit of a pain since you'd probably want to chop at a particular place on the y-axis etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文