|
Disable auto-labels with setXData |
Posted by Micah on Jun-09-2012 00:21 |
|
I'm working on a chart with multiple series with differing sampling frequencies. I have most of it working, except for the labeling. I use setMultiFormat2 to set my date axis format and setXData to set the labels, and it works well almost all of the time. There are instances where the spacing of the labels seems to be handled automatically by ChartDirector, whereas I want every label that matches via setMultiFormat2 (and only those labels) to be shown.
Attached is a sample of the current output. Note that there are labels at points where there is no data point, which I want to avoid.
My code:
my $layer = $chart->addLineLayer2;
$layer->setLineWidth(2);
$layer->setXData($series_1_dates);
$layer->addDataSet($series_1_vals, -1, 'Series1')->setDataSymbol($perlchartdir::SquareSymbol, 7);;
$layer = $chart->addLineLayer2;
$layer->setLineWidth(2);
$layer->setXData($series_2_dates);
$layer->addDataSet($series_2_vals, -1, 'Series2')->setDataSymbol($perlchartdir::SquareSymbol, 7);;
$chart->xAxis->setMultiFormat2(perlchartdir::AllPassFilter(), '{value|h:nn}');
|
Re: Disable auto-labels with setXData |
Posted by Micah on Jun-09-2012 00:25 |
|
For further information, I know it's possible to use setDateScale, but the interval of dates is not always known nor consistent. Sometimes it's in 5-minute intervals, sometimes 15. |
Re: Disable auto-labels with setXData |
Posted by Peter Kwan on Jun-11-2012 22:56 |
|
Hi Micah,
If you look at the y-axis, the labels are at 0, 5, 10, 15, 20, 25. Many of the labels are at positions where there are no data point. (For example, there is no data point at y = 10.) So it is normal the labels are at positions that there are no data points. In fact, the labels are not related to the datapoints. Only the axis range (0 - 25) is related to the data range.
The same applies to an auto-scaled x-axis. The labels may not be related to any data point, but the x-axis range is related to the data range.
Also, for your case, it seems the x-axis is numeric, not date/time. ChartDirector will automatically use a date/time axis only if your x-data looks like being date/time. In your case, may be your data are small numbers like 0, 300, 600, .... ChartDirector in this case will use a numeric axis and the labels will be at x = 0, 100, 200, 300, 400, .... As you use the format {value|h:nn}, the labels become 0:00, 0:01, 0:03, 0:05, 0:06 ....
If you would like an auto-scaled date/time axis, please use:
$c->xAxis()->setDateScale3('{value|hh:nn}');
If ChartDirector knows that the x-axis is date/time, it will probably choose 0, 60, 120, 180, .... If the labels are too dense (there are in sufficient space for per minute label), it may choose 0, 120, 240, 360, .... If you want ChartDirector to use denser labels, you may use Axis.setTickDensity, like:
#minimum label spacing = 30 pixels (default is 50 pixels)
$c->xAxis()->setTickDensity(30);
You mentioned that you want to remove labels if there are no data points at those positions. Suppose your data point are at:
00:00:12, 00:00:13, 0:01:59, 0.03:33, 0:07:44
Do you mean you want to remove all the labels (as it is unlikely any auto axis labels will be at "odd positions"?
In short, ChartDirector does not know and will not detect whether your data are "regularly spaced" or not. Even if your data are "regularly spaced", the spacing can happen to be an "odd interval" (like the regular spacing can be 0:01:17.0127 or some odd number), so it may not be used for axis labelling as well.
If you know that your data must be regularly spaced, and the spacing must be some "nice number" (like 5 minutes, 15 minutes, etc), and you would like to use that number as the label spacing, you may obtain the spacing you want to use and tell ChartDirector about that by using setDateScale. It should be quite easy to determine the spacing under the above assumption. You just need to obtain the spacing between the first two data points for every series, and choose the shortest one.
Hope this can help.
Regards
Peter Kwan |
|