使用Jfreechart更改条形图中值轴的起始值 [英] Change the starting value of value axis in Bar chart with Jfreechart
问题描述
我的Java Web应用程序的BAR图表中显示了以下值. 9.46373791E8 9.45942547E8 9.45559945E8 9.45187023E8 9.44856693E8 9.44417826E8 9.44007878E8
I have the following values to be shown in a BAR chart in my Java web application. 9.46373791E8 9.45942547E8 9.45559945E8 9.45187023E8 9.44856693E8 9.44417826E8 9.44007878E8
如您所见,这些值实际上很接近,并且存在细微的差异.当我使用Jfreechart生成条形图时,所有条形都显示几乎相同的高度,并且无法从视觉上分辨出差异.所以我想将(0,0)更改为(0,9),以使x轴在y轴上的数字为9.我仍然想显示条形图在条形图顶部某处所代表的真实值.
As u can see the values are really close and have minor differences. When i generate a bar chart using Jfreechart, all the bars appear almost the same height and there is no way of telling the difference visually. So i want to change the (0,0) to (0,9)so that x axis is at number 9 on y axis. I still want to show the real values that the bars represent somewhere on top of the bar.
请提出建议.我尝试了以下内容,但没有成功
please suggest ideas. i tried the below but it didnt work
Double d=(double) 9;
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLowerMargin(d);
这是我的原始程序供您参考
This is my original program for your reference
JFreeChart chart =
ChartFactory.createBarChart(
title,
"File Date",
"File Size",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false);
chart.setBackgroundPaint(Color.white);
// Set the background color of the chart
chart.getTitle().setPaint(Color.DARK_GRAY);
chart.setBorderVisible(true);
// Adjust the color of the title
CategoryPlot plot = chart.getCategoryPlot();
plot.getRangeAxis().setLowerBound(d);
// Get the Plot object for a bar graph
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.blue);
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setSeriesPaint(0, Color.decode("#00008B"));
推荐答案
我想您想执行以下操作:
I guess you want to do the following:
barChart.getCategoryPlot().getRangeAxis().setLowerBound(9.0);
barChart是您的JFreeChart Object
.
where barChart is you JFreeChart Object
.
但是由于您的值高于9.0E8(超过9000),因此您不应该将下限设置为9.0E8而不是9.0,因为在9.0E8时0和9之间的差异不大超越.
But since yours values are above 9.0E8 ( IT'S OVER 9000), shouldn't you put the lower bound at 9.0E8 instead of 9.0 since at there is not so much difference between 0 and 9 when you are at 9.0E8 and beyond.
我已经测试了您的代码,并且可以在Windows Vista下的计算机上正常工作...
I've tested your code and it's working on my computer under Windows Vista...
我的完整代码在这里:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class ChartTester extends JFrame {
private static final long serialVersionUID = 1L;
public ChartTester(final String title) {
super(title);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Returns a sample dataset.
* @return The dataset.
*/
private CategoryDataset createDataset() {
final String rowName = "Row";
final String[] columnName = { "Column1","Column2","Column3","Column4","Column5"};
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(9.2, rowName, columnName[0]);
dataset.addValue(9.3, rowName, columnName[1]);
dataset.addValue(9.4, rowName, columnName[2]);
dataset.addValue(9.5, rowName, columnName[3]);
dataset.addValue(10.0, rowName, columnName[4]);
return dataset;
}
/**
* Creates a sample chart.
* @param dataset the dataset.
* @return The chart.
*/
private JFreeChart createChart(final CategoryDataset dataset) {
double d =9.0;
final JFreeChart chart =
ChartFactory.createBarChart(
"Chart Title",
"X Axis",
"Y Axis",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false);
chart.setBackgroundPaint(Color.white);
// Set the background color of the chart
chart.getTitle().setPaint(Color.DARK_GRAY);
chart.setBorderVisible(true);
// Adjust the color of the title
CategoryPlot plot = chart.getCategoryPlot();
plot.getRangeAxis().setLowerBound(d);
// Get the Plot object for a bar graph
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.blue);
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setSeriesPaint(0, Color.decode("#00008B"));
return chart;
}
public static void main(final String[] args) {
final ChartTester test = new ChartTester("Test");
test.pack();
test.setVisible(true);
}
}
这篇关于使用Jfreechart更改条形图中值轴的起始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!