在Jfreechart中更改图形的X轴起始值 [英] To change the X-axis starting value of graph in Jfreechart
问题描述
我正在计算图像红色成分的直方图,并将其存储在redhisto []中。数组的索引表示强度(0到255)
,值表示具有该强度的像素数。然后用JFreeChart绘制这些值。
I am calculating histogram of red component of the image and stored it in redhisto[]. The index of the array represent the intensity(0 to 255) and the value represent the number of pixel with that intensity. Then plotting those values with JFreeChart.
我的问题是:
- 如何使X轴值从0开始。现在它从负数开始。
-
我们可以更改图形中条形的颜色
代码为:
- How to make X-axis value start from 0. Now its starting from negative number.
Can we change the color of the bars in the graph code is :
public class Histogram extends ApplicationFrame {
public Histogram(final String title) throws IOException {
super(title);
IntervalXYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private IntervalXYDataset createDataset() throws IOException {
BufferedImage imageA = ImageIO.read(new File("XYZ.bmp"));
int[] red = new int[imageA.getHeight()*imageA.getWidth()];
int[] redhisto = new int[256];
int[] pixel;
int k= 0;
for (int y = 0; y < imageA.getHeight(); y++) {
for (int x = 0; x < imageA.getWidth(); x++) {
pixel = imageA.getRaster().getPixel(x, y, new int[3]);
red[k] = pixel[0];
k++;
}
}
for(int x=0;x<red.length;x++){
int y = red[x];
redhisto[y]++;
}
final XYSeries series = new XYSeries("No of pixels");
for(int i=0; i<redhisto.length;i++)
series.add(i,redhisto[i]);
final XYSeriesCollection dataset = new XYSeriesCollection(series);
return dataset;
}
private JFreeChart createChart(IntervalXYDataset dataset) {
final JFreeChart chart = ChartFactory.createXYBarChart("Color Intensity Histogram","X",false,"Y",dataset,PlotOrientation.VERTICAL,true,true,false);
XYPlot plot = (XYPlot) chart.getPlot();
return chart;
}
public static void main(final String[] args) throws IOException {
final Histogram demo = new Histogram("Image Histogram");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
推荐答案
您可以更改域轴的下限并设置系列绘制,如下所示。默认的 XYBarPainter
有一个渐变颜色突出显示,所以我使用了 StandardXYBarPainter
。
You can change the lower bound of the domain axis and set the series paint as shown below. The default XYBarPainter
has a gradient color highlight, so I used a StandardXYBarPainter
.
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setLowerBound(0);
XYBarRenderer r = (XYBarRenderer) plot.getRenderer();
r.setBarPainter(new StandardXYBarPainter());
r.setSeriesPaint(0, Color.blue);
这篇关于在Jfreechart中更改图形的X轴起始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!