Stacked chart to display stroke along with value

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Amit9344
Posts: 6
Joined: Fri Apr 22, 2016 7:06 pm
antibot: No, of course not.

Stacked chart to display stroke along with value

Post by Amit9344 » Fri May 20, 2016 4:05 pm

Hi

How to implement a stacked chart with rectangular marker along with value.
as shown in the below image.Thanks in advance

Image

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

Re: Stacked chart to display stroke along with value

Post by paradoxoff » Fri May 20, 2016 9:27 pm

Custom CategoryAnnotation.

Amit9344
Posts: 6
Joined: Fri Apr 22, 2016 7:06 pm
antibot: No, of course not.

Re: Stacked chart to display stroke along with value

Post by Amit9344 » Mon May 23, 2016 1:19 pm

Thanks for reply.Could you please provide snippet of code for better understanding because I'm new to jfree chart.
Thanks in advance

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

Re: Stacked chart to display stroke along with value

Post by paradoxoff » Mon May 23, 2016 9:14 pm

Code: Select all

public class CategoryAnnotationDemo extends ApplicationFrame {

    public CategoryAnnotationDemo(String title) {
        super(title);

        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 350));
        setContentPane(chartPanel);
    }

    private CategoryDataset createDataset() {
        double[][] data = new double[][]{
            {10, 8},
            {8, 4},
            {12, 6},
            {6, 9},
            {9, 12}};
        return DatasetUtilities.createCategoryDataset(
                "Row ", "Column ", data);
    }

    private JFreeChart createChart(final CategoryDataset dataset) {
        StackedBarRenderer br = new StackedBarRenderer();
        CategoryPlot cp = new CategoryPlot(dataset, new CategoryAxis("x"), new NumberAxis("x"), br);
        cp.setOrientation(PlotOrientation.HORIZONTAL);
        cp.addAnnotation(new CategoryLevelAnnotation("Column 1", 5.0));
        return new JFreeChart(cp);
    }

    public static void main(final String[] args) {

        final CategoryAnnotationDemo demo
                = new CategoryAnnotationDemo("Bar Chart");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

    static class CategoryLevelAnnotation extends AbstractAnnotation implements CategoryAnnotation {

        private Comparable category;

        private double value;

        private int datasetIndex;

        private Stroke stroke = new BasicStroke(3.0f);

        private boolean visible = true;

        public CategoryLevelAnnotation(Comparable key, double value) {
            this.category = key;
            this.value = value;
        }

        public int getDatasetIndex() {
            return 0;
        }

        public void setDatasetIndex(int index) {
            this.datasetIndex = index;
            super.fireAnnotationChanged();
        }

        public boolean isVisible() {
            return true;
        }

        public void setVisible(boolean b) {
        }

        public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea, CategoryAxis domainAxis, ValueAxis rangeAxis) {
            if (!visible) {
                return;
            }
            CategoryDataset dataset = plot.getDataset(this.datasetIndex);
            int catIndex = dataset.getColumnIndex(this.category);
            int catCount = dataset.getColumnCount();

            double lineX1 = 0.0f;
            double lineY1 = 0.0f;
            double lineX2 = 0.0f;
            double lineY2 = 0.0f;
            double textX = 0.0f;
            double textY = 0.0f;

            PlotOrientation orientation = plot.getOrientation();
            RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
                    plot.getDomainAxisLocation(), orientation);
            RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
                    plot.getRangeAxisLocation(), orientation);

            if (orientation == PlotOrientation.HORIZONTAL) {
                lineY1 = domainAxis.getCategoryJava2DCoordinate(
                        CategoryAnchor.START, catIndex, catCount, dataArea,
                        domainEdge);
                lineX1 = rangeAxis.valueToJava2D(this.value, dataArea, rangeEdge);
                lineY2 = domainAxis.getCategoryJava2DCoordinate(
                        CategoryAnchor.END, catIndex, catCount, dataArea,
                        domainEdge);
                lineX2 = rangeAxis.valueToJava2D(this.value, dataArea, rangeEdge);
                textX = lineX1;
                textY = (lineY1 + lineY2) / 2.0;
            } else if (orientation == PlotOrientation.VERTICAL) {
                lineX1 = domainAxis.getCategoryJava2DCoordinate(
                        CategoryAnchor.START, catIndex, catCount, dataArea,
                        domainEdge);
                lineY1 = rangeAxis.valueToJava2D(this.value, dataArea, rangeEdge);
                lineX2 = domainAxis.getCategoryJava2DCoordinate(
                        CategoryAnchor.END, catIndex, catCount, dataArea,
                        domainEdge);
                lineY2 = rangeAxis.valueToJava2D(this.value, dataArea, rangeEdge);
                textY = lineY1;
                textX = (lineX1 + lineX2) / 2.0;
            }
            g2.setPaint(Color.ORANGE);
            g2.setStroke(this.stroke);
            g2.drawLine((int) lineX1, (int) lineY1, (int) lineX2, (int) lineY2);
            g2.setPaint(Color.BLACK);
            TextUtilities.drawAlignedString(String.valueOf(value), g2, (float) textX, (float) textY, TextAnchor.CENTER_LEFT);

        }
    }
}

Locked