Billybong
Billybong

Reputation: 707

Is there a way to zoom a JFreechart chart to a value on X axis?

I have a bunch of charts and I'd like to zoom them all to highlight series at value x +- range based on an event not originating from within the charts.

If I'm reading it correctly all zooming chart methods such as chart::zoomInDomain take screen coordinates. So what I'm looking for is a way to either translate the x domain value to the screen coordinates in the chart or find an alternative way to zoom based on series values.

Upvotes: 1

Views: 292

Answers (1)

trashgod
trashgod

Reputation: 205785

One approach is to adjust the relevant axis to flank the desired range. The exact formulation will depend on your data and goal. As a concrete example, starting from here, the following change to createChart() produces the chart below. It zooms to the value of the last day in the series, flanked by two days on the domain axis and a proportional amount on the range axis:

int n = dataset.getItemCount(0);
double dMin = dataset.getX(0, n - 3).doubleValue();
double dMax = dataset.getX(0, n - 1).doubleValue();
domain.setRange(dMin, 2 * dMax - dMin);
ValueAxis range = plot.getRangeAxis();
double rMin = dataset.getY(0, n - 3).doubleValue();
double rMax = dataset.getY(0, n - 1).doubleValue();
range.setRange(rMin, 2 * rMax - rMin);

image

See also related answers here, here and here.

Upvotes: 1

Related Questions