|
Origin of bars from y-axis |
Posted by Bonnie on Aug-19-2011 03:18 |
|
Hi,
I have a bar graph. The origin of the bars is coming from 0. How can I set it to come from some other y-axis value?
Here are some snippets of my code:
// Normal x-coordinates - tick positions
double[] xCoor = new double[number];
for (int i = 0; i < xCoor.length; ++i) {
xCoor[i] = i;
}
// Shift by -0.2 to shift the box-whiskers to the left
double[] leftShift = new ArrayMath(xCoor).sub(0.21).result();
// Shift by +0.2 to shift the box-whiskers to the left
double[] rightShift = new ArrayMath(xCoor).add(0.21).result();
// For current year data bars
//(boxTop[], boxBottom[], maxData[], minData[], midData[], fillColor, whiskerColor, edgeColor)
BoxWhiskerLayer bwlayer1 = chart.addBoxWhiskerLayer(null, null, curhighs, curlows, null, 0xCC0000, 0xCC0000, 0xCC0000);
bwlayer1.setXData(leftShift);
bwlayer1.setDataGap(0.9); //space between each box whisker
bwlayer1.setLineWidth(2);
// For prevoius year data bars
BoxWhiskerLayer bwlayer2 = chart.addBoxWhiskerLayer(null, null, prevhighs, prevlows, null, 0xCC0000, 0xCC0000, 0xCC0000);
bwlayer2.setXData(rightShift);
bwlayer2.setDataGap(0.9); //space between each box whisker
bwlayer2.setLineWidth(2);
// New x coordinates to have line cross over all bars
double[] linexCoor = {-0.4, number-0.6};
// Add line
LineLayer llayer = chart.addLineLayer(value, 0x8000);
llayer.setXData(linexCoor); //set to the new x coordinates
llayer.setLineWidth(3);
BarLayer mblayer = chart.addBarLayer2(Chart.Side, 2);
mblayer.addDataSet(curdata, 0x3366CC, labels[0]);
mblayer.addDataSet(prevdata, 0xFFCC66, labels[1]);
chart.xAxis().setLinearScale(0, xCoor.length - 1, xAxisTickLabels);
chart.yAxis().setAutoScale(0.1, 0.1);
Thanks! |
Re: Origin of bars from y-axis |
Posted by Peter Kwan on Aug-19-2011 23:56 |
|
Hi Bonnie,
You may use Layer.setBaseLine. For example:
mblayer.setBaseLine(10); //base line of the bars is at y = 10
Hope this can help.
Regards
Peter Kwan |
Re: Origin of bars from y-axis |
Posted by Bonnie on Aug-21-2011 09:54 |
|
setBaseLine() does not seem to be a method of the BarLayer class. I'm not able to use the statement mblayer.setBaseLine(). Am I missing a step? |
Re: Origin of bars from y-axis |
Posted by Bonnie on Aug-23-2011 02:02 |
|
Could it be that we have an older version? I believe we have the Java 4.1 jar file. |
Re: Origin of bars from y-axis |
Posted by Bonnie on Aug-23-2011 02:32 |
|
Yes, that's the problem - we're using an older version that doesn't support that method. Do you have any suggestions as to how to do what we're asking without updating the source? Was there an old way of setting the base line?
Thanks! |
Re: Origin of bars from y-axis |
Posted by Peter Kwan on Aug-23-2011 02:56 |
|
Hi Bonnie,
Yes. Layer.setBaseLine is a new method introduced in ChartDirector Ver 5. For earlier versions of ChartDirector, you may use the following method:
Suppose you want to set the base line to y = 40. First, subtract 40 from your bar data. The ensures the bar length is what you want (eg. if the base line is 40, a bar of value 50 will have length 10).
Then use setUseYAxis to bind the bar layer to a hidden axis, which can be the right y-axis (yAxis2) or an additional axis (created with XYChart.addAxis). To make an axis hidden, simply set its colors (using Axis.setColors) to transparent.
Finally, synchronize the hidden axis with the real y-axis with an offset of -40 (using Axis.syncAxis). It means the zero point of the hidden axis will be aligned with y = 40 of your visible axis.
The resulting bar will start from 0 of the hidden axis, which is equal to 40 of the visible axis, so it appears the bars start at 40 and are also of the correct length.
If you are displaying the bar values on the chart or on tooltips, you would need to use {={value}+40} to represent the value you would like to display.
Hope this can help.
Regards
Peter Kwan |
|