|
X axis shows points apart from the ones passed in setXData |
Posted by Anu on Oct-11-2011 18:33 |
|
Hi,
I'm using the following code to set the data in a column chart. The data getting passed to method setXData using list2Date(labelList,timeZone) is an array of dates having 11th October 2011 11 a.m, 11th October 2011 12 p.m, 11th October 2011 1 p.m., 11th October 2011 2 p.m, 11th October 2011 3 p.m, 11th October 2011 4 p.m, 11th October 2011 5 p.m. My issue is that the final Column Chart getting generated is also showing timings 11:30 a.m., 12:30 p.m., 1:30 p.m, 2:30 p.m, 3:30 p.m, 4:30 p.m on it.
Can someone explain why..
int barWidth = (int)(350 / labelList.size() * 0.8);
columnchart.xAxis().setMargin(barWidth+(barWidth/2), barWidth+(barWidth/2));
layer.setXData(list2Date(labelList,timeZone)); |
Re: X axis shows points apart from the ones passed in setXData |
Posted by Peter Kwan on Oct-12-2011 01:33 |
|
Hi Anu,
Do you mean that the chart displays normally and all bars are at correct position, but the labels on the axis shows 11:00am 11:30am 12:00pm 12:30pm .....
Note that for any auto-scaled axis, the labels on the axis are not related to the data values, but to the data range.
For example, consider the y-axis. Suppose your data are (46, 152, 167, 215, 222). ChartDirector may choose (0, 50, 100, 150, 200, 250) as the y-axis labels, even though none of the data values are the same as the label values. It is perfectly normal and is the expected behaviour. The same applies to the x-axis.
For your case, I suggest you may use a label based x-axis. To do this, you may replace the 3 lines of code you are using with one line:
columnchart.xAxis().setLabels2(list2Date(labelList,timeZone), "{value|hh:nn a}");
With the above code, the labels are exactly as provided by your array.
Hope this can help.
Regards
Peter Kwan |
Re: X axis shows points apart from the ones passed in setXData |
Posted by Anu on Oct-12-2011 14:17 |
|
Hi Peter,
THANKS a ton for your help. It worked for me. And yes you were right. The bar positions were always correct. Just that some extra labels were visible on the X-axis.
I no more need to add the formula for getting bar width and then setting the margin on it's basis. Is the below formula the default one for calculating bar width for column charts? Please let me know if it isn't..
int barWidth = (int)(350 / labelList.size() * 0.8)
Thanks,
Anumeha |
Re: X axis shows points apart from the ones passed in setXData |
Posted by Anu on Oct-12-2011 15:54 |
|
Hi,
I need to set the margin in my column chart as the 1st and the last columns always come as truncated. So, I did the following....
int barWidth = (int)(350 / labelList.size() * 0.8);
columnchart.xAxis().setMargin(barWidth+(barWidth/2), barWidth+(barWidth/2));
Is this way correct? If not, then please let me know some other way by which I can get the actual bar width of the generated column chart so that I can use it to set the margin.
Peter, the code suggested by you worked perfectly for less no. of columns in the chart but for n no. of columns all the corresponding dates come as labels and so nothing is visible on the X-axis.
Do I need to open a separate thread for it?
Thanks,
Anumeha |
Re: X axis shows points apart from the ones passed in setXData |
Posted by Peter Kwan on Oct-12-2011 18:26 |
|
Hi Anu,
Usually, I would suggest the Axis.setLabels method, which would automatically configure the axis to ensure the first and last bars would not go outside the plot area.
If you are worried there are too many labels, you may use Axis.setLabelStep. For example:
//step the labels so that there are no more than 6 labels on the x-axis
columnchart.xAxis().setLabelStep((labelList.size() - 1)/ 6 + 1);
You may rotate the labels by 90 degrees if you want to be able to fit more label on the x-axis (using Axis.setLabelStyle).
If you would like to use the setXData method, the method used internally in ChartDirector is:
int barSpacing = 350 / labelList.size();
columnchart.xAxis().setMargin(barSpacing / 2, barSpacing / 2);
In other words, the margin is not based on the bar width, but based on the spacing between the bars.
If you want the auto-scaling axis labels not to show half-hour increments, you may use Axis.setMinTickInc to set the minimum tick increment to 3600 seconds (= 1 hour).
Hope this can help.
Regards
Peter Kwan |
|