如何使用JFreeChart创建一个条形图,缩短太长的条形并显示可见的提示? [英] How can I create a bar-chart with JFreeChart, that shortens too long bars with a visible hint?

查看:252
本文介绍了如何使用JFreeChart创建一个条形图,缩短太长的条形并显示可见的提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我想问的是:我怎么办?这与 JFreeChart 。如果JFreeChart不可能,你可以推荐其他开源的Java库来产生这样的输出。

解决方案

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

  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链接


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

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.

解决方案

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

这篇关于如何使用JFreeChart创建一个条形图,缩短太长的条形并显示可见的提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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