Adding dot line in the chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
rvijaiganesh
Posts: 19
Joined: Wed Mar 09, 2011 2:11 pm
antibot: No, of course not.

Adding dot line in the chart

Post by rvijaiganesh » Fri Mar 18, 2011 7:25 pm

Hi,

Is it possible to add dot lines only for the selected values.For example the y-axis value is (0,25,50,100,125,150,175,200) .In that i want to add dot lines (with specific color) only the value containing 100 and 200.For other values it should be straight line..

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

Re: Adding dot line in the chart

Post by paradoxoff » Fri Mar 18, 2011 9:38 pm

Create a new renderer and override the getItemStroke(int row, int column) method.
if you want to select the stroke based on the dataset values and not on the series/item index, your new renderer must be able to access the dataset in the getItemStroke(int,int) method. To achieve that, you could also override the initialise() method and store a reference of the submitted XYDataset in your renderer.

rvijaiganesh
Posts: 19
Joined: Wed Mar 09, 2011 2:11 pm
antibot: No, of course not.

Re: Adding dot line in the chart

Post by rvijaiganesh » Sat Mar 19, 2011 7:10 am

Please give any sample for that...

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

Re: Adding dot line in the chart

Post by paradoxoff » Sat Mar 19, 2011 9:29 am

Here you go:

Code: Select all

public class XYItemStrokeDemo{
    public XYItemStrokeDemo() {
        JFrame frame = new JFrame("Item Stroke Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int rows = 4;
        int columns = 2;
        double[] xValues = {1, 2, 3, 4, 5, 6, 7, 8};
        double[] yValues = {20, 0, 18, 8, 11, 17, 14, 17};
        DefaultXYDataset dataset = new DefaultXYDataset();
        double[][] data = new double[2][];
        data[0] = xValues;
        data[1] = yValues;
        dataset.addSeries("Series 1", data);
        XYItemRenderer r = new XYItemStrokeRenderer();
		NumberAxis xAxis = new NumberAxis("x");
        NumberAxis yAxis = new NumberAxis("y");
        XYPlot plot = new XYPlot(dataset, xAxis, yAxis, r);
        JFreeChart chart = new JFreeChart("Item Stroke Demo", new Font("Tahoma",0,18), plot, false);
        ChartPanel cp = new ChartPanel(chart);
        frame.setContentPane(cp);
        frame.pack();
      	frame.setVisible(true);       
    }
    public static void main(String[] args){
        XYItemStrokeDemo demo = new XYItemStrokeDemo();
    }
    static class XYItemStrokeRenderer extends XYLineAndShapeRenderer{
        private Stroke highlightedStroke = new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[]{4, 4}, 1.0f);
        private XYDataset dataset;

        public XYItemRendererState initialise(Graphics2D g2,
            Rectangle2D dataArea,
            XYPlot plot,
            XYDataset data,
            PlotRenderingInfo info) {
                this.dataset = data;
                return super.initialise(g2, dataArea, plot, data, info);

        }
        public Stroke getItemStroke(int series, int item){
            Stroke result = super.getItemStroke(series, item);
            if(item > 0){
                double lastX = dataset.getXValue(series, item - 1);
                double currentX = dataset.getXValue(series, item);
                if(lastX == 4 && currentX == 5){
                    result = highlightedStroke;
                }
            }
            return result;
        }
    }
}

rvijaiganesh
Posts: 19
Joined: Wed Mar 09, 2011 2:11 pm
antibot: No, of course not.

Adding dot line in the grid of the chart

Post by rvijaiganesh » Mon Mar 21, 2011 7:26 am

Thanks for the reply

I need the dot line for the grid not for the graph....

Code: Select all

plot.setRangeGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        plot.setRangeGridlineStroke(new BasicStroke(1.0F));

Locked