简体   繁体   中英

how to set first and last tick mark and x axis value in jfree bar chart x-axis

im using jfree chart in my application. For my application i need to mark only first and last value of the x axis and also tick mark. I've tried

String Male1 = "First";
        String Male2 = "sec";
        String Female1 = "0-4";
        String Female2 = "5-18";
        String Female3 = "19-45";
        String Female4 = "46-64";
        String Female5 = "65+";
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(202, Male1, Female1);
        dataset.addValue(130, Male2, Female1);
        dataset.addValue(216, Male1, Female2);
        dataset.addValue(0, Male2, Female2);
        dataset.addValue(248, Male1, Female3);
        dataset.addValue(458, Male2, Female3);
        dataset.addValue(517, Male1, Female4);
        dataset.addValue(623, Male2, Female4);
        dataset.addValue(1481, Male1, Female5);
        dataset.addValue(680, Male2, Female5);        
        final JFreeChart chart = ChartFactory.createBarChart(
                "", "", "", dataset,
                PlotOrientation.HORIZONTAL, true, true, false);
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setOutlineVisible(false);
        plot.setRangeGridlinesVisible(false);
        plot.setRangeGridlineStroke(new BasicStroke(0.2f));
        plot.setAxisOffset(RectangleInsets.ZERO_INSETS);        

        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAxisLinePaint(Color.decode("#C1C1C1"));
        rangeAxis.setAxisLineVisible(true);
        rangeAxis.setTickMarksVisible(false);
        rangeAxis.setTickMarkOutsideLength(0f);

        final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
        categoryAxis.setTickMarksVisible(false);
        categoryAxis.setAxisLineVisible(false);
        BarRenderer br = new BarRenderer();
        br.setItemMargin(0.03);
        br.setShadowVisible(false);
        br.setBarPainter(new StandardBarPainter());
        br.setSeriesPaint(0, Color.decode("#999999"));
        br.setSeriesPaint(1, Color.decode("#CCCCCC"));        

        chart.getCategoryPlot().setRenderer(br);
        chart.removeLegend();
        try {
            ChartUtilities.saveChartAsPNG(new File("/media/hari/668ea9a3-d26c-4896-a2f0-756dfb532756/jfreeBarchart.png"), chart, 280, 180);
            System.out.println("=====Bar chart=====");
        } catch (Exception e) {
            e.printStackTrace();
        }

For the above code im getting

在此处输入图片说明

But my expectation is

在此处输入图片说明

Please help me to get the expected chart in jfree bar chart

If I understood correctly you want to only display two ticks (0 and 1500) and add a noticeable inside tick mark to both.

To display only two ticks you can call setTickUnit with the appropriate size argument. This size will be used by the axis object to generate the visible ticks.

To add tick marks on the inside you can call setTickMarkInsideLength .

So adding these three lines should do the trick:

rangeAxis.setTickUnit(new NumberTickUnit(1500));
rangeAxis.setTickMarksVisible(true); 
rangeAxis.setTickMarkInsideLength(3f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM