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..
Adding dot line in the chart
-
- Posts: 19
- Joined: Wed Mar 09, 2011 2:11 pm
- antibot: No, of course not.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: Adding dot line in the chart
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.
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.
-
- Posts: 19
- Joined: Wed Mar 09, 2011 2:11 pm
- antibot: No, of course not.
Re: Adding dot line in the chart
Please give any sample for that...
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: Adding dot line in the chart
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;
}
}
}
-
- Posts: 19
- Joined: Wed Mar 09, 2011 2:11 pm
- antibot: No, of course not.
Adding dot line in the grid of the chart
Thanks for the reply
I need the dot line for the grid not for the graph....
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));