Example usage for org.jfree.chart.entity ChartEntity getShapeType

List of usage examples for org.jfree.chart.entity ChartEntity getShapeType

Introduction

In this page you can find the example usage for org.jfree.chart.entity ChartEntity getShapeType.

Prototype

public String getShapeType() 

Source Link

Document

Returns a string describing the entity area.

Usage

From source file:com.igalia.java.zk.components.JFreeChartEngine.java

public byte[] drawChart(Object data) {
    Chart chart = (Chart) data;/*from ww  w.  j  a va  2 s  .c om*/
    ChartImpl impl = getChartImpl(chart);
    JFreeChart jfchart = impl.createChart(chart);

    Plot plot = (Plot) jfchart.getPlot();
    float alpha = (float) (((float) chart.getFgAlpha()) / 255);
    plot.setForegroundAlpha(alpha);

    alpha = (float) (((float) chart.getBgAlpha()) / 255);
    plot.setBackgroundAlpha(alpha);

    int[] bgRGB = chart.getBgRGB();
    if (bgRGB != null) {
        plot.setBackgroundPaint(new Color(bgRGB[0], bgRGB[1], bgRGB[2], chart.getBgAlpha()));
    }

    int[] paneRGB = chart.getPaneRGB();
    if (paneRGB != null) {
        jfchart.setBackgroundPaint(new Color(paneRGB[0], paneRGB[1], paneRGB[2], chart.getPaneAlpha()));
    }

    //since 3.6.3, JFreeChart 1.0.13 change default fonts which does not support Chinese, allow
    //developer to set font.

    //title font
    final Font tfont = chart.getTitleFont();
    if (tfont != null) {
        jfchart.getTitle().setFont(tfont);
    }

    //legend font
    final Font lfont = chart.getLegendFont();
    if (lfont != null) {
        jfchart.getLegend().setItemFont(lfont);
    }

    if (plot instanceof CategoryPlot) {
        final CategoryPlot cplot = (CategoryPlot) plot;
        cplot.setRangeGridlinePaint(new Color(0xc0, 0xc0, 0xc0));

        //Domain axis(x axis)
        final Font xlbfont = chart.getXAxisFont();
        final Font xtkfont = chart.getXAxisTickFont();
        if (xlbfont != null) {
            cplot.getDomainAxis().setLabelFont(xlbfont);
        }
        if (xtkfont != null) {
            cplot.getDomainAxis().setTickLabelFont(xtkfont);
        }

        Color[] colorMappings = (Color[]) chart.getAttribute("series-color-mappings");
        if (colorMappings != null) {
            for (int ii = 0; ii < colorMappings.length; ii++) {
                cplot.getRenderer().setSeriesPaint(ii, colorMappings[ii]);
            }
        }

        Double lowerBound = (Double) chart.getAttribute("range-axis-lower-bound");
        if (lowerBound != null) {
            cplot.getRangeAxis().setAutoRange(false);
            cplot.getRangeAxis().setLowerBound(lowerBound);
        }

        Double upperBound = (Double) chart.getAttribute("range-axis-upper-bound");
        if (upperBound != null) {
            cplot.getRangeAxis().setAutoRange(false);
            cplot.getRangeAxis().setUpperBound(upperBound);
        }

        //Range axis(y axis)
        final Font ylbfont = chart.getYAxisFont();
        final Font ytkfont = chart.getYAxisTickFont();
        if (ylbfont != null) {
            cplot.getRangeAxis().setLabelFont(ylbfont);
        }
        if (ytkfont != null) {
            cplot.getRangeAxis().setTickLabelFont(ytkfont);
        }
    } else if (plot instanceof XYPlot) {
        final XYPlot xyplot = (XYPlot) plot;
        xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);

        //Domain axis(x axis)
        final Font xlbfont = chart.getXAxisFont();
        final Font xtkfont = chart.getXAxisTickFont();
        if (xlbfont != null) {
            xyplot.getDomainAxis().setLabelFont(xlbfont);
        }
        if (xtkfont != null) {
            xyplot.getDomainAxis().setTickLabelFont(xtkfont);
        }

        //Range axis(y axis)
        final Font ylbfont = chart.getYAxisFont();
        final Font ytkfont = chart.getYAxisTickFont();
        if (ylbfont != null) {
            xyplot.getRangeAxis().setLabelFont(ylbfont);
        }
        if (ytkfont != null) {
            xyplot.getRangeAxis().setTickLabelFont(ytkfont);
        }
    } else if (plot instanceof PiePlot) {
        plot.setOutlineStroke(null);
    }

    //callbacks for each area
    ChartRenderingInfo jfinfo = new ChartRenderingInfo();
    BufferedImage bi = jfchart.createBufferedImage(chart.getIntWidth(), chart.getIntHeight(),
            Transparency.TRANSLUCENT, jfinfo);

    //remove old areas
    if (chart.getChildren().size() > 20)
        chart.invalidate(); //improve performance if too many chart
    chart.getChildren().clear();

    if (Events.isListened(chart, Events.ON_CLICK, false) || chart.isShowTooltiptext()) {
        int j = 0;
        String preUrl = null;
        for (Iterator it = jfinfo.getEntityCollection().iterator(); it.hasNext();) {
            ChartEntity ce = (ChartEntity) it.next();
            final String url = ce.getURLText();

            //workaround JFreeChart's bug (skip replicate areas)
            if (url != null) {
                if (preUrl == null) {
                    preUrl = url;
                } else if (url.equals(preUrl)) { //start replicate, skip
                    break;
                }
            }

            //1. JFreeChartEntity area cover the whole chart, will "mask" other areas
            //2. LegendTitle area cover the whole legend, will "mask" each legend
            //3. PlotEntity cover the whole chart plotting araa, will "mask" each bar/line/area
            if (!(ce instanceof JFreeChartEntity)
                    && !(ce instanceof TitleEntity && ((TitleEntity) ce).getTitle() instanceof LegendTitle)
                    && !(ce instanceof PlotEntity)) {
                Area area = new Area();
                area.setParent(chart);
                area.setCoords(ce.getShapeCoords());
                area.setShape(ce.getShapeType());
                area.setId("area_" + chart.getId() + '_' + (j++));
                if (chart.isShowTooltiptext() && ce.getToolTipText() != null) {
                    area.setTooltiptext(ce.getToolTipText());
                }
                area.setAttribute("url", ce.getURLText());
                impl.render(chart, area, ce);
                if (chart.getAreaListener() != null) {
                    try {
                        chart.getAreaListener().onRender(area, ce);
                    } catch (Exception ex) {
                        throw UiException.Aide.wrap(ex);
                    }
                }
            }
        }
    }
    //clean up the "LEGEND_SEQ"
    //used for workaround LegendItemEntity.getSeries() always return 0
    //used for workaround TickLabelEntity no information
    chart.removeAttribute("LEGEND_SEQ");
    chart.removeAttribute("TICK_SEQ");

    try {
        //encode into png image format byte array
        return EncoderUtil.encode(bi, ImageFormat.PNG, true);
    } catch (java.io.IOException ex) {
        throw UiException.Aide.wrap(ex);
    }
}

From source file:org.adempiere.webui.editor.WChartEditor.java

private void render(JFreeChart chart) {
    ChartRenderingInfo info = new ChartRenderingInfo();
    int width = 400;
    int height = chartModel.getWinHeight();
    BufferedImage bi = chart.createBufferedImage(width, height, BufferedImage.TRANSLUCENT, info);
    try {//from ww w .  j  a  va 2  s .  com
        byte[] bytes = EncoderUtil.encode(bi, ImageFormat.PNG, true);

        AImage image = new AImage("", bytes);
        Imagemap myImage = new Imagemap();

        Panel panel = getComponent();
        myImage.setContent(image);
        if (panel.getPanelchildren() != null) {
            panel.getPanelchildren().getChildren().clear();
            panel.getPanelchildren().appendChild(myImage);
        } else {
            Panelchildren pc = new Panelchildren();
            panel.appendChild(pc);
            pc.appendChild(myImage);
        }

        int count = 0;
        for (Iterator<?> it = info.getEntityCollection().getEntities().iterator(); it.hasNext();) {
            ChartEntity entity = (ChartEntity) it.next();

            String key = null;
            String seriesName = null;
            if (entity instanceof CategoryItemEntity) {
                CategoryItemEntity item = ((CategoryItemEntity) entity);
                Comparable<?> colKey = item.getColumnKey();
                Comparable<?> rowKey = item.getRowKey();
                if (colKey != null && rowKey != null) {
                    key = colKey.toString();
                    seriesName = rowKey.toString();
                }
            } else if (entity instanceof PieSectionEntity) {
                Comparable<?> sectionKey = ((PieSectionEntity) entity).getSectionKey();
                if (sectionKey != null) {
                    key = sectionKey.toString();
                }
            }
            if (entity instanceof XYItemEntity) {
                XYItemEntity item = ((XYItemEntity) entity);
                if (item.getDataset() instanceof TimeSeriesCollection) {
                    TimeSeriesCollection data = (TimeSeriesCollection) item.getDataset();
                    TimeSeries series = data.getSeries(item.getSeriesIndex());
                    TimeSeriesDataItem dataitem = series.getDataItem(item.getItem());
                    seriesName = series.getKey().toString();
                    key = dataitem.getPeriod().toString();
                }
            }

            if (key == null)
                continue;

            Area area = new Area();
            myImage.appendChild(area);
            area.setCoords(entity.getShapeCoords());
            area.setShape(entity.getShapeType());
            area.setTooltiptext(entity.getToolTipText());
            area.setId(count + "_WG__" + seriesName + "__" + key);
            count++;
        }

        myImage.addEventListener(Events.ON_CLICK, new EventListener() {
            public void onEvent(Event event) throws Exception {
                MouseEvent me = (MouseEvent) event;
                String areaId = me.getArea();
                if (areaId != null) {
                    String[] strs = areaId.split("__");
                    if (strs.length == 3) {
                        chartMouseClicked(strs[2], strs[1]);
                    }
                }
            }
        });
    } catch (Exception e) {
        log.log(Level.SEVERE, "", e);
    }

}

From source file:org.openfaces.component.chart.impl.helpers.MapRenderUtilities.java

private static String getImageMapAreaTag(Chart chart, ChartView view,
        ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
        URLTagFragmentGenerator urlTagFragmentGenerator, ChartEntity entity, int entityIndex) {
    StringBuilder tag = new StringBuilder();
    boolean hasURL = entity.getURLText() != null && !entity.getURLText().equals("");
    boolean hasAction = viewHasAction(view);
    boolean hasPopup = viewHasPopup(view);
    boolean hasToolTip = entity.getToolTipText() != null && !entity.getToolTipText().equals("");
    boolean hasSelection = chart.getChartSelection() != null;

    String onClick = getOnClick(chart, view, entity);
    String onMouseOut = getOnMouseOut(chart, view, entity);
    String onMouseOver = getOnMouseOver(chart, view, entity);

    boolean hasCustomOnMouseOver = onMouseOver != null && !onMouseOver.equals("");
    boolean hasCustomOnMouseOut = onMouseOut != null && !onMouseOut.equals("");
    boolean hasCustomClick = onClick != null && !onClick.equals("");

    if (hasURL || hasToolTip || hasAction || hasPopup || hasCustomClick || hasSelection) {
        final FacesContext context = FacesContext.getCurrentInstance();
        tag.append("<area");

        if (hasAction || hasCustomClick || hasSelection) {
            if (hasSelection && chart.getChartMenu() != null && Environment.isChrome()) {
                tag.append(" oncontextmenu=\"");
                tag.append("O$('").append(chart.getClientId(context))
                        .append("')._areaContextMenuClick(event, '")
                        .append(chart.getChartMenu().getClientId(context)).append("');");
                tag.append("\"");
            }/*from www. ja va2  s  . c o  m*/
            tag.append(" onclick=\"O$('").append(chart.getClientId(context)).append("')._clickItem(event, '")
                    .append(entityIndex).append("'");
            tag.append(",");
            if (hasCustomClick) {
                tag.append("function(event){").append(onClick).append("}");
            } else {
                tag.append("null");
            }
            tag.append(",").append(hasAction);
            tag.append(",").append(hasSelection);

            tag.append(")\"");
        }

        if (hasCustomOnMouseOver || hasPopup) {
            StringBuilder mouseOverBuilder = new StringBuilder();
            if (hasPopup) {
                final Integer oldEntityIndex = chart.getEntityIndex();
                chart.setEntityIndex(entityIndex);
                mouseOverBuilder.append("O$.ChartPopup._show(event,");
                mouseOverBuilder.append("'");
                mouseOverBuilder.append(chart.getChartView().getChartPopup().getClientId(context));
                mouseOverBuilder.append("',");
                mouseOverBuilder.append("'");
                mouseOverBuilder.append(entityIndex);
                mouseOverBuilder.append("'");
                chart.setEntityIndex(oldEntityIndex);

                if (hasCustomOnMouseOver) {
                    mouseOverBuilder.append(", function(){");
                    mouseOverBuilder.append(onMouseOver);
                    mouseOverBuilder.append("}");
                }
            } else {
                mouseOverBuilder.append(onMouseOver);
            }
            mouseOverBuilder.append(");");

            tag.append(" onmouseover=\"").append(mouseOverBuilder.toString()).append("\"");
        }

        if (hasCustomOnMouseOut) {
            tag.append(" onmouseout=\"").append(onMouseOut).append("\"");
        }

        tag.append(" shape=\"").append(entity.getShapeType()).append("\"" + " coords=\"")
                .append(entity.getShapeCoords()).append("\"");

        if (hasToolTip)
            tag.append(toolTipTagFragmentGenerator.generateToolTipFragment(entity.getToolTipText()));

        if (hasURL && !hasAction)
            tag.append(urlTagFragmentGenerator.generateURLFragment(entity.getURLText()));

        if (!hasToolTip)
            tag.append(" alt=\"\"");

        tag.append("/>");
    }
    return tag.toString();
}