JfreeChart 中的自动缩放 Y 轴

发布于 2024-11-28 02:25:59 字数 1172 浏览 2 评论 0原文

我正在使用 JFreeChart 在我的 Java 应用程序中创建蜡烛图。但是,我的图表最终看起来像这样:

capturebhx http://imageshack.us/photo/my-images/69/capturebhx。 png/

我希望 Y 轴自动缩放,以便图表看起来更像这样:

“捕获2wl” http://imageshack.us/photo/my-images/717/capture2wl。 png/

我认为 org.jfree.chart.axis.NumberAxis.configure() 会执行此操作,但我不确定。我似乎找不到将我的 JFreeChart 对象或 ChartPanel 对象与此 NumberAxis 对象绑定的方法。请帮助我,我迷路了,一直在寻找很长时间来尝试将这些物体绑在一起。或者,如果您能找到其他方法,那就太好了!

一些代码:

...
private DefaultHighLowDataset dataset;
private JFreeChart chart;
private ChartPanel chart_panel;

...

// creates dataset, then chart from dataset, then chart_panel from chart
dataset = new DefaultHighLowDataset("", date, high, low, open, close, volume);
chart = ChartFactory.createCandlestickChart("Blank", "Time", "Price", dataset, false);
chart_panel = new ChartPanel(chart); // what you see in the images
...

I am using JFreeChart to create candlestick charts in my Java app. However, my charts end up looking like this:

capturebhx
http://imageshack.us/photo/my-images/69/capturebhx.png/

I would like to have the Y-axis automatically scaled so that the chart looks more like this:

capture2wl
http://imageshack.us/photo/my-images/717/capture2wl.png/

I think org.jfree.chart.axis.NumberAxis.configure() will do this, but I'm not sure. I can't seem to find a way to tie my JFreeChartobject, or ChartPanel object to this NumberAxis object. Please help me, I am lost and have been looking for a long time to try and tie these objects together. Or, if you can find another way, that'd be great too!

Some code:

...
private DefaultHighLowDataset dataset;
private JFreeChart chart;
private ChartPanel chart_panel;

...

// creates dataset, then chart from dataset, then chart_panel from chart
dataset = new DefaultHighLowDataset("", date, high, low, open, close, volume);
chart = ChartFactory.createCandlestickChart("Blank", "Time", "Price", dataset, false);
chart_panel = new ChartPanel(chart); // what you see in the images
...

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

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

发布评论

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

评论(2

乜一 2024-12-05 02:25:59

请务必< code>setAutoRangeInincludesZero(false) 或“轴范围……强制包含零。”

附录:

我仍然不知道如何将 NumberAxis 对象链接到 ChartPanel 对象或 JFreeChart 对象。

您可能需要查看 org.jfree.chart.demo此处。如果这是未知领域,我会推荐JFreeChart开发者指南

免责声明:不隶属于 Object Refinery Limited;只是一个满意的客户和非常小的贡献者。

Be sure to setAutoRangeIncludesZero(false) or "the axis range…is forced to include zero."

Addendum:

I still don't know how to link a NumberAxis object to a ChartPanel object or JFreeChart object.

You may want to look into the examples in org.jfree.chart.demo and here. If this is terra incognita, I'd recommend The JFreeChart Developer Guide.

Disclaimer: Not affiliated with Object Refinery Limited; just a satisfied customer and very minor contributor.

2024-12-05 02:25:59

我是这样做的:

     final JFreeChart chart = ChartFactory.createCandlestickChart(
            "Candlestick Demo", "Time", "Price", dataset, false);

    double lowestLow = getLowestLow(dataset);
    double highestHigh = getHighestHigh(dataset);

    chart.getXYPlot().getRangeAxis().setRange(lowestLow*0.95, highestHigh*1.05);

我使用这些函数计算最低低点和最低高点

private double getLowestLow(DefaultHighLowDataset dataset){
    double lowest;
    lowest = dataset.getLowValue(0,0);
    for(int i=1;i<dataset.getItemCount(0);i++){
        if(dataset.getLowValue(0,i) < lowest){
            lowest = dataset.getLowValue(0,i);
        }
    }

    return lowest;
}


private double getHighestHigh(DefaultHighLowDataset dataset){
    double highest;
    highest = dataset.getHighValue(0,0);
    for(int i=1;i<dataset.getItemCount(0);i++){
        if(dataset.getLowValue(0,i) > highest){
            highest = dataset.getHighValue(0,i);
        }
    }

    return highest;
}   

这似乎给了我一个非常好的烛台图,它很好地利用了 Y 轴范围。希望这有帮助。

I did it like this:

     final JFreeChart chart = ChartFactory.createCandlestickChart(
            "Candlestick Demo", "Time", "Price", dataset, false);

    double lowestLow = getLowestLow(dataset);
    double highestHigh = getHighestHigh(dataset);

    chart.getXYPlot().getRangeAxis().setRange(lowestLow*0.95, highestHigh*1.05);

I calculate the lowest low and lowest high using these functions

private double getLowestLow(DefaultHighLowDataset dataset){
    double lowest;
    lowest = dataset.getLowValue(0,0);
    for(int i=1;i<dataset.getItemCount(0);i++){
        if(dataset.getLowValue(0,i) < lowest){
            lowest = dataset.getLowValue(0,i);
        }
    }

    return lowest;
}


private double getHighestHigh(DefaultHighLowDataset dataset){
    double highest;
    highest = dataset.getHighValue(0,0);
    for(int i=1;i<dataset.getItemCount(0);i++){
        if(dataset.getLowValue(0,i) > highest){
            highest = dataset.getHighValue(0,i);
        }
    }

    return highest;
}   

This seems to give me a very nice candlestick chart that makes good use of the Y-axis range. Hope this helps.

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