|
Bar chart with small numerical x-values gets squashed |
Posted by Alexandr on Jun-17-2015 20:45 |
|
Hi Peter,
When I use numeric x-values in a bar chart and the difference between maximal and
minimal x is smaller then 1, the bar chart gets squashed. It seems like ChartDirector
has x-axis span minimum set to 1. How can I avoid this behavior?
Here is a minimalistic example, reproducing the issue:
public static void main(String[] args)
{
JFrame frame = new JFrame("sqashed bars issue");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChartViewer viewer = new ChartViewer();
double[] yData = { 85, 156, 179, 211, 123 };
double[] xData = {0.001, 0.002, 0.003, 0.004, 0.005};
XYChart c = new XYChart(250, 250);
c.setPlotArea(30, 20, 200, 200);
BarLayer bl = c.addBarLayer(yData);
bl.setXData(xData);
viewer.setChart(c);
frame.getContentPane().add(viewer);
frame.pack();
frame.setVisible(true);
} |
Re: Bar chart with small numerical x-values gets squashed |
Posted by Peter Kwan on Jun-18-2015 02:21 |
|
Hi Alexandr,
For your case, instead of using Layer.setXData, you may use Axis.setLabels, like:
BarLayer bl = c.addBarLayer(yData);
c.xAxis().setLabels2(xData);
If xData are configured as labels, they are just considered as names for human readable.
In this case, the real x-coordinates for the bars are just the array index 0, 1, 2, 3, 4, ...
Normally, the first x-coordinate (x = 0) is at the left edge of the chart. However, for bar
charts, this will result in half of the first bar falling outside the left edge (since the center
of the bar is at x = 0). So for bar charts, ChartDirector will automatically indent the x-
axis for 0.5 unit on either side. This indentation is suitable for labels based x-axis, which
is the most common for bar charts.
If you use Layer.setXData, then the xData will be used to position the bars. This is most
commonly used when the bars are not evenly spaced. Note that the labels on the x-axis
is not necessarily the same as xData, just like the labels on the y-axis may not be the
same as that of the yData. (For this example, the y-axis labels are "0, 50, 100, 150, 200,
250", while the yData are "85, 156, 179, 211, 123".) If the bars are evenly spaced, which
is the most common case, you may consider to use Axis.setLabels instead of
Layer.setXData.
If you would like to use Layer.setXData, to disable the 0.5 units of indentation, you may
use Axis.setIndent. For example:
c.xAxis().setIndent(false);
Without indentation, you may run into the issue mentioned above - that half of the first
bar may fall outside the chart, and the same for the last bar. To solve the problem, you
may use Axis.setMargin to add additional margin to the x-axis. Also, if the bars are really
unevenly spaced, it may be difficult to automatically determine the bar width. For
example, if the bars are at 0, 4, 6, 9, 11.48, it is difficult to know if the bar width is 1 x-
unit, or 2 x-units or some other values. ChartDirector will guess a bar width, but in case
it is not what you need, you may use BarLayer.setBarWidth to supply the bar width.
Hope this can help.
Regards
Peter Kwan |
Re: Bar chart with small numerical x-values gets squashed |
Posted by Alexandr on Jun-18-2015 23:15 |
|
Thanks for the quick answer, Peter!
Axis.setIndent(false) works for me.
I can't use labels since I am building a histogram which has a second layer, which shows
density curve and its x-values must be synchronized with the bars contained by the bar
layer.
Regards
Alexandr |
Re: Bar chart with small numerical x-values gets squashed |
Posted by Peter Kwan on Jun-19-2015 01:42 |
|
Hi Alexandr,
For your reference, there is a new example in ChartDirector 6.0 that is a histogram with a
bell curve.
http://www.advsofteng.com/doc/cdjava.htm#histogram.htm
Regards
Peter Kwan |
|