|
Hard coding x-axis labels |
Posted by Thom on May-25-2012 19:32 |
|
Designing an Audiogram line chart (JSP) that requires the x-axis to be labeled as the
following:
// The labels for the line chart
String[] labels = {"0", "125", "250", "500", "1000", "2000", "4000", "8000", "8500"};
When CD displays the chart, the labels do not display correctly.
Please advise the best way to handle this. |
Re: Hard coding x-axis labels |
Posted by Peter Kwan on May-25-2012 23:13 |
|
Hi Thom,
You can display any labels you like. They are just names and they can contain anything you like. The names can be considered as having no meaning except for human reading.
The more important question is where you would like the labels to be positioned. Do you want the labels to be equally spaced (that is, the distance between "4000" and "8000" is the same as the distance between "8000" and "8500"), or would you like the labels to be spaced according to their equivalent numeric values (that is, the distance between "4000 and "8000" is 8 times the distance between "8000" and "8500")? Or are you using some special formula to determine how the labels should be positioned?
(The middle part of your labels look like logarithmic, but the first 3 labels look linear, and and the last labels do not fit either the linear or logarithmic scale.)
In the simplest case, the code is like:
String[] labels = {"0", "125", "250", "500", "1000", "2000", "4000", "8000", "8500"};
c.xAxis().setLabels(labels);
Now the x-axis should have the labels you want, where the labels are equally spaced. If you then add layer with 9 data points, the 9 points should become align with these 9 labels.
If you would like to add some points in between two labels, you would need to provide the x-coordinates of the points (using layer.setXData). In the above simplest case, the x-coordinates are 0, 1, 2, 3, 4, 5, 6, 7, 8 for the 9 labels positions. For example, if you want to put a point in the middle of the "2000" and "4000" labels, you may use the x-coordinate 5.5.
Hope this can help.
Regards
Peter Kwan |
Re: Hard coding x-axis labels |
Posted by Thom on May-25-2012 23:29 |
|
Thank you for the quick response.
I used the code provided but it bunches all the labels at point 0. They do not distribute
across the x-axis.
It is mandatory to have the lines spaced according to the numerical values and then have
the ability to set the y-axis values to appear on each line.
For instance, y-axis ranges from 0-120 incremented by 10.
If y-axis = 15, I need it plot that point on line 500. (15,500)
Hope this clears it up a bit. |
Re: Hard coding x-axis labels |
Posted by Peter Kwan on May-26-2012 02:56 |
|
Hi Thom,
If the labels are crowded at 0 with the Axis.setLabels, it is probably because your code is using x-coordinates that are not consistent with axis labels. Are you using x-coordinate from 0 to 8 as mentioned in my previous message, or are using some other x-coordinate ranges?
You mentioned you would like "to have the lines spaced according to the numerical values". I assume that means the spacing between the labels "4000" to "8000" should be 40 times wider than the spacing between "0" to "125", because the numeric values are 40 times bigger. If this is the case, you may try the following code:
//The labels are not just names, but have positional signifance. So they should
//be treated as numbers, not strings.
double[] labels = {0, 125, 250, 500, 1000, 2000, 4000, 8000, 8500};
c.xAxis().setLinearScale(0, labels[labels.length - 1], Chart.NoValue);
for (int i = 0; i < labels.length; ++i)
c.xAxis().addLabel(i, "" + i);
For the y-axis, if you want the y-axis range to be 0 - 120 incremented by 10, please use:
c.yAxis().setLinearScale(0, 120, 10);
Hope this can help.
Regards
Peter Kwan |
Re: Hard coding x-axis labels |
Posted by Thom on May-26-2012 04:45 |
|
Here is a code snippet for the x-axis and it still bunches at 0 even with your code
recommendation:
double[] dataX = {0, 125, 250, 500, 1000, 2000, 4000, 8000, 8500};
double[] labels = {0, 125, 250, 500, 1000, 2000, 4000, 8000, 8500};
c.xAxis().setLinearScale(0, labels[labels.length - 1], Chart.NoValue);
for (int i = 0; i < labels.length; ++i)
c.xAxis().addLabel(i, "" + i);
Appreciate all your assistance. I feel like I'm missing something basic. |
Re: Hard coding x-axis labels |
Posted by Thom on May-28-2012 23:43 |
|
Peter,
I have corrected this.
I was attempting to reset the x-axis with my layer.setXData(data0) statement below. I
commented out this line and it worked as expected.
Thank you for your assistance. Lessons learned.
LineLayer layer = c.addLineLayer(data0, 0xff0000);
//layer.setXData(data0);
layer.setLineWidth(1); |
Re: Hard coding x-axis labels |
Posted by Peter Kwan on May-29-2012 00:53 |
|
Hi Thom,
Acutally, there is a mistake in my original code. If you use setXData, the code should be:
c.xAxis().setLinearScale(0, labels[labels.length - 1], Chart.NoValue);
for (int i = 0; i < labels.length; ++i)
c.xAxis().addLabel(labels[i], "" + labels[i]);
If you do not use setXData, then the simplest code is:
c.xAxis().setLabels(labels);
Hope this can help.
Regards
Peter Kwan |
|