Java 在Jfreechart中更改图形的X轴起始值的步骤

Java 在Jfreechart中更改图形的X轴起始值的步骤,java,jfreechart,Java,Jfreechart,我正在计算图像红色分量的直方图,并将其存储在redhisto[]中。数组的索引表示强度(0到255) 该值表示具有该强度的像素数。然后用JFreeChart绘制这些值 我的问题是: 如何使X轴值从0开始。现在它从负数开始 我们能改变图表中条形图的颜色吗 代码是: public class Histogram extends ApplicationFrame { public Histogram(final String title) throws IOException { su

我正在计算图像红色分量的直方图,并将其存储在redhisto[]中。数组的索引表示强度(0到255) 该值表示具有该强度的像素数。然后用JFreeChart绘制这些值

我的问题是:

  • 如何使X轴值从0开始。现在它从负数开始
  • 我们能改变图表中条形图的颜色吗 代码是:

     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);
     }
    }
    
    公共类直方图扩展了ApplicationFrame{
    公共直方图(最终字符串标题)引发IOException{
    超级(标题);
    IntervalXYDataset数据集=createDataset();
    JFreeChart chart=createChart(数据集);
    最终图表面板图表面板=新图表面板(图表);
    setPreferredSize(新java.awt.Dimension(500270));
    setContentPane(图表面板);
    }
    私有IntervalXYDataset createDataset()引发IOException{
    BuffereImage imageA=ImageIO.read(新文件(“XYZ.bmp”);
    int[]red=新int[imageA.getHeight()*imageA.getWidth()];
    int[]redhisto=新int[256];
    int[]像素;
    int k=0;
    对于(int y=0;y对于(int x=0;x您可以更改域轴的下限,并按如下所示设置系列绘制。默认的
    XYBarPainter
    具有渐变色高亮显示,因此我使用了
    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);
    

        XYPlot plot = (XYPlot) chart.getPlot();  
    
        //To change the lower bound of X-axis
        NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
        xAxis.setLowerBound(0);
    
        //To change the lower bound of Y-axis       
        NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
        yAxis.setLowerBound(0);
    
        // To change the color
        XYItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.green);