java JfreeChart 中的自动缩放 Y 轴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6945718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto-Scale Y-Axis in JfreeChart
提问by supercoder
I am using JFreeChart
to create candlestick charts in my Java app. However, my charts end up looking like this:
我正在使用JFreeChart
我的 Java 应用程序创建烛台图表。但是,我的图表最终看起来像这样:
http://imageshack.us/photo/my-images/69/capturebhx.png/
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:
我想让 Y 轴自动缩放,使图表看起来更像这样:
http://imageshack.us/photo/my-images/717/capture2wl.png/
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 JFreeChart
object, 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!
我认为org.jfree.chart.axis.NumberAxis.configure()
会这样做,但我不确定。我似乎无法找到一种方法来将我的JFreeChart
对象或ChartPanel
对象绑定到这个NumberAxis
对象。请帮助我,我迷路了,一直在寻找很长时间试图将这些对象联系在一起。或者,如果你能找到另一种方式,那也太棒了!
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
...
回答by trashgod
Be sure to setAutoRangeIncludesZero(false)
or "the axis range…is forced to include zero."
确保setAutoRangeIncludesZero(false)
或“轴范围……强制包括零”。
Addendum:
附录:
I still don't know how to link a
NumberAxis
object to aChartPanel
object orJFreeChart
object.
我仍然不知道如何将
NumberAxis
对象链接到ChartPanel
对象或JFreeChart
对象。
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?.
您可能需要查看org.jfree.chart.demo
和此处的示例。如果这是terra incognita,我会推荐The JFreeChart Developer Guide?.
?Disclaimer: Not affiliated with Object Refinery Limited; just a satisfied customer and very minor contributor.
? 免责声明:不隶属于 Object Refinery Limited;只是一个满意的客户和非常小的贡献者。
回答by Eric Savoie
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.
这似乎给了我一个很好的烛台图表,它充分利用了 Y 轴范围。希望这可以帮助。