barchart issue(?) when all values are 0

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
cee_time
Posts: 8
Joined: Thu Oct 06, 2016 10:23 pm
antibot: No, of course not.

barchart issue(?) when all values are 0

Post by cee_time » Wed Oct 19, 2016 2:35 pm

Hi:

i´m trying to implement a barchart that compares two values, in some cases those two values are 0 . So in this particular case paint two lines over de y axis(sorry my bad english), and not present the 0 values over de x axis. the next image shows better my problem.

Image

the code to plot the chart is

Code: Select all

Utils utilitarios = new Utils();

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        String serie = "";
        String mes = "";
        String mes1 = "kWh";
        if (tipo == 0) {
            dataset.addValue(suministro.getMesAnioPasado(), "", "");
            dataset.addValue(suministro.getMesActual(), "", "kWh");
        } else {
            dataset.addValue(suministro.getMesAnterior(), "", "");
            dataset.addValue(suministro.getMesActual(), "", "kWh");
        }
        JFreeChart chart = ChartFactory.createBarChart("", "", "", dataset, PlotOrientation.VERTICAL, false, true, false);

        chart.setBackgroundPaint(Color.white);

        StandardChartTheme chartTheme = (StandardChartTheme)StandardChartTheme.createJFreeTheme();
        InputStream is = GeneraPDF.class.getResourceAsStream("/resources/fonts/arial-narrow.ttf");
        java.awt.Font arialN = java.awt.Font.createFont(0, is);
        java.awt.Font arialNBase1 = arialN.deriveFont(6.5F);
        Map<TextAttribute, Object> attributes = new HashMap();
        attributes.put(TextAttribute.TRACKING, 0.07D);
        java.awt.Font arialNBase = arialNBase1.deriveFont(attributes);
        chartTheme.setExtraLargeFont(arialNBase);
        chartTheme.setLargeFont(arialNBase);
        chartTheme.setRegularFont(arialNBase);
        chartTheme.setSmallFont(arialNBase);

        chartTheme.apply(chart);

        CategoryPlot plot = chart.getCategoryPlot();
        if (tipo == 0) {
            plot.setRenderer(new AnioPasBarRenderer());
        } else {
            plot.setRenderer(new AntPasBarRenderer());
        }
        plot.setBackgroundPaint(Color.white);
        plot.setDomainGridlinePaint(Color.white);

        plot.setRangeGridlinePaint(Color.black);
        plot.setRangeGridlinesVisible(false);

        CategoryAxis dAxis = plot.getDomainAxis();
        ValueAxis rAxis = plot.getRangeAxis();
        dAxis.setTickLabelFont(arialNBase);
        rAxis.setTickLabelFont(arialNBase);

        rAxis.setTickMarksVisible(false);
        dAxis.setTickMarksVisible(false);
        rAxis.setTickLabelsVisible(false);
        rAxis.setVisible(false);
        if(tipo==0){
            if(suministro.getMesActual() != 0 || suministro.getMesAnioPasado() != 0){
                rAxis.setUpperMargin(0.25D);
            }
        } else {
            if(suministro.getMesActual() != 0 || suministro.getMesAnterior()!= 0){
                rAxis.setUpperMargin(0.25D);
            }
        }
        rAxis.setLowerMargin(0);
        
        NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(false);
        rangeAxis.setRangeWithMargins(rangeAxis.getLowerBound()-1,rangeAxis.getUpperBound()+1);
        BarRenderer renderer = (BarRenderer)plot.getRenderer();
        renderer.setDrawBarOutline(Boolean.FALSE.booleanValue());

        renderer.setMaximumBarWidth(0.3D);
        renderer.setItemMargin(0.0D);
        renderer.setShadowVisible(false);

        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(Boolean.TRUE);
        renderer.setBaseItemLabelFont(arialNBase);

        renderer.setGradientPaintTransformer(null);
        renderer.setBarPainter(new StandardBarPainter());
        Paint gris = new Color(120, 120, 121);
        renderer.setSeriesPaint(0, Color.black);

        renderer.setBaseItemLabelPaint(Color.black);

        plot.setOutlineVisible(false);
        plot.setAxisOffset(RectangleInsets.ZERO_INSETS);

        int width = 65;
        int height = 67;

        PdfContentByte Add_Chart_Content = writer.getDirectContent();

        PdfTemplate template_Chart_Holder = Add_Chart_Content.createTemplate(width, height);

        Graphics2D Graphics_Chart = template_Chart_Holder.createGraphics(width, height, new DefaultFontMapper());

        Rectangle2D Chart_Region = new Rectangle2D.Double(0.0D, 0.0D, 65.0D, 67.0D);

        chart.draw(Graphics_Chart, Chart_Region);

        Graphics_Chart.dispose();

        PdfContentByte textosVariables = writer.getDirectContent();
        ColumnText ct2 = new ColumnText(textosVariables);

        ct2.setUseAscender(true);
        fuentes.getArialN().setSize(10.0F);
        fuentes.getArialN().setColor(BaseColor.BLACK);
        Paragraph p2 = new Paragraph(" loren ipsun blablablabla ", fuentes.getArialN());
        fuentes.getArialNB();
        fuentes.getArialNB().setColor(BaseColor.BLACK);
        fuentes.getArialNB().setSize(10.0F);
        if (tipo == 0) {
            Add_Chart_Content.addTemplate(template_Chart_Holder, 410.17322F, 96.0F);
        } else {
            Add_Chart_Content.addTemplate(template_Chart_Holder, 593.1732F, 96.0F);
        }
        if (tipo == 0) {
            p2.add(new Chunk(" loren ipsun blablablabla"));
            p2.add(new Chunk("  loren ipsun blablablabla", fuentes.getArialN()));
            ct2.setSimpleColumn(475.17322F, 97.56378F, 585.95746F, 150.56378F);
        } else {
            p2.add(new Chunk(" loren ipsun blablablabla"));
            p2.add(new Chunk(" loren ipsun blablablabla.", fuentes.getArialN()));
            ct2.setSimpleColumn(658.1732F, 97.56378F, 774.95746F, 150.56378F);
        }
        ct2.setLeading(10.0F);
        ct2.addText(p2);
        ct2.go();
if one of the two values are not 0 or two values are not 0, the graph display ok.

Thanks in advance...

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: barchart issue(?) when all values are 0

Post by paradoxoff » Wed Oct 19, 2016 3:35 pm

Do you want to keep the item label from showing up if the value is 0?

cee_time
Posts: 8
Joined: Thu Oct 06, 2016 10:23 pm
antibot: No, of course not.

Re: barchart issue(?) when all values are 0

Post by cee_time » Wed Oct 19, 2016 4:02 pm

yes.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: barchart issue(?) when all values are 0

Post by paradoxoff » Wed Oct 19, 2016 5:59 pm

After scrolling through a long passage of code that had apparently nothing to do with your problem and that used several classes that are not part of the standard JFreeChart package, I found that you are using a normal StandardCategoryItemLabelGenerator. This will display the value for the given row/category combination, regardless of the absolute value.
Create your own StandardCategoryItemLabelGenerator and override generateLabel(CategoryDataset dataset, int row, int column). In your overriden method, get the value from the dataset. If it is zero, retun null, otherwise return super.generateLabel(CategoryDataset dataset, int row, int column).

Locked