【问题标题】:To change the X-axis starting value of graph in Jfreechart在 Jfreechart 中更改图形的 X 轴起始值
【发布时间】:2014-01-12 09:00:40
【问题描述】:

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

我的问题是:

  1. 如何使 X 轴值从 0 开始。现在它从负数开始。
  2. 我们可以更改图表中条形的颜色吗 代码是:

     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);
     }
    }
    

【问题讨论】:

    标签: java jfreechart


    【解决方案1】:

    您可以更改域轴的下限并设置系列绘制,如下所示。默认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);
    

    【讨论】:

      【解决方案2】:
          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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        相关资源
        最近更新 更多