|
Bar Chart alignment |
Posted by L on Sep-24-2011 04:45 |
|
Hello,
I am using a bar chart with a continuous x axis (time). This is probably not the greatest
idea, but I figured I would ask a few questions.
Is there any way to keep the bars from centering on the x series data (rather, to right align
on the x data) without shifting all of my x values?
It looks like bar gap depends on adjacent data points, so if I had data missing, that would
create a problem and Chart.touchbar would force non adjacent points to touch. Is that
correct? It seems my only option is to set bar width explicitly if I may have points missing.
Is there a better chart type I could use to create bars vs time?
Thanks,
L |
Re: Bar Chart alignment |
Posted by Peter Kwan on Sep-26-2011 02:21 |
|
Hi L,
For a continuous x-axis, it is suggested you set the bar width explicitly, because there is no way to know the correct bar width to use.
In a continuous x-axis, the bars can be located anywhere you like. Suppose the x-coordinates at at:
x = 1, 3.7, NoValue, 6.8. 11.0
In the above case, what should be the bar width? ChartDirector will still try to automatically determine a bar width, but it may not be what you want. So it is better to set the bar width explicitly with your own code.
To position a bar so that its left edge aligns with given x-coordinate (that is, the bar is to the right of the x-coordinate), I can think of the following method:
- Instead of adding a simple bar chart, use a "Multi-Bar Chart" (see "Multi-Bar Chart" sample code) with 2 data sets. In this way, each x-coordinate will have two bars, one to the left and one to the right. The bar width can be set so that the two bars touched.
You may use an empty array as the first data set. In this way, you only see the bar to the right of the x-coordinate.
The code structure is like (in VB/VBScript):
Set layer = c.addBarLayer2(cd.Side)
Call layer.addDataSet(Array(), cd.Transparent)
Call layer.addDataSet(myData, ....)
Call layer.setBarWidth(myBarWidth, myBarWidht / 2)
- You may use Axis.setLabelOffset and Axis.setTickOffset to shift all x-axis labels and ticks by a certain amount along the x-axis, so that they appear to be at the left side of the bar. To achieve this, the bar width must be set so that it is equal to twice the shift distance. (For example, if the labels and ticks are shifted by 20 x-axis units, the bar width should be 40 x-axis units.)
One complication is that in setBarWidth, the bar width is specified in pixel unit. So you would need to convert the x-axis unit into pixel unit. To do this, you may add the bar layer, then use XYChart.layoutAxes to ask ChartDirector to auto-scale and layout the axis, and then use XYChart.getXCoor to convert from x-axis unit to pixel unit.
In my opinion, if the x-coordinates are really evenly spaced, it is better to use a label based x-axis (configured using Axis.setLabels) instead of a true date/time x-axis (or continuous x-axis).
Hope this can help.
Regards
Peter Kwan |
Re: Bar Chart alignment |
Posted by L on Oct-04-2011 03:16 |
|
Hi Peter,
Thank you for your response. I believe you're right, and setting the width of my bars
explicitly is the best way to accurately display my data on a continuous x axis. Your 2 bar
layer solution to the right/left alignment problem works perfectly and saves a lot of
annoying code to shift my time series data.
thanks again,
L |
|