Link to home
Start Free TrialLog in
Avatar of dshrenik
dshrenikFlag for United States of America

asked on

JFreeChart: CombinedDomainXYPlot

I have a CombinedDomainXYPlot with 3 subplots (XYPlot).

I am trying to make the base shape visible and fill it with this code, but it does not seem to work. If possible, please let me know what I am missing. Thanks!
XYItemRenderer r = plot.getRenderer();
        if (renderer instanceof XYLineAndShapeRenderer) {
        	XYLineAndShapeRenderer lineAndShapeRenderer = (XYLineAndShapeRenderer) r;
        	lineAndShapeRenderer.setBaseShapesVisible(true);
        	lineAndShapeRenderer.setBaseShapesFilled(true);
        }

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image

Strange that it does not work.
Maybe you could try to use ...ShapesVisible(..) and ...ShapesFilled(...)  methods specific to series ?
Avatar of dshrenik

ASKER

Can you tell me how I can do that?
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Even this code does not seem to work:
final XYItemRenderer renderer = plot.getRenderer();
		
		if (renderer instanceof StandardXYItemRenderer) {
			final StandardXYItemRenderer itemRenderer = (StandardXYItemRenderer) renderer;
			itemRenderer.setSeriesStroke(0, new BasicStroke(2.0f));
			itemRenderer.setSeriesStroke(1, new BasicStroke(2.0f));
		}
		
        if (renderer instanceof XYLineAndShapeRenderer) {
        	XYLineAndShapeRenderer lineAndShapeRenderer = (XYLineAndShapeRenderer) renderer;
        	//lineAndShapeRenderer.setBaseShapesVisible(true);
        	//lineAndShapeRenderer.setBaseShapesFilled(true);
        	lineAndShapeRenderer.setSeriesShapesVisible(0, true);
        	lineAndShapeRenderer.setSeriesShapesFilled(0, true);
        }

Open in new window

Maybe you can post your code - I could then try to play with it

CombinedDomainXYPlot mainPlot = new CombinedDomainXYPlot(new DateAxis(""));
		mainPlot.setGap(10.0);
		
		final TimeSeriesCollection dataset1 = new TimeSeriesCollection();
		final TimeSeriesCollection dataset2 = new TimeSeriesCollection();
		final TimeSeriesCollection dataset3 = new TimeSeriesCollection();
		final TimeSeries series1 = new TimeSeries("A");
		final TimeSeries series2 = new TimeSeries("B");
		final TimeSeries series3 = new TimeSeries("C");
		final TimeSeries series4 = new TimeSeries("D");
		
		Minute xValue;
		double yValue1, yValue2, yValue3, yValue4;
///		
// I add values to all 4 series here
///
		}

		dataset1.addSeries(series1);
		dataset2.addSeries(series2);
		dataset2.addSeries(series3);
		dataset3.addSeries(series4);

		XYItemRenderer renderer1 = new StandardXYItemRenderer();
		NumberAxis rangeAxis1 = new NumberAxis("");
		XYPlot subplot1 = new XYPlot(dataset1, null, rangeAxis1, renderer1);
		subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		
		XYItemRenderer renderer2 = new StandardXYItemRenderer();
		NumberAxis rangeAxis2 = new NumberAxis("");
		rangeAxis2.setAutoRangeIncludesZero(false);
		XYPlot subplot2 = new XYPlot(dataset2, null, rangeAxis2, renderer2);
		subplot2.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		
		XYItemRenderer renderer3 = new StandardXYItemRenderer();
		NumberAxis rangeAxis3 = new NumberAxis("");
		rangeAxis3.setAutoRangeIncludesZero(false);
		XYPlot subplot3 = new XYPlot(dataset3, null, rangeAxis3, renderer3);
		subplot3.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		
		mainPlot.add(subplot1, 1);
		mainPlot.add(subplot2, 1);
		mainPlot.add(subplot3, 1);
		mainPlot.setOrientation(PlotOrientation.VERTICAL);
		
		final JFreeChart chart = new JFreeChart(
				"",
				JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true
				);
		
		chart.setBackgroundPaint(Color.white);
		final XYPlot plot = chart.getXYPlot();
		//plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(false);
		
		setRenderer(subplot1);
		setRenderer(subplot2);
		setRenderer(subplot3);
		
        final DateAxis domainAxis = (DateAxis) mainPlot.getDomainAxis();
        domainAxis.setDateFormatOverride(new SimpleDateFormat("hh:mma"));

		ChartPanel chartPanel = new ChartPanel(chart);
		//chartPanel.setPreferredSize(new Dimension(785, 440));
		chartPanel.setMouseWheelEnabled(true);
		return chartPanel;
	}
	
	private void setRenderer(XYPlot plot) {
		final XYItemRenderer renderer = plot.getRenderer();
		
		if (renderer instanceof StandardXYItemRenderer) {
			final StandardXYItemRenderer itemRenderer = (StandardXYItemRenderer) renderer;
			itemRenderer.setSeriesStroke(0, new BasicStroke(2.0f));
			itemRenderer.setSeriesStroke(1, new BasicStroke(2.0f));
		}
		
        if (renderer instanceof XYLineAndShapeRenderer) {
        	XYLineAndShapeRenderer lineAndShapeRenderer = (XYLineAndShapeRenderer) renderer;
        	//lineAndShapeRenderer.setBaseShapesVisible(true);
        	//lineAndShapeRenderer.setBaseShapesFilled(true);
        	lineAndShapeRenderer.setSeriesShapesVisible(0, true);
        	lineAndShapeRenderer.setSeriesShapesFilled(0, true);
        }
	}

Open in new window

For me to play I need aself-sufficient code
- this is just part which I cannot compile.

Put System.out.println(something)
within the  if (renderer instanceof XYLineAndShapeRenderer){...  }
to check if it goes there
I left out the block of code that adds values because I am not allowed to share that code. Moreover, it is dependent on other packages in my project.
Would it be possible for you to add dummy values to the charts. Sorry if I am asking too much!
There is no header part either - class, main() etc.

Just invent some simple data.

First place printout.

And the forst  "if" is working ?

I was looking at the class hierarchy and i don't think
it can be instance of both - maybe i'm not right

No, I think if you put three points in each and add the class and main, etc  - that would be good - you should have beetter idea what kind of
numberss should be there
Sure. I'll do that.